Skip to main content

How to resolve ASP.NET core web API 2 mins timeout issue


Introduction

We are in the new world of microservices and cross-platform applications which will be supported for multiple platforms and multiple heterogeneous teams can work on the same application. I like ASP.NET Core by the way its groomed to support modern architecture and adhere to the software principles. I am a big fan of dot net and now I become the craziest fan after seeing the sophisticated facility by dot net core to support infrastructure level where we can easily perform vertical and horizontal scaling. It very important design aspect is to keep things simple and short and by the way, RESTFul applications are build and it is a powerful mantra for REST-based application and frameworks. Some times we need to overrule some principles and order to handle some situations. I would like to share my situation of handling HTTP long polling to resolve the ASP.Net core 2 mins issue.

What is HTTP Long polling?

In the RESTFul term, when a client asks for a query from the server, the server responds to the request, and then it will terminate.  Our client frameworks designed in the way to handle resilient situations where it can wait for some limited amount of time to get the server response and if not it will throw the timeout error. It is applicable for HTTP Client in our ASP.NET Core web API.
But In some situations, the server needs some extra time to respond to the client request where we need to open the HTTP Connection active irrespective of framework-specific time. This is called HTTP Long polling.

What is the issue with HTTP Long Polling in ASP.NET Core?

To meet my requirement I dragged to the situation of the Long polling in one of my ASP.NET Core applications. I was clueless to resolve the following issue where it always fails for 2 mins wherein case I need a lot more time to sustain the request.  I got this weird error

502 - Web server received an invalid response while acting as a gateway or proxy server


I tried almost all possible ways to resolve but whenever I hit the button merely 2 mins my request fails and throws the error wherein I need to standby a little more long time.

Approach 1 – Connection HTTP Header

This Issue gives me a little more opportunity to learn about Connection HTTP Header. Let me share my thoughts.  By default in c#, the HTTP Client Header object Connection key has Keep-Alive as value.

What is HTTP Keep-Alive?

Consider that we are visiting a site, By default, the browser will create a new HTTP Connection for each resource for the page we are visiting. This will increases the page load time of your site and this is the main performance hindrance. Microsoft used their mantra to reduce the page load time or API load time by using Keep-Alive value to the connection key in Http Client key object. by the way, It will create only one HTTP Connection object and all subsidiary requests will use the same object and so we gain performance.
I thought of using this to enable HTTP Long Pooling in my application. But I got no luck. My Application throws an error immediately after 2 mins. I was clueless at this movement.

Approach 2 – IIS Connection Timeout

I just tried with increasing Connection Time to 3600 secs in my IIS Connection settings where my site is hosted.
Now also I got no luck.

Approach 3 – Kestral Request Time out

I got to know that the IIS does not affect any of the settings that I made. This is a strange thing for me because I resolved time out problems in ASP.NET by adjusting values in IIS. What’s new in ASP.NET Core? These things make me more interested in learning the hosting modal of ASP.NET core.
Our Thala mantra “Never ever give up!!!
I need to solve the Long Polling where my client is crushing me to get the solution. With the energy of our thala mantra, I started learning the hosting modal. I would like to share a few things which I learned

Kestral Web Server

It is the cross-platform hosting modal where we can host our ASP.NET Core application in IIS, Nginx, and Apache web servers. This is the cross-cutting model which helps us host our application in all platform that .NET  Core supports.
By default, ASP.NET Core application is having out process hosting modal wherein the IIS server will act as Reverse Proxy/Gateway to the ASP.NET Core Kestral web server. In simple words, the application will request the IIS Server and it will forward the request to the kestrel web server and so settings made in IIS has no effect on my changes.

Important Note

Kestral web server has default HTTP time out of 2 mins and so we can’t sustain the HTTP Long Polling.
If we increase the HTTP time out of  Kestral then we can sustain the HTTP Long polling in ASP.NET Core applications and this will be applicable only for out process hosting modal. I did the following on the Web. Config of my application which resolves my 2 mins timeout issue
<?XML version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
</handlers>
<aspNetCore requestTimeout="01:00:00" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
We need to clean publish to apply these changes properly in the webserver.

Conclusion

I guess this article could help fellow developers like me who are trying to resolve the same kind of issue. Please share if you have different thoughts so that I could furnish this article which would help others.

Comments

  1. Great Post!!! Thanks for the data update and waiting for your new updates.
    what does .net framework do
    do i need .net framework

    ReplyDelete
  2. Awesome blog! This is a helpful article. This blog is clear and with lots of useful information. Thanks for sharing more blogs.
    Virginia Family Laws
    Virginia Sex Crimes Lawyer

    ReplyDelete
  3. The blog post about software design corner is really interesting. I'm interested to see what kind of information this blog will have me learn. I'm also interested in seeing more posts about software design, especially in terms of ASP.NET core web API 2 mins timeout issue.
    Learn ASP.NET

    ReplyDelete
  4. Hi, I have applied above solution in my web config file then my connectionString doesn't work. found connection initialize exception.

    ReplyDelete
  5. How to apply timeout is 5 seconds. If response is not received within 5 seconds.
    Please help me

    ReplyDelete

Post a Comment

Popular posts from this blog

How to Resolve ASP.NET Core Key Protection Ring Problem in AWS Lambda

Introduction When it comes to server less web application design using asp.net core razor pages, we definitely need to consider a factor of data protection key management and its lifetime in asp.net core. I developed a site using AWS toolkit of ASP.NET Core Razor Pages. The main advantage of ASP.NET Core is cross-platform from where we can deploy our application in MAC, Linux or windows. I deployed my site initially in IIS Server from which I got the results as expected .but later period I decided to host my site in AWS Lambda in order to meet our client requirement. Strangely, I got unexpected behavior from my site. I just refer the cloud information Lambda Log to identify or pinpoint the case, I got the error Information like “Error Unprotecting the session cookie” from the log. In this article, I tried to explain the root cause of the problem and its solution to overcome such kind of issue. Data Protection in ASP.NET Core This is feature in ASP.NET Core which acts as repl

Which linq method performs better: Where(expression).FirstorDefault() vs .FirstOrDefault(expression)

 Introduction When it comes to LINQ, we always have multiple options to execute the query for the same scenario. Choosing correct one is always challenging aspect and debatable one. In one of our previous articles   Any Vs Count  , we have done performance testing about best LINQ methods over .NET types. In this article, I would like to share about  Where(expression).FirstorDefault() vs .FirstOrDefault(expression) Approaches Performance testing for  Where(expression).FirstorDefault() vs .FirstOrDefault(expression) is very interesting IEnumerable<T> or ICollcetion<T>  .FirstOrDefault(expression) is better than  Where(expression).FirstorDefault() Public API To check the performance, I need some amount of data which should already available. So I decided to choose this  public api . Thanks to publicapis Public API Models Entry class using System ; using System.Collections.Generic ; using System.Text ;   namespace AnyVsCount { public class Entry { pub