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.\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

【Owin 学习系列】2. Owin Startup 类解析的更多相关文章

  1. 【Owin 学习系列】1. 第一个 Owin 程序

    IIS 中的 Owin 在 IIS 里面部署 Owin,既能得到 Owin 管道模型的灵活性和模块特性,也能很好地利用 IIS 成熟的配置,Owin 程序将会跑在 ASP.NET request 的管 ...

  2. Java并发包源码学习系列:ReentrantReadWriteLock读写锁解析

    目录 ReadWriteLock读写锁概述 读写锁案例 ReentrantReadWriteLock架构总览 Sync重要字段及内部类表示 写锁的获取 void lock() boolean writ ...

  3. asp.net core 系列 2 启动Startup类介绍

    一.Startup类 ASP.NET Core 应用是一个控制台应用,它在其 Program.Main 方法中创建 Web 服务器.其中Main方法是应用的托管入口点,Main 方法调用 WebHos ...

  4. Android学习系列(20)--App数据格式之解析Json

    JSON数据格式,在Android中被广泛运用于客户端和网络(或者说服务器)通信,非常有必要系统的了解学习.     恰逢本人最近对json做了一个简单的学习,特此总结一下,以飨各位.     为了文 ...

  5. 【SpringCloud微服务实战学习系列】创建应用及解析

    一.创建应用 使用官方Spring Initializr工具生成基础项目(http://start.spring.io/) 导入Intellij idea中 目录结构如下: 二.目录结构说明: src ...

  6. 【Spring源码深度解析学习系列】复杂标签属性解析(四)

    一.创建用于属性承载的BeanDefinition BeanDefiniton是一个接口,在Spring中存在三种实现:RootBeanDefinition.ChildBeanDefinition.G ...

  7. CobaltStrike逆向学习系列(3):Beacon C2Profile 解析

    这是[信安成长计划]的第 3 篇文章 关注微信公众号[信安成长计划] 0x00 目录 0x01 Controller 端分析 0x02 Beacon 端分析 0x03 展示图 在上一篇文章中完成了 S ...

  8. Caffe学习系列(10):命令行解析

    训练网络命令: sudo sh ./build/tools/caffe train --solver=examples/mnist/train_lenet.sh 用预先训练好的权重来fine-tuni ...

  9. Java并发包源码学习系列:挂起与唤醒线程LockSupport工具类

    目录 LockSupport概述 park与unpark相关方法 中断演示 blocker的作用 测试无blocker 测试带blocker JDK提供的demo 总结 参考阅读 系列传送门: Jav ...

随机推荐

  1. js基础回顾-数据类型和typeof怎么用

    js的基本数据类型有六种,undefined.null.number.string.boolean.object. 未定义        空      数字        字符串    布尔     ...

  2. bootstrap中的下拉菜单

    下拉菜单必要的代码: <div  class="container"> <div  class="dropdown"> <butt ...

  3. asp.net mvc中html helper的一大优势

    刚上手这个框架,发现其中的html helper用起来很方便,让我们这些从web form 过渡来的coder有一种使用控件的快感,嘻嘻! 言归正传,我要说的是在使用它时,系统会自动执行表单的现场恢复 ...

  4. 编写自己的Nmap(NSE)脚本

    编写自己的Nmap脚本 一.介绍 在上一篇文章Nmap脚本引擎原理中我们介绍了基本的NSE知识,这篇文章介绍如何基于Nmap框架编写简单的NSE脚本文件,下一篇文章,Nmap脚本文件分析(AMQP协议 ...

  5. 如何用phpcms将静态网页生成动态网页?

    在前两篇随笔中已经简单介绍了phpcms,那么现在让我们来看一下如何用phpcms将静态网页生成动态网页? 1.在templates文件夹下新建模板文件夹ceshi(名字可以自己随笔起) 2.在ces ...

  6. Docker Hub工作流程-Docker for Web Developers(6)

    在Github上创建项目仓库 和创建其他Github项目一样,在Github创建一个仓库,然后在仓库里面增加一个dockerfile,然后提交并推送到Github上. 我已经创建的仓库地址:https ...

  7. noip模拟 市长选举

    题目描述 利贝尔王国的卢安市因为前段时间的市长被捕事件,导致没有市长管理城市.他们需要一个新的市长. 竞选的人有两位.一位是诺曼,因支持旅游业而受到支持者的拥护.一位是波尔多斯,代表的是卢安的传统行业 ...

  8. EF架构~codeFirst从初始化到数据库迁移

    一些介绍 CodeFirst是EntityFrameworks的一种开发模式,即代码优先,它以业务代码为主,通过代码来生成数据库,并且加上migration的强大数据表比对功能来生成数据库版本,让程序 ...

  9. javascript 闭包 转载

    http://www.jb51.net/article/24101.htm var name = "the window"; var object = { name:"m ...

  10. DocNan博文目录

    算法 时频分析:窗口傅立叶变换 数学误区:乘积的求和 MHD simulation with python Linux Linux: Bash基本命令 Linux: 安装和启用firefox浏览器的j ...