I implement Graceful shutdown on ASP.NET Core 2.1. I noticed that when you call SIGINT or Ctrl + C in the console, all requests that are currently being processed are interrupted. Accordingly, if the Controller, through its Actions, performs any queries in the DB without transactions, the system may be in a non-consistent state.
From the IApplicationLifetime Description https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.hosting.iapplicationlifetime?view=aspnetcore-2.1
Consider the ApplicationStopping event.
ApplicationStopping Triggered when the application host is performing a graceful shutdown. Requests may still be in flight. Shutdown will block until this event completes.
I expected that the server will wait until the completion of the processing of current requests, prohibiting new arrivals. In reality, it is cut down despite existing requests.
How to wait for the completion of current requests?
Example:
public static void Main(string[] args) { WebHostExtensions.Run(BuildWebHost(args)); } private static IWebHost BuildWebHost(string[] args) { return WebHostExtensions.CreateDefaultBuilder(args) .UseSerilog() .UseStartup<Startup>() .UseUrls("http://0.0.0.0:9100") .Build(); } Controller:
public class UserOrdersController : Controller { public async Task<IActionResult> GetUserOrders(string productId) { await Task.Delay(5000); Console.WriteLine("Finished"); //..................................... } }string productId)public class UserOrdersController : Controller { public async Task<IActionResult> GetUserOrders(string productId) { await Task.Delay(5000); Console.WriteLine("Finished"); //..................................... } }
"Finished" will be displayed, if you send a signal of completion.
.UseShutdownTimeout(TimeSpan.FromSeconds(10))in the builder, but it did not help. - the Destroy