C
C#2y ago
JulianusIV

✅ Blocking ASP.NET Core Shutdown fully

When i shut down my ASP.NET Core API i have to make 0-n API requests and receive (and answer) that many api requests myself, so i have to block the host shutdown until that is completed. I have tried to use the WebApplication.Lifetime.ApplicationStopped CancellationToken, and subscribed the Register event to block the thread until i got all requests or until a timeout, but the requests end up with a 502, which usually happens when the API is down, so id guess the shutdown of the whole app is blocked, but the controllers still dont get routed to anymore. Does someone know how i can achieve something like this?
11 Replies
JulianusIV
JulianusIV2y ago
Well uhh ill take that as a no
Becquerel
Becquerel2y ago
why do you have to make requests when your API is shutting down?
JulianusIV
JulianusIV2y ago
I need to unsubscribe a lease Basically I start up the api, and it sends out requests, subscribing to a few topics During a lease time i then get api requests back whenever that topic is updated Unsubscribe and subscribe both require me me to respond with a challenge tho, which is why i still need the api up during shutdown
atakancracker
atakancracker2y ago
You can add scope filter to your API controllers to filter whether they are available or not
JulianusIV
JulianusIV2y ago
Ill take a look at that thank you
atakancracker
atakancracker2y ago
https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/filters?view=aspnetcore-6.0 You can add some flag to your config file and switch open/close to specified endpoints/controllers of your API through scope filter
JulianusIV
JulianusIV2y ago
after reading through this i dont exactly know how i would use this to achieve what i want to do, since the api simple refuses the connection as soon as a ctrl + c (or a docker stop) is being sent to it, and even if i were to add filters id still need to be able to get a call into my Controller in order to properly be able to handle it, unless one of those filters is still being sent data during a shutdown, so i can just move my controller logic to a filter (which would probably pose a whole load of other issues with security and the such i imagine)
atakancracker
atakancracker2y ago
if you shut down docker instance or IIS then its just off
JulianusIV
JulianusIV2y ago
i know that docker still gives the container content time to shutdown gracefully same as if you just send a ctrl + c and i had something like this in an earlier version (asp.net core 3.1), where using Lifetime.ApplicationStopped would work (as far as i remember), but since i updated to net7 it just refuses connection immediately but if nothing like that works now ill just add a little admin panel and add a stop feature that handles the whole unsubscribe and then shuts down the api
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
JulianusIV
JulianusIV2y ago
Well ill just add something as an answer here then For just running something in console blocking SIGINT with Console.CancelKeyPress like:
Consoel.CancelKeyPress += (sender, e) =>
{
//do something and wait for api requests
app.Lifetime.StopApplication();
}
Consoel.CancelKeyPress += (sender, e) =>
{
//do something and wait for api requests
app.Lifetime.StopApplication();
}
in your Main will work perfectly fine. Docker however seems to be sending a SIGTERM on stop. SIGTERM can be held up using AppDomain.CurrentDomain.ProcessExit, however the api shutdown is already triggered at that point, which makes the api refuse any connections. The easiest solution here, and what i will be doing, is probably to add the SIGINT handler for when it is ran in console, but also add a button in a management page to shut down the application, which waits for all requests to come in before calling app.Lifetime.StopApplication(), for using it in docker