【问题标题】:.NET Core 2.1 + Identity as UI + windows service - how to set port?.NET Core 2.1 + Identity as UI + windows service - 如何设置端口?
【发布时间】:2018-05-29 05:01:51
【问题描述】:

背景:经过一番尝试,我设法将 .NET Core 2.1 + 身份设置为 UI + windows 服务(在this 文章之后),如下所示:

  • 安装 .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 应用程序' -> 确定

  • 选择 ASP.NET Core 2.1(在顶部)-> Web 应用程序
    取消选择“配置 HTTPS
    '更改身份验证' -> '个人用户帐户' -> 确定

  • 在 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();
    }
    
  • 在 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

我得到MyProj\bin\Release\netcoreapp2.1\win-x64\MyProj.exe,我将其作为服务安装并运行它


然后在 Chrome 中:

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


那就是未应用 launchSettings.json 中的端口设置

如何让服务在 http 44322 上侦听?

谢谢!

【问题讨论】:

    标签: .net-core asp.net-core-2.0


    【解决方案1】:

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

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

    或者,您也可以在网络主机生成器上调用.UseUrls("…")

    WebHost.CreateDefaultBuilder(args)
                .UseUrls("…")
                .UseStartup<Startup>()
    

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 1970-01-01
      • 2018-11-18
      • 2019-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-11
      相关资源
      最近更新 更多