.NET Core 2.1 + Identity as UI + windows service - 如何设置端口?

kof*_*fus 1 .net-core asp.net-core-2.0

背景:带着几分戏我的设法建立.NET核心2.1 +身份作为UI + windows服务(以下文章)是这样的:

  • 安装 .NET Core 2.1 Preview 2 SDK
  • 安装 Visual Studio 2017 预览版
  • dotnet 添加包 Microsoft.AspNetCore.Hosting.WindowsServices --version 2.1.0-rc1-final

  • 新建项目 -> '.NET Core' -> 'ASP.NET Core Web 应用程序' -> OK

  • 选择 ASP.NET Core 2.1(在顶部)-> Web 应用程序
    取消选择 'Configure for HTTPS'Change
    Authentication' -> 'Individual User Accounts' -> OK

  • 在 Program.cs 中:
    添加using Microsoft.AspNetCore.Hosting.WindowsServices;

    替换
    CreateWebHostBuilder(args).Build().Run();
    为:

    if (Debugger.IsAttached || args.Contains("--debug"))
    {
        CreateWebHostBuilder(args).Build().Run();
    }
    else
    {
        CreateWebHostBuilder(args).Build().RunAsService();
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 在 Startup.cs 中:
    备注app.UseHttpsRedirection();

  • 在launchSettings.json:
    替换:

    "MyProj": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }

    和:

    "MyProj": { "commandName": "Project", "launchBrowser": true, "applicationUrl": "https://localhost:44322;http://localhost:44322", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Production" } }

(我认为这就是全部)

然后我重建解决方案并在项目目录中执行以下操作:

dotnet publish -c Release --self-contained -r win-x64
Run Code Online (Sandbox Code Playgroud)

我得到MyProj\bin\Release\netcoreapp2.1\win-x64\MyProj.exe了我作为服务安装并


在 Chrome 中运行它 然后我:

http://localhost:5000/- 有效 :)
http://localhost:44322/- 无效 :(


那是没有应用来自 launchSettings.json 的端口设置

如何让服务监听 http 44322 ?

谢谢!

Mar*_*ich 5

launchSettings.json仅用于由dotnet run或 Visual Studio解释的开发设置。

在生产中,您可以设置ASPNETCORE_URLS环境变量或使用MyProj.exe --urls "http://localhost:5020;https://localhost:5021".

或者,您也可以调用.UseUrls("…")Web 主机构建器:

WebHost.CreateDefaultBuilder(args)
            .UseUrls("…")
            .UseStartup<Startup>()
Run Code Online (Sandbox Code Playgroud)