0

I am calling a web api through a dll file which is being used by .net client desktop application. Dll file is written in .Net 4.0.

Sometimes I get frequent errors on this line.

var postResponse = client.PostAsJsonAsync(path, Input).Result;

After the error message i check IIS and web service log files. As per the log files, the above line of code didn't get to web service because web method logs all activities in its own log file.

Is there a way to avoid timeout errors.

Dll file code

 private T CallService<T>(string path)
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = EndpointAddress;
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.Timeout = TimeSpan.FromSeconds(30);

            var response = client.PostAsJsonAsync(path, Input).Result;

            if (response.IsSuccessful)
            {
                var serviceResponse = response.Content.ReadAsAsync<T>().Result;

               // return service response;
            } 
            else
            {
               // return default (T)
            }
        }
    }

Web Service Method

       [HttpPost]
        public CusResponse GetCustomer(CustomerOperationInput input)
        {


        }

Here is the header data when it times out

{StatusCode: 500, ReasonPhrase: 'Internal Server Error', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  Pragma: no-cache
  Connection: close
  Cache-Control: no-cache
  Date: Wed, 06 Nov 2019 11:06:45 GMT
  Server: Microsoft-IIS/7.5
  X-AspNet-Version: 4.0.30319
  X-Powered-By: ASP.NET
  Content-Length: 36
  Content-Type: application/json; charset=utf-8
  Expires: -1
}}

Error from IIS trace file

<EventData>
  <Data Name="ContextId">{00000000-0000-0000-230F-008000000019}</Data>
  <Data Name="ModuleName">ManagedPipelineHandler</Data>
  <Data Name="Notification">128</Data>
  <Data Name="HttpStatus">500</Data>
  <Data Name="HttpReason">Internal Server Error</Data>
  <Data Name="HttpSubStatus">0</Data>
  <Data Name="ErrorCode">0</Data>
  <Data Name="ConfigExceptionInfo"></Data>
 </EventData>

enter image description here

enter image description here

user1263981
  • 2,953
  • 8
  • 57
  • 98
  • Have you tried `client.Timeout = Timeout.Infinite`? – Theodor Zoulias Nov 06 '19 at 11:02
  • 1
    Also take a look at the [documentation](https://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient) of the `HttpClient` class: *`HttpClient` is intended to be instantiated once and re-used throughout the life of an application. Instantiating an `HttpClient` class for every request will exhaust the number of sockets available under heavy loads.* – Theodor Zoulias Nov 06 '19 at 11:04
  • 2
    `.Result` is wrong. You shouldn't access an async `Result` like this, it can cause deadlocking. Which I'd imagine is your issue here. `CallService` should be `async` and you should call this method using `await` – Liam Nov 06 '19 at 11:11
  • 2
    Does this answer your question? [await works but calling task.Result hangs/deadlocks](https://stackoverflow.com/questions/17248680/await-works-but-calling-task-result-hangs-deadlocks) – Liam Nov 06 '19 at 11:12
  • aren't async tasks only supported in .Net Framework 4.5 onwards. – user1263981 Nov 06 '19 at 11:51
  • HttpClient was added in 4.5. You're already working with 4.5, so why use all those blocking calls? 4.0 is *not* supported anyway. 4.5 came out *7* years ago. The earliest supported version is 4.5.2 It's high time you upgraded to a current version – Panagiotis Kanavos Nov 06 '19 at 15:10
  • No I am not working with 4.5. – user1263981 Nov 06 '19 at 16:51

1 Answers1

1

This is not a timeout. A timeout means: "I have given up waiting because I got no response".

However, you got a response from the server. The fact that you have "header data" at all shows that. The reply was 500 Internal Server Error. That will happen automatically in ASP.NET when an unhandled exception is thrown, but it can also be explicitly sent. Either way, it means something bad happened.

The Content-Type header is set to application/json, so the body of the response may have an error message that you can use to figure out what went wrong.

So read the body of the response.

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84