【问题标题】:command line: execute ASP.Net Core dll with certain profile name命令行:使用特定配置文件名称执行 ASP.Net Core dll
【发布时间】:2020-02-26 12:22:50
【问题描述】:

当我使用dotnet WebApplicationLaunchProfile.dll 运行 ASP.Net Core 应用程序时,请帮我指定配置文件名称

我使用这个简短的表格只是为了有可能在 VS 之外运行项目并检查它是如何工作的。

我知道我可以使用dotnet run --launch-profile testProfileName,但是它需要.csproj.cs 源文件可用。我想避免复制源代码文件并使用已编译的程序集。我的流程是:

  • 使用dotnet build 构建
  • 将构建结果(bin\Debug 文件夹内容)复制到单独的文件夹中
  • 使用 dotnet WebApplicationLaunchProfile.dll 表单启动 dll 以使用正确的配置文件名称

但问题是

当应用程序通过 dotnet run 启动时,使用第一个带有 "commandName": "Project" 的配置文件

如果项目使用dotnet dll 命令执行,则规则from the documentation is not applied。显然不应该应用它,因为我没有使用dotnet run 命令。

但是我想以某种方式指定个人资料名称。

我已经从 VS 模板创建了新的 ASP.Net Core 项目并更新了 launchSettings.json 文件如下:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
...
  "profiles": {
...
    "WebApplicationLaunchProfile": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "weatherforecast",
      "applicationUrl": "https://localhost:4430;http://localhost:8080",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

我已经调整了profiles:WebApplicationLaunchProfile:applicationUrl 值并替换了 5000 和 5001 端口,如您所见。 项目已上传至https://github.com/oleksabor/curly-broccoli,仅供快速参考。 但是除了launchSettings.json之外没有任何修改。

我可以使用VS或dotnet run命令启动应用程序没有任何问题

但是,当使用dotnet dll 命令执行应用程序时出现错误,如下所示:

C:\projects\WebApplicationLaunchProfile\bin\Debug\netcoreapp3.1>dotnet WebApplicationLaunchProfile.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.  

我占用了 5000 端口,想使用 4430 代替。

dotnet WebApplicationLaunchProfile.dll --launch-profile WebApplicationLaunchProfile不起作用(错误相同)

是否可以使用dotnet dll 设置配置文件名称?

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    我原以为dotnet xxx.dll -launch-profile 会起作用。您是否使用了默认的 Program.cs 文件? (当我们从模板创建项目时生成的,使用 Host.CreateDefaultBuilder(args).ConfigureWebHostDefaults(...),这就是配置魔法发生的地方)

    否则,您可以尝试为您的进程设置此环境变量:ASPNETCORE_URLS。它应该被拿起和使用。更多信息请访问 github 问题页面github.com/aspnet/KestrelHttpServer/issues/639。例如:set ASPNETCORE_URLS=https://localhost:4430;http://localhost:8080

    【讨论】:

    • ASPNETCORE_URLS 有效,但没有双引号,例如 set ASPNETCORE_URLS=https://localhost:4430;http://localhost:8080。但是,使用配置文件名称,我不仅可以设置 url,还可以设置 environmentVariables,所以应用配置文件会很好
    • --launch-profile
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2018-08-18
    • 2017-04-07
    • 2017-07-11
    • 2013-12-18
    相关资源
    最近更新 更多