0

I'm trying to make a HttpRequest from my Dynamics CRM online to create a record on the external ERP system. I use HttpClient but I am not sure if I did it correctly, I am not getting any response. Code below:

 public async void Execute(){
        string query1 = $"<RequestData>" +
        $"<firstField>{model.firstField}</firstField>" +
        $"<secondField>{model.secondField}</secondField>" +
        $"<thirdField>{model.thirdField}</thirdField>" +
            $"</RequestData>";
       HttpClient client = CreateClient();
       HttpResponseMessage response = await SendRequest(client, HttpMethod.Put, query);
}

  private HttpClient CreateClient()
    {
        using (HttpClient client = new HttpClient())
        {
            client.BaseAddress = baseAdress;
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            return client;
        }
    }

     private async Task<HttpResponseMessage> SendRequest(HttpClient httpClient, HttpMethod method, string query)
    {
        HttpRequestMessage request = new HttpRequestMessage(method, query);
        return await httpClient.SendAsync(request);
    }
Jarmoosh
  • 1
  • 1
  • 3
    Try remove `using` from `CreateClient` and move that `using` to `Execute` – Aleks Andreev Feb 18 '18 at 10:29
  • 1
    Moreover, you probably [don't need to use `using` with (i.e., dispose) `HttpClient`](https://stackoverflow.com/q/15705092/4934172). Specially if you will be using it more than once, declare it as a class-level variable, initialize it once (in your `CreateClient` method *without `using`*), and you should be good to go. – 41686d6564 stands w. Palestine Feb 18 '18 at 10:37

1 Answers1

2

As soon as the HttpClient is leaving the using statement in the CreateClient method its being disposed. Remove the using statement in the CreateClient method and implement the using in the Execute method instead

public async void Execute()
{
string query = $"<RequestData>" +
$"<firstField>{model.firstField}</firstField>" +
$"<secondField>{model.secondField}</secondField>" +
$"<thirdField>{model.thirdField}</thirdField>" +
    $"</RequestData>";
   using(HttpClient client = CreateClient())
   {
    HttpResponseMessage response = await SendRequest(client, HttpMethod.Put, query);
   }
}

private HttpClient CreateClient()
{
var client = new HttpClient();
   client.BaseAddress = baseAdress;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}

private async Task<HttpResponseMessage> SendRequest(HttpClient httpClient, HttpMethod method, string query)
{
HttpRequestMessage request = new HttpRequestMessage(method, query);
return await httpClient.SendAsync(request);
}
Marcus Höglund
  • 16,172
  • 11
  • 47
  • 69
  • does query should contain the whole url? or the begginning is in the "BaseAddress" and the query is the rest of url? – Jarmoosh Feb 18 '18 at 11:01
  • @Jarmoosh If you set the BaseAddress on the HttpClient then you only need to provide the routes ontop of the base adress for example 'api/user' – Marcus Höglund Feb 18 '18 at 11:13