c# - 命令行 : execute ASP. 具有特定配置文件名称的 Net Core dll

标签 c# asp.net-core

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

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

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

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

问题是

When the app is launched with dotnet run, the first profile with "commandName": "Project" is used

规则 from the documentation is not applied如果使用 dotnet dll 命令执行项目。很明显,它不应该被应用,因为我没有使用 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 设置配置文件名称?

最佳答案

我原以为 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

关于c# - 命令行 : execute ASP. 具有特定配置文件名称的 Net Core dll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60413875/

相关文章:

c# - InRequestScope 无法在 ExceptionLogger 上下文中工作

asp.net-core - VS2015 ASP.NET 5 beta7依赖无法解决

docker - 无法读取已安装的卷

c# - 绘图板/手写笔库

c# - 从 View 模型绑定(bind)到 ListView 项目点击属性

c# - C#,TeamCity-避免在TeamCity服务器上进行后期构建事件

c# - 如何在 docker 容器中运行 asp.net 核心应用程序?

c# - ASP.NET MVC DropDownList NullReferenceException

c# - 为 HTTPS 配置 ASP.NET Core 2.0 Kestrel

c# - 如何获取 ASP.NET Core 中所有路由的列表?