简体   繁体   中英

Getting 500 error from .net core WEB API for long running task

I am using .net core API for retrieving data from other services and passing the data to Angular application. I am facing 500 exception from the API for long running tasks after waiting sometime. I wanted to wait my .net core API for long time for getting the data from other services. I added the keep alive timeout in the Program class as below.

 public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
        .ConfigureLogging((hostingContext, logging) =>
        {
            logging.AddConsole();
        })
        .UseStartup<Startup>()
        .UseKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30);});

But still no changes. Also I tried with background services by implementing IHostedService as per the article here . But still facing the issue. I would appreciate someone please help me how to handle this issue.

The KeepAliveTimeout you're setting from code on the server is the timeout for handling incoming requests. Setting that will not help with outgoing requests that timeout.

If you're calling the external service using a HttpClient , have a look at setting its HttpClient.Timeout Property which

Gets or sets the timespan to wait before the request times out.

And

The default value is 100,000 milliseconds (100 seconds).

Slightly off-topic
It feels like this might not be the best structure in getting the information from the external service. An API, as far as I'm concerned, should be quick and light.

If the data of the external service isn't too volatile, you could take a look at other ways of getting the information from the external service like running a cron job/webjob/azure function and storing the result in some form of cache or storage.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM