在VS 2013的MVC5模板中,有一个Startup类,因为被OwinStartupAttribute
装饰而被调用。启动类是否每个服务器运行一个实例?
我可以知道这个启动类是每个IIS运行一次,还是每次发出一个Http请求?
在VS 2013的MVC5模板中,有一个Startup类,因为被OwinStartupAttribute
装饰而被调用。启动类是否每个服务器运行一个实例?
我可以知道这个启动类是每个IIS运行一次,还是每次发出一个Http请求?
该属性指定的类被OWIN框架用来启动应用程序。请参阅本教程中的示例:http://www.asp.net/aspnet/overview/owin-and-katana/owin-startup-class-detection
用OwinStartupAttribute
装饰的类每AppDomain
执行一次。
另一方面,OWIN中间件由微软的OWIN实现管理,并将在每个请求中执行一次。
例如Microsoft.Owin.Security.Cookies.CookieAuthenticationMiddleware
如下所示。在这些封面之下,CookieAuthenticationHandler
类被OWIN多次提交给服务器,例如图像,脚本,页面等等。您可以在OnValidateIdentity
中放置一个断点以查看此行为并查看Context.Request.Path
。
简而言之,虽然您的“启动”类根据AppDomain
被调用一次,但您的回调代表像OnValidateIdentity
将根据http请求触发一次。
public void ConfigureAuth(IAppBuilder app)
{
// app.UseCookieAuthentication(...) is only called once per AppDomain
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// callback delegate is called once per http request
OnValidateIdentity = ctx =>
{
return ctx.RejectIdentity(); // Reject every identity. No one can log into my app! It's that secure.
return Task.FromResult<object>(null);
}
}
});
}
谢谢,但它仍然是不明确的“应用程序”是否指的是全球应用程序在所有浏览器的服务器级别,'或',应用为每个HTTP请求看到它。 –
它指向您的应用程序,它将由服务器启动,并将服务多个Http请求。启动类被调用来启动应用程序,而不是每个Http请求。 –