Gracefully stopping ASP.NET Core Web App (from within Visual Studio debugger), an IHostedService-related question
Asked Answered
G

2

11

I am totally new to ASP.NET Core so I am probably missing something by 50 miles because I've spent 3 hours on this to no avail.

I've made a Web App from within VS 2017, hosted by Kestrel which runs under IIS Express (which is all default when you make a new web app). When I press F5 the app fires up, my Chrome browser opens up and I see the 'hello world'-esque output. When I close this browser window the app terminates.

Now, I need some tasks going on in the background and I need these tasks to have cleanup logic - I found that IHostedService allows me to do this.

The documentation states:

When you register an IHostedService, .NET Core will call the StartAsync() and StopAsync() methods of your IHostedService type during application start and stop respectively.

I have done:

public void ConfigureServices(IServiceCollection services)
{
    services.AddHostedService<MyBackgroundService>();
}

And the service starts as expected, everything is fine. Extremely simple stuff. But it never stops, not gracefully at least.

When I close that auto-started browser window, my app closes along with it but this method in my IHostedService service:

public Task StopAsync(CancellationToken cancellationToken)
{
    ...
}

Is never called.

I have tried the example provided in documentation and it exhibits the same issue - the cleanup is never done when I try it in VS.

I have noticed a peculiar information from an output channel called "ASP.NET Core Web Server":

BackgroundTasksSample> Application started. Press Ctrl+C to shut down.

It's obviously some redirection from the console as this app appears to be a console app in the first place. So I wonder if VS is, by seemingly hiding the console from me, preventing me from performing the graceful shut down of my app?

I also noticed a little IIS Express tray icon which lists any apps I've ran and it has a Stop button which doesn't seem to do anything. I assume IIS Express is here simply acting as some kind of a proxy.

I am dumbfounded, especially since I seem to be the only one having this problem.

How do I properly terminate my Web App?

Grindery answered 3/4, 2019 at 16:14 Comment(2)
Docs say "If the app shuts down unexpectedly, StopAsync might not be called. Therefore, any methods called or operations conducted in StopAsync might not occur." - I'm guessing that closing the browser window counts as an 'unexpected' shutdown?Elsie
@Elsie you guessed correctly, that is indeed the case.Grindery
G
8

I have found the solution. First, I shall quote myself:

I also noticed a little IIS Express tray icon which lists any apps I've ran and it has a Stop button which doesn't seem to do anything. I assume IIS Express is here simply acting as some kind of a proxy.

Actually that right there is the way to gracefully stop your Kestrel app which is hosted under IIS Express (which is a default option in VS).

Apparently, my IIS Express had a... "hickup", and the Stop command didn't work - there was no error message, simply nothing happened. After closing all the VS instances and killing dotnet.exe process in Task Manager I opened up the solution again and was finally able to stop the app.

Grindery answered 4/4, 2019 at 9:25 Comment(1)
I am not so sure why this was such a challenge for me to figure out (i.e. how to stop site running in Visual Studio 2019), but until I came across this answer I did not have it figured out.Hegumen
H
6

I am adding an "answer" to supplement the answer given by @bokibeg. To stop a project running in iis in Visual Studio 2019 go to your system tray and right click on the iis express icon. Then select the project that is running. A menu will pop up with a button to stop site. enter image description here

Hegumen answered 23/3, 2020 at 17:14 Comment(1)
Interestingly, I just noticed after stopping the site using this method (I am using Visual Studio 2019 working on am ASP.NET Core 3.1 project) the stop button now appears (which is what I have become accustomed to in older versions of Visual Studio).Hegumen

© 2022 - 2024 — McMap. All rights reserved.