Search code examples
node.jstypescripthttpasp.net-corehttpclient

Request timeout for HttpClient


I am working in TypeScript with ASP.NET Core Web API.

Scenario:

I am working with a large data response service on the backend side. That took more than two minutes to load the data. As I know the default timeout for HttpClient is for two minutes for localhost (Chrome hold for request time out), but when I publish my code to the IIS site, after two minutes it gives a 500 Internal Server Error.

On the API side:

I have set the time to 1000 seconds.

 services.AddDbContext<BusinessContext>(options =>
     options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"], opts => opts.CommandTimeout(1000).EnableRetryOnFailure()));

On the web side:

Try 1:

This is a simple service call

async getData(lookUp: Lookup): Promise<PacketSearchItem[]> {
    return await this.http.post(environment.apiBaseUrl + "Packet/ManualSearch", lookUp)
      .toPromise() as PacketSearchItem[];
}

Try: 2

async getData(lookUp: Lookup): Promise<PacketSearchItem[]> {
    return await this.http.post(environment.apiBaseUrl + "Packet/ManualSearch", lookUp).pipe(timeout(10000000))
      .toPromise() as PacketSearchItem[];
}

Try: 3

async getData(lookUp: Lookup): Promise<PacketSearchItem[]> {
    return await this.http.post(environment.apiBaseUrl + "Packet/ManualSearch", lookUp, { headers: new HttpHeaders({ timeout: `${10000000}` })}).pipe(timeout(10000000))
      .toPromise() as PacketSearchItem[];
}

Research on Google:

Reference

I used the upper link code to, but it gives me the same error, because after 2 minutes a request time out occur in Google Chrome.

Error Screenshot:

1) Console Error:

Enter image description here

2.) Network Error:

Enter image description here

I want to wait for Chrome to get the response from the API.

I already added the CORS policy:

 services.AddCors(options => {
     options.AddPolicy("CorsPolicy",
         builder => builder.AllowAnyOrigin()
         .AllowAnyMethod()
         .AllowAnyHeader()
         .AllowCredentials());
});

app.UseExceptionHandler(
    builder => {
        builder.Run(
            async context => {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.Headers.Add("Access-Control-Allow-Origin", "*");

                var error = context.Features.Get<IExceptionHandlerFeature>();
                if (error != null) {
                    context.Response.Headers.Add("Application-Error", error.Error.Message);
                    // CORS
                    context.Response.Headers.Add("access-control-expose-headers", "Application-Error");

                    await context.Response.WriteAsync(error.Error.Message).ConfigureAwait(false);
                }
            });
    });

Solution

  • After a lot of research I found the answer. It is problem in a ASP.NET Core configuration file:

    web.config

     <aspNetCore requestTimeout="00:20:00">
    

    We have to increase the time-out here.

    Helping link:

    Reference