我发布了我的 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 项目的端口?
最佳答案
您需要在 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]
关于port - 如何更改已发布的 Blazor Server 项目的端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67610456/