Is there Application_End from Global.asax in Owin? Is there Application_End from Global.asax in Owin? asp.net asp.net

Is there Application_End from Global.asax in Owin?


AppProperties, found in Microsoft.Owin.BuilderProperties, exposes the CancellationToken for OnAppDisposing.

You can get this token and register a callback to it

public class Startup{    public void Configuration(IAppBuilder app)    {        var properties = new AppProperties(app.Properties);        CancellationToken token = properties.OnAppDisposing;        if (token != CancellationToken.None)        {            token.Register(() =>            {                // do stuff            });        }    }}


I packaged this up in a little helper so you can do this:

public class Startup{    public void Configuration(IAppBuilder app)    {        app.OnDisposing(() =>        {            // do stuff        });    }}

The helper:

static class AppBuilderExtensions{    public static void OnDisposing(this IAppBuilder app, Action cleanup)    {        var properties = new AppProperties(app.Properties);        var token = properties.OnAppDisposing;        if (token != CancellationToken.None)        {            token.Register(cleanup);        }    }}