问题详情:K Commands(OwinHost.exe)是不是 OWIN 中的 Host 角色?如果是,那 Microsoft.AspNet.Hosting 对应的是 OWIN 中的哪个角色?

OWIN 中,除了 Host 和 Server 的概念容易混淆,K Commands(OwinHost.exe)与 Microsoft.AspNet.Hosting 也是很容易混淆的一点,先看一下它们的概念:

  • OwinHost.exe: While some will want to write a custom process to run Katana Web applications, many would prefer to simply launch a pre-built executable that can start a server and run their application. For this scenario, the Katana component suite includes OwinHost.exe. When run from within a project’s root directory, this executable will start a server (it uses the HttpListener server by default) and use conventions to find and run the user’s startup class. For more granular control, the executable provides a number of additional command line parameters.
  • K Commands: Whenever you want to run your app in command line using K* commands, you will use k run. The K command is your entry point to the runtime. To run an application you can use K run to build you can use K build, and all other commands that are about taking your application and running it.
  • Microsoft.AspNet.Hosting: The Hosting repo contains code required to host an ASP.NET vNext application, it is the entry point used when self-hosting an application.

上面是从网上各个地方搜刮的概念,再来结合 Microsoft.AspNet.Hosting/Program.cs 的源码:

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Framework.ConfigurationModel;
using Microsoft.Framework.DependencyInjection;
using Microsoft.Framework.DependencyInjection.Fallback;
using Microsoft.Framework.Logging;
using Microsoft.Framework.Runtime; namespace Microsoft.AspNet.Hosting
{
public class Program
{
private const string HostingIniFile = "Microsoft.AspNet.Hosting.ini";
private readonly IServiceProvider _serviceProvider; public Program(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
} public void Main(string[] args)
{
var config = new Configuration();
if (File.Exists(HostingIniFile))
{
config.AddIniFile(HostingIniFile);
}
config.AddEnvironmentVariables();
config.AddCommandLine(args); var services = HostingServices.Create(_serviceProvider, config)
.BuildServiceProvider();
var appEnv = services.GetRequiredService<IApplicationEnvironment>();
var hostingEnv = services.GetRequiredService<IHostingEnvironment>(); var context = new HostingContext()
{
Services = services,
Configuration = config,
ServerName = config.Get("server"), // TODO: Key names
ApplicationName = config.Get("app") // TODO: Key names
?? appEnv.ApplicationName,
EnvironmentName = hostingEnv.EnvironmentName,
}; var engine = services.GetRequiredService<IHostingEngine>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
var appShutdownService = _serviceProvider.GetRequiredService<IApplicationShutdown>();
var shutdownHandle = new ManualResetEvent(false);
var serverShutdown = engine.Start(context); appShutdownService.ShutdownRequested.Register(() =>
{
try
{
serverShutdown.Dispose();
}
catch (Exception ex)
{
var logger = loggerFactory.Create<Program>();
logger.WriteError("TODO: Dispose threw an exception", ex);
}
shutdownHandle.Set();
}); var ignored = Task.Run(() =>
{
Console.WriteLine("Started");
Console.ReadLine();
appShutdownService.RequestShutdown();
}); shutdownHandle.WaitOne();
}
}
}

K Commands(OwinHost.exe)的作用就是启动并加载 OWIN 组件,使你的应用程序处于运行状态,看上面 Program.cs 代码,就会发现 Microsoft.AspNet.Hosting 其实就是一个控制台项目,当然除此之外还会包含 Builder、Server、Startup 等一些操作,这些构成了基本的 OWIN Host,它是一个进程,负责启动并加载 OWIN 组件(在之前的博文中有说明),而 K Commands(OwinHost.exe)只不过是一个命令,用来去开启它,就像一个车钥匙,用来发动汽车一样。结合 IIS 的一些东西,K Commands(OwinHost.exe)就像我们点击“启动”、“停止” 的后台处理命令,当然还有一些 URL 绑定等,这些都通过命令去加载,过程大概是这样:K Commands -> project.json -> Microsoft.AspNet.Hosting -> Started。

在之前曾说过,如果采用 IIS 的部署,那 ASP.NET 5 的 project.json 配置,就是下面这么简单:

{
"webroot": "wwwroot",
"dependencies": {
"Microsoft.AspNet.Server.IIS": "1.0.0-beta1"
}
}

你会发现,没有了 Microsoft.AspNet.Hosting、Microsoft.AspNet.Server.WebListener,也没有了 Commands,为什么呢?因为 IIS 既是 Host,又是 Server,IIS 与 ASP.NET 5 的 OWIN 管道处理,是通过 Microsoft.AspNet.Server.IIS(AspNet.Loader.dll)打通的,所以 OWIN 的处理组件都是通过 IIS,这时候的 ASP.NET 5 其实就不是纯粹的 OWIN。

回答上面的问题,K Commands(OwinHost.exe)不是 OWIN 中的 Host 角色,Microsoft.AspNet.Hosting 才是,应该准确的说,K Commands(OwinHost.exe)和 OWIN 中的 Host 不存在概念问题,它只是一个命令,用来开启 Microsoft.AspNet.Hosting,Microsoft.AspNet.Hosting 是 OWIN 中 Host 概念的具体体现。

