0 Comments

When working in an OWIN project (using Microsoft.Owin.Host.SystemWeb) you know when you are at the start of the application, because you will most likely have a Startup.cs class with a Configuration method. This is where you can use the IAppBuilder to configure your application, or even to invoke other classes that have work to do when the application starts. But what about when the application ends? Well there is a nice yet not very well documented (IMO) way of doing this:

public void Configuration(IAppBuilder app)
{
    var properties = new AppProperties(app.Properties);
    var token = properties.OnAppDisposing;

    if (token != CancellationToken.None)
    {
        token.Register(() =>
        {
            // do stuff here for ending / disposing
        });
    }
}

If you are familiar with OWIN you will already know that the app.Properties is loaded with info, but it’s a Dictionary<string,object>, so you kind of have to go look for what you want and cast it. With the AppProperties class (from the Microsoft.Owin.BuilderProperties) you can easily access some predefined properties that are in the app.Properties, and those will now be strongly typed (yeah !). So now in order to “hook” into the end of my application I only need to register an action on the OnAppDisposing token and bang, once my application is ending (disposing) this code will execute.

Don’t make the same mistake I did the first time I tested that. Of course I lauched my application in debug mode, with two breakpoints, one at the beginning of the Configuration method and the other in the beginning of the registered action. Of course the first break point was hit and everything was perfect, then I did Shift+F5 to stop debugging…..and nothing happened the second breakpoint was not hit, what the…. Yeah I know what you will say I stopped debugging, I did not end(dispose) my application. So I restarted the application in debug mode and then I stopped my IIS express (or any other host) and VOILA !!!! the other breakpoint was hit! Congrats to me :)

 

Mat