简体   繁体   中英

.Net Core api long running request

I have a web app calling a .Net Core API for generating PDF documents. I'm doing a bunch of HTML to pdf conversion so it takes a while to complete 200 pages document. I have it working in my local but not my AppService. At first, I was getting "The specified CGI application encountered an error and the server terminated the process." if I went over 100 pages (small page sets work).

The code calling my api:

var httpClient = new HttpClient();
HttpContent content = new StringContent(JsonConvert.SerializeObject(pdfRecipeDto), Encoding.UTF8, "application/json");
httpClient.Timeout = System.TimeSpan.FromMinutes(30);
var pdfFileUrl = httpClient.PostAsync("http://yada-yada.azurewebsites.net/api/pdf/Generate", content)
                 .GetAwaiter()
                 .GetResult()
                 .Content.ReadAsStringAsync().Result; // yes i know this is gross

I found this post and others that said simular. tried the first answer but after manually modifying my web.config in Kudu (bc i don't know how to set it in my project)

<?xml version="1.0" encoding="utf-8"?>
 <configuration>
  <system.webServer>
    <handlers>
     <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore requestTimeout="00:20:00"  processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

the requestTimeout="00:20:00" being the important part.

But i get a "500 - The request timed out. The web server failed to respond within the specified time.".

i also tried the adding .UseKestrel(...) to my program.cs

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
    .UseApplicationInsights()
            .UseStartup<Startup>()
            .UseKestrel(o =>
            {
                o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30);
            })
            .Build();;

But that seemed to do nothing.

Yes, Azure Load Balancer has a default idle timeout setting of approximately four minutes it's documented in the FAQ here: https://docs.microsoft.com/en-us/azure/app-service/app-service-web-availability-performance-application-issues-faq#why-does-my-request-time-out-after-230-seconds

Q: Why does my request time out after 230 seconds?

Azure Load Balancer has a default idle timeout setting of four minutes. This is generally a reasonable response time limit for a web request. If your web app requires background processing, we recommend using Azure WebJobs. The Azure web app can call WebJobs and be notified when background processing is finished. You can choose from multiple methods for using WebJobs, including queues and triggers. WebJobs is designed for background processing. You can do as much background processing as you want in a WebJob. You could use webjob for your task and see how it goes.

Further, all Azure Web Apps (as well as Mobile App/Services, WebJobs and Functions) run in a secure environment called a sandbox. Each app runs inside its own sandbox, isolating its execution from other instances on the same machine as well as providing an additional degree of security and privacy which would otherwise not be available. Checkout the GitHub page https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#unsupported-frameworks , which lists the supported and unsupported PDF generators on Azure App Service.

So turns out Azure App Service have a hardcoded 230 seconds rule for requests. even if you make changes to the web.config and/or startup.cs.

I found the answer here: https://stackoverflow.com/a/38676086/5919289 And https://social.msdn.microsoft.com/Forums/en-US/17305ddc-07b2-436c-881b-286d1744c98f/503-errors-with-large-pdf-file?forum=windowsazurewebsitespreview

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