有没有从Global.asax中的Owin Application_End?asax、Global、Application_End、Owin

2023-09-03 01:55:49 作者:半生轻狂客

Startup.cs是一种新的方式来初始化您的应用程序,而不是的Application_Start 在Global.asax中和它的罚款。但有一个地方把我拆卸的逻辑,比如这个:

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);
  }
}

看着 Microsoft.Owin 命名空间,但它只是似乎有 OwinStartupAttribute 。这是否意味着应用程序生命周期事件是由 System.Web.HttpApplication 仍在处理实例和不支持的OWIN规范?

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?

推荐答案

AppProperties Microsoft.Owin.BuilderProperties ,公开的CancellationToken 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
            });
        }
    }
}