首页 文章

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

提问于
浏览
3

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

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

2 回答

  • 5

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

    您在project.json(例如 Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:54540 )中注册新的Web命令并使用 dnx (例如 dnx . web )运行它时,托管块使用的组件是've probably already used this API without realizing it, as it':

    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

  • 5

    ...我有一个控制台应用程序,它将使用Microsoft.Owin.Hosting.WebApp.Start(...)来托管[并]将参数传递给在运行时确定的初始化方法...

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

    有没有办法可以在C#控制台应用程序中自我托管,这样我就可以保留这种初始化架构?

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

    关于Pinpoint的答案, in ASP.NET Core rc1 我们需要运行 dnx.exe 而不是直接运行我们的应用程序 . WebHostBuilder 的工作隐藏在 dnx.exe 可执行文件中 . Dnx.exe 也启动了我们的应用程序 . 我们的应用程序的 Main() 方法调用 WebApplication.Run() ,之后我们使用 IApplicationBuilder 的实例通过调用 appBuilder.Use() 将中间件添加到HTTP管道 . 我们的应用程序和 dnx.exe 共同负责创建/配置主机 . 它's convoluted and I am glad that this changed in rc2. I supposed that in rc1 the equivalent of OWIN' s 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

相关问题