【Owin 学习系列】2. Owin Startup 类解析

来源:互联网 发布:淘宝网络科技有限公司 编辑:程序博客网 时间:2024/05/18 09:10

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

原创粉丝点击