ASP.NET 5 / MVC 6 控制台托管应用程序

ASP.NET 5 / MVC 6 Console Hosted App

在 MVC5 中,我有一个控制台应用程序,它将使用 Microsoft.Owin.Hosting.WebApp.Start(...) 来托管一堆控制器,这些控制器将从放置在外部文件夹中的程序集动态加载,并且 运行通过 API 调用对它们进行一些自定义初始化。这样我就可以将参数传递给在 运行 时确定的初始化方法(并且不会像维护配置文件那样笨拙)。

在 MVC6 中,据我所知,自托管现在由 DNX 运行time 使用 Microsoft.AspNet.Hosting 完成,但这都是通过命令行完成的。有没有一种方法可以让我从 C# 控制台应用程序中自行托管,以便我可以保留此初始化架构?

Katana 的 WebApp 静态 class 已被 WebHostBuilder 取代,它提供了一种更加灵活的方法:https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs.

您可能已经在没有意识到的情况下使用了这个 API,因为当您在 project.json 中注册新的 Web 命令时,它是托管块使用的组件(例如 Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:54540) 和 运行 它使用 dnx (例如 dnx . web):

namespace Microsoft.AspNet.Hosting
{
    public class Program
    {
        private const string HostingIniFile = "Microsoft.AspNet.Hosting.ini";
        private const string ConfigFileKey = "config";

        private readonly IServiceProvider _serviceProvider;

        public Program(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public void Main(string[] args)
        {
            // Allow the location of the ini file to be specified via a --config command line arg
            var tempBuilder = new ConfigurationBuilder().AddCommandLine(args);
            var tempConfig = tempBuilder.Build();
            var configFilePath = tempConfig[ConfigFileKey] ?? HostingIniFile;

            var appBasePath = _serviceProvider.GetRequiredService<IApplicationEnvironment>().ApplicationBasePath;
            var builder = new ConfigurationBuilder(appBasePath);
            builder.AddIniFile(configFilePath, optional: true);
            builder.AddEnvironmentVariables();
            builder.AddCommandLine(args);
            var config = builder.Build();

            var host = new WebHostBuilder(_serviceProvider, config).Build();
            using (host.Start())
            {
                Console.WriteLine("Started");
                var appShutdownService = host.ApplicationServices.GetRequiredService<IApplicationShutdown>();
                Console.CancelKeyPress += (sender, eventArgs) =>
                {
                    appShutdownService.RequestShutdown();
                    // Don't terminate the process immediately, wait for the Main thread to exit gracefully.
                    eventArgs.Cancel = true;
                };
                appShutdownService.ShutdownRequested.WaitHandle.WaitOne();
            }
        }
    }
}

https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/Program.cs

...I had a console application that would use Microsoft.Owin.Hosting.WebApp.Start(...) to host [and to] pass parameters to the initialization method that were determined at runtime...

在 ASP.NET 4.x 中,我们使用 OWIN 主机在控制台应用程序中自行托管。我们 运行 我们 MyApp.exe 直接。它的 Main() 方法调用 WebApp.Start() 来创建 OWIN 主机。我们使用 IAppBuilder 的实例通过 appBuilder.Use() 构建 HTTP 管道,并将其与 appBuilder.Build() 链接在一起。这些都在 Microsoft.Owin.Hosting 命名空间内。

Is there a way I can self-host from within a C# console application so I can keep this initialization architecture?

在 ASP.NET Core rc2 中,我们使用 IWebHost 在控制台应用程序中自托管。 (虽然 OWIN 启发了它,但它不是 OWIN 主机。)我们直接 运行 我们的 MyApp.exeMain() 方法创建了一个新的 WebHostBuilder(),我们用它来通过 webHostBuilder.Use() 构建 HTTP 管道,并将其与 webHostBuilder.Build() 链接在一起。这都在 Microsoft.AspNet.Hosting 命名空间内。

关于 Pinpoint 的回答,在 ASP.NET Core rc1 我们需要 运行 dnx.exe 而不是 运行ning 我们的应用程序直接地。 WebHostBuilder 的工作隐藏在 dnx.exe 可执行文件中。 Dnx.exe 也启动了我们的应用程序。我们应用程序的 Main() 方法调用 WebApplication.Run(),之后我们使用 IApplicationBuilder 的实例通过调用 appBuilder.Use() 将中间件添加到 HTTP 管道。我们的应用程序和 dnx.exe 共同承担 creating/configuring 主机的责任。这很复杂,我很高兴这在 rc2 中发生了变化。我认为在 rc1 中 OWIN 的 WebApp.Start() 的等价物是 WebApplication.Run().

ASP.NET 4.x            ASP.NET Core rc1           ASP.NET Core rc2

N/A                    Dnx.exe                      N/A
MyApp.exe              MyApp.dll                    MyApp.exe
Main(args)             Main(args)                   Main(args)
WebApp.Start()         WebApplication.Run(args)     N/A   
appBuilder.Use()       appBuilder.Use()             webHostBuilder.Use()
appBuilder.Build()     N/A                          webHostBuilder.Build()

一些参考资料

http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

https://msdn.microsoft.com/en-us/library/microsoft.owin.hosting.webapp%28v=vs.113%29.aspx