问题描述
在 MVC5 中,我有一个控制台应用程序,它将使用 Microsoft.Owin.Hosting.WebApp.Start(...) 来托管一堆控制器,这些控制器将从放置在外部文件夹中的程序集中动态加载并运行一些自定义通过 API 调用对它们进行初始化.这样我可以将参数传递给在运行时确定的初始化方法(并且不会像维护配置文件那样笨拙).
In MVC5, I had a console application that would use Microsoft.Owin.Hosting.WebApp.Start(...) to host a bunch of controllers that would be dynamically loaded from assemblies placed in an external folder and run some custom initialization on them via API call. This way I could pass parameters to the initialization method that were determined at runtime (and would not be as clunky as maintaining config files).
据我所知,在 MVC6 中,自托管现在是由 DNX 运行时使用 Microsoft.AspNet.Hosting 完成的,但这一切都是通过命令行完成的.有没有一种方法可以让我在 C# 控制台应用程序中自托管,这样我就可以保留这个初始化架构?
In MVC6, the self-hosting is now done, as far as I know, by the DNX runtime using Microsoft.AspNet.Hosting, but this is all done via command line. Is there a way I can self-host from within a C# console application so I can keep this initialization architecture?
推荐答案
Katana 的 WebApp
静态类已被替换为 WebHostBuilder
,提供了更灵活的方法:https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/WebHostBuilder.cs.
Katana's WebApp
static class has been replaced by WebHostBuilder
, that offers a much more flexible approach: 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
)运行它:
You've probably already used this API without realizing it, as it's the component used by the hosting block when you register a new web command in your project.json (e.g Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:54540
) and run it using dnx
(e.g 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
这篇关于ASP.NET 5/MVC 6 控制台托管应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!