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


Owin Startup 類解析

每個 Owin 程序都有 startup 類,在這個 startup 類里面你可以指定應用程序管道模型中的組件。你可以通過不同的方式來連接你的 startup 類和運行時,這些取決於你選擇的宿主模型(OwinHost, IIS, and IIS-Express)。

你可以通過下面幾種方式來連接你的 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 代碼

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 類,可以通過瀏覽器看到結果。

 

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

<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 里面加入下面的代碼

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

 

指定 Owin startup 類的 Configuration 方法

<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 運行以后可以看到結果

 

web.config 配置文件里有多個 owin:appStartup 值,那么會啟用最后一個配置 OwinDemo.Startup2 。

<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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM