Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Gracefully Abort Asp.Net Core Startup Within ApplicationStarted Event?

Tags:

asp.net-core

Running Asp.Net Core 1.1

Most of the time I want my ASP.NET Core app to run no matter what happens during startup, but there are a couple critical errors during startup that I want it to shut down on. In Startup.cs, I have tried passing the ApplicationLifetime into the ApplicationStarted event during the Configure method:

            appLifetime.ApplicationStarted.Register((al) => {
               var appLtime = (IApplicationLifetime)al;

               try {
                ... various startup code....

               } catch (Exception ex) {
                   appLtime.StopApplication();
               }

            }, appLifetime);

But this ends throwing this error:

Unhandled Exception: System.ObjectDisposedException: The CancellationTokenSource has been disposed. at System.Threading.CancellationTokenSource.ThrowObjectDisposedException() at Microsoft.AspNetCore.Hosting.WebHostExtensions.<>c__DisplayClass0_0.b__0() at System.Action`1.Invoke(T obj)

I'm guessing this is because I'm calling Stop() from within Start(), but I don't know any other way. The application does shut down so maybe it doesn't matter, but if anyone has any suggestions I really appreciate it.

Thanks, Buzz

like image 666
buzzripper Avatar asked Feb 21 '17 14:02

buzzripper


People also ask

What ConfigureServices () method does in startup CS?

The ConfigureServices method is a public method on your Startup class that takes an IServiceCollection instance as a parameter and optionally returns an IServiceProvider . The ConfigureServices method is called before Configure .

What does webhost CreateDefaultBuilder () do?

CreateDefaultBuilder()Initializes a new instance of the WebHostBuilder class with pre-configured defaults.

What is IWebHostBuilder in .NET Core?

UseIIS(IWebHostBuilder) Configures the port and base path the server should listen on when running behind AspNetCoreModule. The app will also be configured to capture startup errors. UseIISIntegration(IWebHostBuilder) Configures the port and base path the server should listen on when running behind AspNetCoreModule.

How does CS handle errors in startup?

To deal with such errors we can use UseStatusCodePages() method (status code pages middleware) to provide status code pages. To enable default text-only handlers for common error status codes, call UseStatusCodePages in the Startup.


1 Answers

I know this is an old question but since it's not answered yet and I just went through the same process I thought maybe I could help others in the same boat.

I'm using ASP.NET Core 2.1 but the solution seems to work on 1.x as well.

Modify your Configure method by adding an IApplicationLifetime parameter to it and adding your ApplicationStarted delegate at the end like so...

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IApplicationLifetime applicationLifetime)
{
    // existing code

    applicationLifetime.ApplicationStartup.Register(() => OnStartUp(applicationLifetime));
}

Create an OnStartUp method in the Startup.cs file like so...

public void OnStartUp(IApplicationLifetime applicationLifetime)
{
    try
    {
        // Do your startup magic
    }
    catch(Exception ex)
    {
        applicationLifetime.StopApplication();
    }
}

If something goes wrong during your OnStartUp code it will now successfully shut down the application without throwing the ObjectDisposedException.

like image 105
Mark Dellacca Avatar answered Jan 02 '23 19:01

Mark Dellacca