Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there Application_End from Global.asax in Owin?

Startup.cs is a new way to initialize your app instead of Application_Start in Global.asax and it's fine. But is there a place to put my teardown logic, for example this:

public class WebApiApplication : System.Web.HttpApplication {   protected void Application_End()   {     // Release you ServiceBroker listener     SqlDependency.Stop(connString);   } } 

Looked in Microsoft.Owin namespace but it only seems to have OwinStartupAttribute. Does this mean that application lifecycle events are still processed by System.Web.HttpApplication instance and are not supported by OWIN specification?

like image 888
2ooom Avatar asked Dec 12 '14 13:12

2ooom


2 Answers

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             });         }     } } 
like image 179
Mihai Dinculescu Avatar answered Oct 03 '22 06:10

Mihai Dinculescu


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);         }     } } 
like image 24
Thomas Eyde Avatar answered Oct 03 '22 07:10

Thomas Eyde