问题详情: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 概念的具体体现。