简体   繁体   English

从.net核心WEB API获取500错误以进行长期运行的任务

[英]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. 我正在使用.net核心API从其他服务中检索数据并将数据传递给Angular应用程序。 I am facing 500 exception from the API for long running tasks after waiting sometime. 等待一段时间后,我将面临长时间运行任务的API异常500。 I wanted to wait my .net core API for long time for getting the data from other services. 我想等我的.net核心API很长时间才能从其他服务获取数据。 I added the keep alive timeout in the Program class as below. 我在Program类中添加了保持活动超时,如下所示。

 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 . 我也通过实施IHostedService按照文章与后台服务试图在这里 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. 您在服务器上的代码中设置的KeepAliveTimeout是处理传入请求的超时。 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 如果您使用HttpClient调用外部服务,请查看设置其HttpClient.Timeout属性 ,该属性

Gets or sets the timespan to wait before the request times out. 获取或设置请求超时之前要等待的时间跨度。

And

The default value is 100,000 milliseconds (100 seconds). 默认值为100,000毫秒(100秒)。

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. 就我而言,API应该快速而轻松。

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. 如果外部服务的数据不太不稳定,则可以查看从外部服务获取信息的其他方式,例如运行cron作业/ webjob / azure函数并将结果存储在某种形式的缓存或存储中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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