gpt4 book ai didi

c# - 将命令行参数传递给 Visual Studio 容器工具中 .NET Core 控制台应用程序的 Docker 调试运行

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:36 25 4
gpt4 key购买 nike

在 Visual Studio 2019 中,我可以使用类似于下面 launchSettings.json 中的配置来调试带有命令行参数的 .NET Core 控制台应用程序。

{
"profiles": {
"MyConsoleApp": {
"commandName": "Project",
"commandLineArgs": "-a -v"
}
}
}

我希望能够使用 Docker 容器内的 Visual Studio 容器工具对此进行调试。我向 launchSettings.json 添加了另一个构建配置文件,如下所示:

{
"profiles": {
"Docker": {
"commandName": "Docker",
"commandLineArgs": "-a -v"
},
"MyConsoleApp": {
"commandName": "Project",
"commandLineArgs": "-a -v"
}
}
}

这会根据定义的 Dockerfile 成功构建并运行控制台应用程序,但是 docker run 命令不包含 commandLineArgs 中指定的值在 launchSettings.jsonDocker 配置文件中。

生成的 docker run 命令(特别是没有 -a -v):

docker run -dt -v "C:\Users\MyUser\vsdbg\vs2017u5:/remote_debugger:rw" -v "C:\Users\MyUser\source\repos\MyConsoleApp\src\MyConsoleApp:/app" -v "C:\Users\MyUser\.nuget\packages\:/root/.nuget/fallbackpackages2" -v "C:\Program Files\dotnet\sdk\NuGetFallbackFolder:/root/.nuget/fallbackpackages" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -e "ASPNETCORE_ENVIRONMENT=Development" -e "NUGET_PACKAGES=/root/.nuget/fallbackpackages2" -e "NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages;/root/.nuget/fallbackpackages2" --entrypoint tail myconsoleapp:dev -f /dev/null

问题:

如何将命令行参数传递给 Visual Studio 容器工具中 .NET Core 控制台应用程序的 Docker 调试运行?

更新:

刚刚偶然发现 this .

当我将这样的内容添加到我的项目文件中时:

<DockerDebuggeeArguments>-a -v</DockerDebuggeeArguments>

控制台应用程序完全无法调试,并显示以下消息:

-------------------------------------------------------------------
You may only use the Microsoft .NET Core Debugger (vsdbg) with
Visual Studio Code, Visual Studio or Visual Studio for Mac software
to help you develop and test your applications.
-------------------------------------------------------------------
Did you mean to run dotnet SDK commands? Please install dotnet SDK from:
https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
The target process exited without raising a CoreCLR started event. Ensure that the target process is configured to use .NET Core. This may be expected if the target process did not run on .NET Core.
The program 'dotnet' has exited with code 145 (0x91).

我安装了 Microsoft .NET Core SDK 2.2.301 (x64)

如果我排除 DockerDebuggeeArguments 值,我可以在 Docker 中进行调试,但不会传入任何命令行参数。

最佳答案

事实证明,正确的解决方案是将以下行添加到 .csproj文件:

<DockerDebuggeeArguments>-a -v</DockerDebuggeeArguments>

但是,现在有一个错误不允许它工作。

目前的一些解决方法:

I've identified a bug in our code preventing this scenario from working as intended. I've identified some possible workarounds:

  1. If you're using Linux containers, try setting the following MSBuild properties, to pass in -a -v to your application:

<DockerDebuggeeArguments>--additionalProbingPath /root/.nuget/fallbackpackages2 --additionalProbingPath /root/.nuget/fallbackpackages "bin/$(Configuration)/$(TargetFramework)/$(AssemblyName).dll" -a -v</DockerDebuggeeArguments>

<DockerDebuggeeProgram>/usr/bin/dotnet</DockerDebuggeeProgram>

  1. For Windows containers:

<DockerDebuggeeArguments>--additionalProbingPath
c:\.nuget\fallbackpackages2 --additionalProbingPath
c:\.nuget\fallbackpackages
"bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).dll" -a
-v</DockerDebuggeeArguments>

<DockerDebuggeeProgram>C:\Program Files\dotnet\dotnet</DockerDebuggeeProgram>

  1. For either Windows or Linux containers, if it's an option, you can change your code to use environment variables instead of command-line arguments

通过@bwateratmsft

关于c# - 将命令行参数传递给 Visual Studio 容器工具中 .NET Core 控制台应用程序的 Docker 调试运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57045234/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com