【问题标题】:Is there Application_End from Global.asax in Owin?Owin 中是否有来自 Global.asax 的 Application_End?
【发布时间】:2015-02-11 05:45:20
【问题描述】:

Startup.cs 是一种初始化应用程序的新方法,而不是 Global.asax 中的 Application_Start,这很好。但是有没有地方可以放置我的拆解逻辑,例如:

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

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

【问题讨论】:

    标签: asp.net .net asp.net-mvc owin katana


    【解决方案1】:

    Microsoft.Owin.BuilderProperties 中找到的AppProperties 公开了OnAppDisposingCancellationToken

    你可以得到这个token并向它注册一个callback

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

    【讨论】:

      【解决方案2】:

      我把它打包成一个小助手,这样你就可以这样做了:

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

      帮手:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多