asp.net - 如何将简单的 Dotnet 应用程序发布到 Ubuntu?

标签 asp.net ubuntu .net-core

我有一个 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/"
第二种方式,添加Urlsappsettings.jsonappsettings.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/

相关文章:

linux - 来自emacs的Ubuntu Linux python绘图,无法从 '_imaging'导入名称 'PIL'

c# - 导出到 Excel 电子表格 (XLSX) 失败

c# - 中继器内的按钮 Onclick

c# - 使用 Request.Files 并在不重新加载页面的情况下从 POST 更改为 GET?

c# - DateTime.UtcNow.ToString ("o"上的构建错误)

asp.net-core - 如何根据环境更改 appSettings.json 文件中的值

javascript - 在 highcharts 中显示 .Net Core Web API 响应

c# - 在 asp :LinkButton been clicked 时调用 JavaScript 函数

python - 在 Ubuntu/Quckly 中为命令行服务器提供基本 GUI 的一般布局和架构?

php - 本地主机/phpinfo.php