我想从Global.asax中检索Owin IAppBuilder实例中的属性。我可以想出这样做的唯一方法是使用MapOwinPath和一个我不关心的路径来简单地利用回调(参见下面的示例)。是否有更简洁的方法来访问IAppBuilder实例而无需注册虚假路由?
public static class HostingUtils
{
private static readonly Lazy<CancellationToken> shutdownToken = new Lazy<CancellationToken>(GetOwinShutdownToken);
public static CancellationToken ShutdownToken { get { return shutdownToken.Value; } }
private static CancellationToken GetOwinShutdownToken()
{
CancellationToken? token = null;
new RouteCollection().MapOwinPath("hack", "hack",
map => token = map.Properties["host.OnAppDisposing"] as CancellationToken?);
return token.GetValueOrDefault(CancellationToken.None);
}
}