OWIN 中 K Commands(OwinHost.exe)与 Microsoft.AspNet.Hosting 的角色问题的更多相关文章

  1. OWIN 中 K Commands 与 OwinHost.exe 相等吗?

    OwinHost.exe: While some will want to write a custom process to run Katana Web applications, many wo ...

  2. 深入理解 OWIN 中的 Host 和 Server

    The Open Web Interface for .NET (OWIN),注意单词为大写,之前好像都写成了 Owin,但用于项目的时候,可以写成:Microsoft.Owin.*. OWIN 体系 ...

  3. Encountered an unexpected error when attempting to resolve tag helper directive '@addTagHelper' with value '"*, Microsoft.AspNet.Mvc.TagHelpers"'

    project.json 配置: { "version": "1.0.0-*", "compilationOptions": { " ...

  4. 从Microsoft.AspNet.Identity看微软推荐的一种MVC的分层架构

    Microsoft.AspNet.Identity简介 Microsoft.AspNet.Identity是微软在MVC 5.0中新引入的一种membership框架,和之前ASP.NET传统的mem ...

  5. OwinHost.exe用法

    简介 OwinHost.exe是微软提供的自宿主host,如果自己不想写owin的host,可以用这个. 官方对OwinHost的描述为:Provides a stand-alone executab ...

  6. 命名空间“Microsoft.AspNet”中不存在类型或命名空间名“Mvc”

    问题: 错误 CS0234 命名空间"Microsoft.AspNet"中不存在类型或命名空间名"Mvc"(是否缺少程序集引用?) 解决方案: 打开文件夹 Us ...

  7. 机器学习中 K近邻法(knn)与k-means的区别

    简介 K近邻法(knn)是一种基本的分类与回归方法.k-means是一种简单而有效的聚类方法.虽然两者用途不同.解决的问题不同,但是在算法上有很多相似性,于是将二者放在一起,这样能够更好地对比二者的异 ...

  8. 排错技能:任务管理器中追踪某w3wp.exe是哪个IIS站点的application pool

    如果Windows的任务管理器中发现某个w3wp.exe占用了100%CPU,那我们就要揪出这是那个网站的application pool在作怪, 首先,每个站点一定要单独使用各自的applicati ...

  9. 如何在Qt资源文件中包含和释放exe等各种类型文件?

    操作系统:Windows 10 X64 企业版 Qt: 5.8.0 QtCreater: 4.2.1 刚刚开始学习Qt,不断遇到困难和挑战,前几天在各个QQ群里询问如何在Qt的资源文件中包含和释放ex ...

随机推荐

  1. dd——留言板再加验证码功能

    1.找到后台-核心-频道模型-自定义表单 2.然后点击增加新的自定义表单 diyid 这个,不管他,默认就好 自定义表单名称 这个的话,比如你要加个留言板还是投诉建议?写上去呗 数据表  这个不要碰, ...

  2. 使用 SWFObject.js 插入Flash

    今天学习了一下使用 SWFObject.js 这个js插入flash. 下载地址:http://code.google.com/p/swfobject/ 这个js的好处 1.IE中没有讨厌的虚框问题了 ...

  3. iOS单元格高度计算

    // Created by mac on 16/6/29. // Copyright © 2016年 zcc. All rights reserved. // #import "HotCel ...

  4. Unity3D 与android交互流程步骤

    1.Android端代码可以在Eclipse中开发(AndroidStudio没有试,应该也可以) 2.Unity3D端代码要在Unity中开发 3.Android和Unity3D端,两边都需要加入一 ...

  5. Palm是一家英國智能手機公司

    據TCL方面介紹,本次收購只涉及品牌,不會涉及員工和其他資產.被收購之後,Palm仍將繼續把總部設於美國加州矽谷,以發揮該區域所獨有的先進技術和人才的優勢. TCL通訊CEO郭愛平表示TCL將把Pal ...

  6. jvm的内存分配总结

    最近看了周志明版本的<深入理解Java虚拟机>第一版和第二版,写的很好,收获很多,此处总结一下.   jvm中内存划分:   如上图,一共分为五块,其中: 线程共享区域为: 1.java堆 ...

  7. 5.Powershell变量

    在指令执行过程中,会有一些数据产生,这些数据被用于以后的语句,需要一个存储单元暂时的存放这些数据,这个时候定义一个变量来存储数据.例如$string = “Hello Powershell!” Pow ...

  8. Jmeter之参数化

    Jmeter参数化分为两类,一类是在badboy录制脚本时进行参数化,二是再Jmeter里进行参数化 一:badboy录制脚本时进行参数化的步骤 1.脚本录制成功后->在左下角,点击variab ...

  9. SQL Server差异备份的备份/还原原理

    SQL Server差异备份的备份/还原原理 记住一点:差异备份是基于最后一次完整备份的差异,而不是基于最后一次差异的差异   备份过程: 1-完整备份之后有无对数据库做过修改,如果有,记录数据库的最 ...

  10. 【直播】APP全量混淆和瘦身技术揭秘

    [直播]APP全量混淆和瘦身技术揭秘 近些年来移动APP数量呈现爆炸式的增长,黑产也从原来的PC端转移到了移动端,通过逆向手段造成数据泄漏.源码被盗.APP被山寨.破解后注入病毒或广告现象让用户苦不堪 ...