文章出處

問題詳情: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 概念的具體體現。


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


全站熱搜
創作者介紹
創作者 大師兄 的頭像
大師兄

IT工程師數位筆記本

大師兄 發表在 痞客邦 留言(0) 人氣()