问题详情: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. js中函数的一些理论知识

      函数的一些理论知识 1. 函数:                执行一个明确的动作并提供一个返回值的独立代码块.同时函数也是javascript中的一级公民(就是函数和其它变量一样). 2.函数的 ...

  2. javascript 创建对象的7种模式

    使用字面量方式创建一个 student 对象: var student = function (){ name : "redjoy", age : 21, sex: women, ...

  3. JAligner的一个坑

    JAligner是一个集成多个罚分矩阵的蛋白质序列比对工具包,提供充足的API供开发人员调用. 但是,不可否认的是,它的结构写得不够规范.以前我是将它放在普通的Java项目里使用,没有问题.但是,今天 ...

  4. Hibernate4 实例

    一.hibernate需要的配置文件 首先hibernate中有两种xml文件. .cfg,xml文件负责配置连接数据库的信息.指定映射类.指定hbm映射文件. .hbm.xml文件负责配置持久化类和 ...

  5. centos上安装php运行环境

    可以参考,但我安装的过程不完全一样http://www.cnblogs.com/liulun/p/3535346.html 我先安装的apache,直接执行的yum -y install httpd ...

  6. C及C++中typedef的简单使用指南

    又是在学数据结构的时候,发现了之前学习的知识遗忘很多,在发现对C/C++中关键字typedef的理解还是没有到位后,我翻阅了学C++用到的课本,又问了度娘,也看了不少关于typedef用法的博客.于是 ...

  7. 平凡的KTV后台,不平凡的KTV数据

    之前就是说过“一个项目有很多重要的步骤以及功能”,那我们现在就来看看对于KTV项目来说:后台是处于什么样的重要作用! 首先就得了解KTV后台的一些功能了: 1.歌曲管理 .歌手管理 .设置资源路径 2 ...

  8. touchstart、touchmove、touchend 实现移动端上的触屏拖拽

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  9. Map Network Driver

    K: \\10.11.80.5\deptfiling-pubgz$ O: \\10.11.80.5\deptfiling-itdgz$

  10. Java实现文件在某个目录的检索

    package com.filetest; import java.io.File; import java.util.Date; import java.util.Scanner; public c ...