【Owin 學習系列】2. Owin Startup 類解析

Owin Startup 類解析

每一個 Owin 程序都有 startup 類,在這個 startup 類裏面你能夠指定應用程序管道模型中的組件。你能夠經過不一樣的方式來鏈接你的 startup 類和運行時,這些取決於你選擇的宿主模型(OwinHost, IIS, and IIS-Express)。web

你能夠經過下面幾種方式來鏈接你的 startup 類和宿主程序。瀏覽器

  • 命名約定:Katana 會在 namespace 中查找一個叫 Startup 的類。
  • OwinStartup 特性:這是開發者最經常使用的一種方式,下面的特性將會設置 startup 類到 命名空間 OwinDemo 下面的 Startup 類。OwinStartup 特性會覆蓋命名約定。
[assembly: OwinStartup(typeof(OwinDemo.Startup))]
  • Configuration 文件中的 appSetting 元素,appSetting 元素會覆蓋命名約定和 OwinStartup 特性。你能夠有多個 startup 類 (每一個都使用 OwinStartup 特性) ,能夠用下面的配置文件來選擇使用哪個 startup 類。
<appSettings>  
  <add key="owin:appStartup" value="OwinDemo.Startup2" />
</appSettings>

startup.cs 代碼app

using System;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(OwinDemo.Startup))] namespace OwinDemo
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello, world.");
            });
        }
    }

    public class Startup2
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello, this is Owin startup class 2.");
            });
        }
    }
}

F5 運行之後會進入 startup2 類,能夠經過瀏覽器看到結果。ui

 

你也在配置文件中指定 startup 類的別名,同時也要在 OwinStartup 特性裏設定,而後就會根據別名和 OwinStartup 特性找到對應的 startup 類。this

<appSettings>  
  <add key="owin:appStartup" value="ProductionConfiguration" />       
</appSettings>
using System;
using Microsoft.Owin;
using Owin;

[assembly: OwinStartup("ProductionConfiguration", typeof(OwinDemo.Startup2))] namespace OwinDemo
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello, world.");
            });
        }
    }

    public class Startup2
    {
        public void Configuration(IAppBuilder app)
        {
            // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
            app.Run(context =>
            {
                context.Response.ContentType = "text/plain";
                return context.Response.WriteAsync("Hello, this is Owin startup class 2.");
            });
        }
    }
}

 

若是要關閉 OWIN startup 發現,那麼只須要在 appSetting 裏面加入下面的代碼spa

<add key="owin:AutomaticAppStartup " value="false" />

 

指定 Owin startup 類的 Configuration 方法code

<add key="owin:appStartup" value="OwinDemo.Startup2.ConfigurationTwo" />


public class Startup2 { public void Configuration(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello, this is Owin startup class 2."); }); } public void ConfigurationTwo(IAppBuilder app) { // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello, this is Owin startup class 2 and ConfigurationTwo."); }); } }

F5 運行之後能夠看到結果orm

 

web.config 配置文件裏有多個 owin:appStartup 值,那麼會啓用最後一個配置 OwinDemo.Startup2 。blog

<appSettings>
    <add key="owin:appStartup" value="OwinDemo.Startup2.ConfigurationTwo" />
    <add key="owin:appStartup" value="OwinDemo.Startup2" />
  </appSettings>

 

使用  Owinhost.exe

Nuget 裏安裝 OwinHost開發

導航到你的應用程序文件夾(包含了 web.config 的文件夾),而後運行 Owinhost.exe

..\packages\Owinhost<Version>\tools\Owinhost.exe

最後訪問 http://localhost:5000/ ,就能夠看到效果了。

 

也能夠經過指定 OwinHost.exe 後面的參數訪問不一樣的 startup 類

..\packages\OwinHost.3.1.0\tools\Owinhost.exe OwinDemo.Startup2.ConfigurationTwo

 

源代碼連接:

連接: http://pan.baidu.com/s/1bOfTRC 密碼: xfhk

 

參考連接:

https://docs.microsoft.com/zh-cn/aspnet/aspnet/overview/owin-and-katana/owin-startup-class-detection

相關文章
相關標籤/搜索