【问题标题】:How to self-host ASP.NET 5 MVC6 application如何自托管 ASP.NET 5 MVC6 应用程序
【发布时间】:2016-02-11 04:07:25
【问题描述】:

刚开始学习 ASP.NET 5 / MVC 6 我很好奇在 IIS 之外自托管这样的应用程序 - 作为 Windows 服务。 我应该为此使用 TopShelf,就像使用 OWIN/Katana 应用程序一样,还是 ASP.NET 5 通过 NuGet 包提供一些内置的自托管(即服务)选项?

【问题讨论】:

    标签: asp.net-core-mvc


    【解决方案1】:

    您可以使用Kestrel 库进行自托管。 在project.json文件中为库添加依赖:

    "dependencies": {
        "EntityFramework.Commands": "7.0.0-rc1-final",
        // Dependencies deleted for brevity.
        "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final"
    }
    

    然后为 Kestrel 指定此命令:

    "commands": {
        "web": "Microsoft.AspNet.Server.Kestrel"
    }
    

    您可以通过命令行从包含 MVC 项目的文件夹中启动它:

    dnx web
    

    请注意,dnvm 必须先运行。

    【讨论】:

    • dnx 会作为 Windows 后台服务运行吗?
    • 不,它将作为 Windows 进程运行。
    • 那么,这个进程会在启动它的控制台窗口关闭后终止,还是会继续在后台运行?
    • 例如,您可以在 Windows Sheduler 中指定新任务。然后它将在后台模式下运行。或者使用第三方工具将进程作为服务运行。
    • 本示例使用 owin 并托管在 windows 服务中 - blog.uship.com/shippingcode/…
    【解决方案2】:

    所有 ASP.NET Core 应用程序都是自托管的。

    是的,你没看错!

    public class Program
    {
        public static void Main(string[] args)
        {
            var config = new ConfigurationBuilder()
                .AddCommandLine(args)
                .AddEnvironmentVariables(prefix: "ASPNETCORE_")
                .Build();
    
            var host = new WebHostBuilder()
                .UseConfiguration(config)
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseIISIntegration() //// Here IIS integration is optional
                .UseStartup()
                .Build();
    
            host.Run();
        }
    }
    

    查看here了解更多详情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-02-11
      • 1970-01-01
      • 1970-01-01
      • 2015-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多