-2

I am not getting a response from a Dynamics CRM web server, and was wondering if there is anything wrong with my below code. I am building a JSON payload and sending the request to a server using WEB API endpoint

using (HttpClient httpClient = new HttpClient()) {
    Uri requesturi = new Uri(string.Format("{0}/api/data/v8.2",url));
    httpClient.BaseAddress = requesturi;
    httpClient.Timeout = new TimeSpan(0, 10, 0);  // 10 minutes
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result);
    httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, string.Format("/contacts",contact));                   
    request.Content = new StringContent(props.ToString(), Encoding.UTF8, "application/json");
    HttpResponseMessage createResponse1 = await httpClient.SendAsync(request);
    return createResponse1.StatusCode;            
}
Daryl
  • 18,592
  • 9
  • 78
  • 145
Ravi Shastri
  • 57
  • 1
  • 2
  • 12

1 Answers1

1

Lower the following line.

Old

httpClient.Timeout = new TimeSpan(0, 10, 0); // 10 minutes 

New

httpClient.Timeout = new TimeSpan(0, 0, 10); // 10 seconds

10 minutes is an amazingly long time to wait for request to complete. 30 seconds is an eternity for a web service request.

As you have specified such a long timeout if there is a transient error or lookup issue you will need to wait 10 minutes for it to timeout.

Chances are you will now get an error or exception from the code.

Adam Carr
  • 678
  • 4
  • 13