我有一个 Asp 核心 dotnet 应用程序。该应用程序运行正常,我正在尝试将其发布到 Ubuntu。
我在 Ubuntu 上安装了 .Net。
这是我的应用程序的配置文件(LaunchSettings.json 文件):
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:49855",
"sslPort": 44315
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"MyApp": {
"commandName": "Project",
"launchBrowser": true,
"applicationUrl": "https://localhost:6001;http://localhost:6000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
基本上我希望我的应用程序在端口 6000/6001 上运行我成功地将应用程序复制到服务器,但是当我运行 dotnet MyApp.dll ,我收到错误:
Unable to start Kestrel. System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use.
我检查了哪个进程在端口 5000 上运行(通过命令
fuser 5000/tcp
然后 ps -p PID -o comm=
)。我将 dotnet 作为使用端口 5000 的进程。我将其杀死并再次运行命令 dotnet MyApp.dll
但我遇到了同样的错误。请问有什么办法可以解决吗?即让应用程序从另一个端口运行或让 dotnet 使用另一个端口?
最佳答案
在 asp.net 核心中,您可以使用 3 种方式
第一种方法,使用 --urls=[url]
dotnet run 的选项,
例如 :
dotnet run --urls="http://localhost:6000/;https://localhost:6001/"
或者dotnet MyApp.dll --urls="http://localhost:6000/;https://localhost:6001/"
第二种方式,添加Urls
在 appsettings.json
或 appsettings.Development.json
例如:{
"Urls": "http://localhost:6000;https://localhost:6001"
}
第三种方式,使用 UseUrls()
在 Program class
并将配置添加到 ConfigureWebHostDefaults
例如:public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.UseUrls("http://localhost:6000;https://localhost:6001");
});
}
关于asp.net - 如何将简单的 Dotnet 应用程序发布到 Ubuntu?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65639324/