OWIN如何挂钩ASP.NET启动 [英] How OWIN hooks on ASP.NET startup

查看:182
本文介绍了OWIN如何挂钩ASP.NET启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览和谷歌搜索OWIN之类的东西如何挂钩ASP.NET活动。

I've been browsing and googling about how did things like OWIN hooks on the ASP.NET activities.

我知道如果我们创建一个自托管的OWIN,我们将手动调用它,显然我们正在调用OWIN使其开始运行。

I know if we create a self hosted OWIN, we will call it manually and it is clear that we are calling OWIN to make it start running.

但是,我发现当ASP.NET启动时没有自动调用OWIN需要调用webapp start或其他任何东西。 OWIN只是挂钩进入ASP.NET并在每个请求上充当拦截器。

However, I see that OWIN is automatically invoked when ASP.NET started without the need of calling webapp start or anything else. OWIN just hook into ASP.NET and act as an interceptor on each request.

我的例子是signalr,我们在OWIN配置方法中调用signalr的映射。但是,我没有看到任何可能调用OWIN配置方法的东西。但是signalr已经映射并且可以工作。

My example would be signalr, we call the mapping of signalr in OWIN configuration method. However, I dont see anything that could possibly call OWIN configuration method. But signalr is already mapped and works.

OWIN如何挂钩ASP.NET活动?注册OWIN并自动调用的挂钩或ASP.NET是OWIN吗?

How did OWIN hooks on ASP.NET activities? Is it OWIN who register the hooks or ASP.NET who now recognize OWIN and call automatically?

推荐答案

你会有一个类似的行在您的项目上这样:

You will have a similar line like this on your project:

[assembly: OwinStartup(typeof(MyApp.Security.Authentication.Startup))]

上面的行告诉.NET将在开头调用的类和方法。

The line above informs .NET the class and method that will be invoked at the beginning.

您可以在WebConfig中配置启动的替代方案

Alternative you can configure the startup in WebConfig

<appSettings>
...
<add key="owin:appStartup" value="MyApp.Security.Authentication.Startup" />
...
</appSettings>

从那时起,您可以放置​​OWIN组件,以及通常放置的所有配置项在Global.asax Application_Start事件处理程序中。

From that point, you can place OWIN components, as well as all the configuration items you would normally place in the Global.asax Application_Start event handler.

删除Global.asax类:
如果使用OWIN,则无需使用Gobal.asax类并启动Application_Start事件,以便删除它。

Delete Global.asax Class: If you use OWIN, there's no need to use Gobal.asax class and fire up the Application_Start event, so you can delete it.

Startup.cs的代码示例

Code example of Startup.cs

using System.Web.Http;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(MyApp.Security.Authentication.Startup))]
namespace MyApp.Security.Authentication
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            HttpConfiguration config = new HttpConfiguration();
            WebApiConfig.Register(config);
            app.UseWebApi(config);
        }
    }
}

编辑:

OWIN使用启动类,您可以在其中指定要包含在应用程序管道中的组件。如果你看一下 Katana源代码,Katana SystemWeb主机使用 PreApplicationStartMethodAttribute 来挂钩进入应用程序启动。
PreApplicationStartMethodAttribute,在.NET 4中引入,允许您在应用程序启动时尽早在ASP.NET管道中运行代码。我的意思是提前,甚至在Application_Start之前。

OWIN uses a startup class where you can specify the components you wish to include in the application pipeline. If you look at the Katana source code, the Katana SystemWeb host uses the PreApplicationStartMethodAttribute to hook into the application startup. PreApplicationStartMethodAttribute, wich is introduced in .NET 4, allows you to have code run way early in the ASP.NET pipeline as an application starts up. I mean way early, even before Application_Start.

检查此 link 链接

这篇关于OWIN如何挂钩ASP.NET启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