如何更改已发布 Blazor 服务器项目的端口?

时间:2021-05-19 20:29:03

标签: port blazor kestrel

我发布了我的 Blazor 项目。然后当我通过“dotnet myapp.dll”从命令行运行它时,它给出了这个错误:

crit: Microsoft.AspNetCore.Server.Kestrel[0]
Unable to start Kestrel. System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use.

我已经更改了 launchsettings.json 中的端口,但这没有任何效果。它仍然使用端口5000。我在哪里可以更改已编译的blazor项目的端口?

1 个答案:

答案 0 :(得分:1)

您需要在 Program.cs 中添加如下所示的 UseUrls() 方法。在本例中,我选择了端口 8700。

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseStartup<Startup>();
            webBuilder.UseStaticWebAssets();
            webBuilder.UseUrls("http://localhost:8700");
        });

然后当我从命令行运行 'dotnet myapp.dll' 时,我可以看到这样的 Kestrel 输出:

info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:8700
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]