<legend id='Uu4Gy'><style id='Uu4Gy'><dir id='Uu4Gy'><q id='Uu4Gy'></q></dir></style></legend>

    1. <i id='Uu4Gy'><tr id='Uu4Gy'><dt id='Uu4Gy'><q id='Uu4Gy'><span id='Uu4Gy'><b id='Uu4Gy'><form id='Uu4Gy'><ins id='Uu4Gy'></ins><ul id='Uu4Gy'></ul><sub id='Uu4Gy'></sub></form><legend id='Uu4Gy'></legend><bdo id='Uu4Gy'><pre id='Uu4Gy'><center id='Uu4Gy'></center></pre></bdo></b><th id='Uu4Gy'></th></span></q></dt></tr></i><div id='Uu4Gy'><tfoot id='Uu4Gy'></tfoot><dl id='Uu4Gy'><fieldset id='Uu4Gy'></fieldset></dl></div>

      <small id='Uu4Gy'></small><noframes id='Uu4Gy'>

        <tfoot id='Uu4Gy'></tfoot>
          <bdo id='Uu4Gy'></bdo><ul id='Uu4Gy'></ul>

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

        ASP.NET 5 / MVC 6 Console Hosted App(ASP.NET 5/MVC 6 控制台托管应用程序)

        <small id='88uPI'></small><noframes id='88uPI'>

        <tfoot id='88uPI'></tfoot>

        <legend id='88uPI'><style id='88uPI'><dir id='88uPI'><q id='88uPI'></q></dir></style></legend>
        • <bdo id='88uPI'></bdo><ul id='88uPI'></ul>
            <tbody id='88uPI'></tbody>

              <i id='88uPI'><tr id='88uPI'><dt id='88uPI'><q id='88uPI'><span id='88uPI'><b id='88uPI'><form id='88uPI'><ins id='88uPI'></ins><ul id='88uPI'></ul><sub id='88uPI'></sub></form><legend id='88uPI'></legend><bdo id='88uPI'><pre id='88uPI'><center id='88uPI'></center></pre></bdo></b><th id='88uPI'></th></span></q></dt></tr></i><div id='88uPI'><tfoot id='88uPI'></tfoot><dl id='88uPI'><fieldset id='88uPI'></fieldset></dl></div>

                  本文介绍了ASP.NET 5/MVC 6 控制台托管应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 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 控制台托管应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Performance overhead of using attributes in .NET(在 .NET 中使用属性的性能开销)
                  Accessing attribute info from DTE(从 DTE 访问属性信息)
                  c# Hide a property in datagridview with datasource(c#使用数据源隐藏datagridview中的属性)
                  Extract Display name and description Attribute from within a HTML helper(从 HTML 帮助器中提取显示名称和描述属性)
                  C# Attributes and their uses(C# 属性及其用途)
                  C# - Getting all enums value by attribute(C# - 按属性获取所有枚举值)
                      <bdo id='HFKu8'></bdo><ul id='HFKu8'></ul>
                      <tfoot id='HFKu8'></tfoot>

                      1. <legend id='HFKu8'><style id='HFKu8'><dir id='HFKu8'><q id='HFKu8'></q></dir></style></legend>
                            <tbody id='HFKu8'></tbody>
                            <i id='HFKu8'><tr id='HFKu8'><dt id='HFKu8'><q id='HFKu8'><span id='HFKu8'><b id='HFKu8'><form id='HFKu8'><ins id='HFKu8'></ins><ul id='HFKu8'></ul><sub id='HFKu8'></sub></form><legend id='HFKu8'></legend><bdo id='HFKu8'><pre id='HFKu8'><center id='HFKu8'></center></pre></bdo></b><th id='HFKu8'></th></span></q></dt></tr></i><div id='HFKu8'><tfoot id='HFKu8'></tfoot><dl id='HFKu8'><fieldset id='HFKu8'></fieldset></dl></div>
                          • <small id='HFKu8'></small><noframes id='HFKu8'>