c# - 无法调试 dotnet 核心 GenericHost docker 容器

标签 c# .net docker .net-core

我在最新的 Windows 版本 Windows 10 2004 上使用 Linux 容器并启用 WSL 2 和 Docker Desktop 2.3.0.3 (45519)。
我右键单击 docker-compose 文件,然后选择 Set as Startup Project .
然后我按 F5 进行调试。
我可以看到使用 docker ps 运行的图像但是断点没有被击中。
我无法查看日志(在 Visual Studio Containers 窗口中),因为它说:

It was not possible to find any compatible framework version
The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
  - No frameworks were found.

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=3.1.0&arch=x64&rid=debian.10-x64
我已经从上面给出的链接安装了 SDK。
构建输出如下:
1>------ Build started: Project: Libertas.Host.Tickers.ScheduledTasks, Configuration: Debug Any CPU ------
1>Libertas.Host.Tickers.ScheduledTasks -> C:\Users\User\Source\Repos\myrepo\Libertas\src\Libertas.Host.Tickers.ScheduledTasks\bin\Debug\netcoreapp3.1\Libertas.Host.Tickers.ScheduledTasks.dll
1>docker build -f "C:\Users\User\Source\Repos\myrepo\Libertas\src\Libertas.Host.Tickers.ScheduledTasks\Dockerfile" --force-rm -t libertashosttickersscheduledtasks:dev --target base  --label "com.microsoft.created-by=visual-studio" --label "com.microsoft.visual-studio.project-name=Libertas.Host.Tickers.ScheduledTasks" "C:\Users\User\Source\Repos\myrepo\Libertas\src"
1>Sending build context to Docker daemon  1.362MB
1>
1>Step 1/4 : FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
1> ---> 86a2e7d45948
1>Step 2/4 : WORKDIR /app
1> ---> Running in d1ed1740d43e
1>Removing intermediate container d1ed1740d43e
1> ---> 90bd1703e28d
1>Step 3/4 : LABEL com.microsoft.created-by=visual-studio
1> ---> Running in 2626d5865d89
1>Removing intermediate container 2626d5865d89
1> ---> da74703374d2
1>Step 4/4 : LABEL com.microsoft.visual-studio.project-name=Libertas.Host.Tickers.ScheduledTasks
1> ---> Running in 7a381e7ea47a
1>Removing intermediate container 7a381e7ea47a
1> ---> fd2dd439cce6
1>Successfully built fd2dd439cce6
1>Successfully tagged libertashosttickersscheduledtasks:dev
1>SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.
1>docker rm -f 9ff95181e06801ed3d4b4d5743397604b743d77c840f2047fb2caee046e5d8eb
1>Error: No such container: 9ff95181e06801ed3d4b4d5743397604b743d77c840f2047fb2caee046e5d8eb
1>docker run -dt -v "C:\Users\User\vsdbg\vs2017u5:/remote_debugger:rw" -v "C:\Users\User\Source\Repos\myrepo\Libertas\src\Libertas.Host.Tickers.ScheduledTasks:/app" -v "C:\Users\User\Source\Repos\myrepo\Libertas\src:/src/" -v "C:\Users\User\AppData\Roaming\Microsoft\UserSecrets:/root/.microsoft/usersecrets:ro" -v "C:\Users\User\.nuget\packages\:/root/.nuget/fallbackpackages" -e "DOTNET_USE_POLLING_FILE_WATCHER=1" -e "NUGET_PACKAGES=/root/.nuget/fallbackpackages" -e "NUGET_FALLBACK_PACKAGES=/root/.nuget/fallbackpackages" --name Libertas.Host.Tickers.ScheduledTasks_1 --entrypoint tail libertashosttickersscheduledtasks:dev -f /dev/null
1>ba95df9d32d6a0af07b1eab32af606131e075b2afff664c4003dbe3eae349543
========== Build: 1 succeeded, 0 failed, 6 up-to-date, 0 skipped ==========
我的 Dockerfile 如下:
#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/core/runtime:3.1-buster-slim AS base
WORKDIR /app

FROM mcr.microsoft.com/dotnet/core/sdk:3.1-buster AS build
WORKDIR /src
COPY ["Libertas.Host.Tickers.ScheduledTasks/Libertas.Host.Tickers.ScheduledTasks.csproj", "Libertas.Host.Tickers.ScheduledTasks/"]
COPY ["Libertas.Application.Tickers.ScheduledTasks/Libertas.Application.Tickers.ScheduledTasks.csproj", "Libertas.Application.Tickers.ScheduledTasks/"]
COPY ["PolygonIo.WebSocket/PolygonIo.WebSocket.csproj", "PolygonIo.WebSocket/"]
RUN dotnet restore "Libertas.Host.Tickers.ScheduledTasks/Libertas.Host.Tickers.ScheduledTasks.csproj"
COPY . .
WORKDIR "/src/Libertas.Host.Tickers.ScheduledTasks"
RUN dotnet build "Libertas.Host.Tickers.ScheduledTasks.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "Libertas.Host.Tickers.ScheduledTasks.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "Libertas.Host.Tickers.ScheduledTasks.dll"]
我无法点击任何断点,甚至无法确认应用程序是否正在运行。我有一个通用主机应用程序,我什至没有碰到 public static void Main(string[] args) 中第一行的断点.
指针非常感谢。
更新和修复
所以这是确凿的证据,特别是因为这是一个控制台应用程序,而不是一个 AspNetCore 应用程序。
It was not possible to find any compatible framework version
The framework 'Microsoft.AspNetCore.App', version '3.1.0' was not found.
  - No frameworks were found.

You can resolve the problem by installing the specified framework and/or SDK.

The specified framework can be found at:
  - https://aka.ms/dotnet-core-applaunch?framework=Microsoft.AspNetCore.App&framework_version=3.1.0&arch=x64&rid=debian.10-x64
我发现我引用的库之一引用了 Grpc.AspNetCore .
一旦我将这段代码移出,它就能够运行(我可以在容器实例未运行之前确认),并进行全面调试。
它编译好,容器运行,容器内的应用程序似乎从未启动。

我想找出原因,因为我不完全理解这是如何修复所有内容以及将来可以采取什么措施来避免的(似乎任何引用意外或以其他方式引用 AspNetCore 依赖项的库的控制台应用程序都可能具有同样的问题)。

最佳答案

我遇到了类似的问题......好吧,我认为原因非常相似,就我而言,我无能为力,我什至无法构建项目,我有编译错误,“简单”错误...... . 大声笑...让我们说 using system;出错了,以及默认情况下存在的其他一些库。
经过大量调查,我发现我的项目在 net standard 2.0 中,但其中一个 nuget 包适用于 net standard 2.1,删除该包并找到修复该部分代码的方法解决了问题。
我相信的原因与您所追求的类似,在添加库时,您正在添加外部代码,您无法控制的外部代码,因此如果外部库引用了与您的项目不兼容的另一个库,就像在我的情况下一样,所以我相信图书馆正在努力决定它是作为控制台还是网络服务器应用程序,您是否尝试查看创建的过程?
我还查看了您提到的库( https://www.nuget.org/packages/Grpc.AspNetCore ),然后单击依赖项,然后有这个包:

Grpc.AspNetCore.Server.ClientFactory
这可能导致了崩溃。
小心 nuget 包,它们非常酷且“有用”,但请查看依赖项,有时最好编写自己的代码,这是我的教训。
更多详情:https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/dependencies

关于c# - 无法调试 dotnet 核心 GenericHost docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62798735/

相关文章:

c# - XMLHttpRequest() 未被识别为 IsAjaxRequest?

c# - 在服务器上获取请求客户端的IP地址

c# - 如何对扩展方法进行分组?

c# - 列表中的所有引用都指向 'this'

amazon-web-services - AWS Blue/Green ECS部署中的Codebuild在执行命令时返回错误:docker build -t:latest

docker - 如何将配置文件保存在正在运行的容器中?

c# - 触发并忘记异步任务方法有时不会调用

c# - MVC View 没有将模型传回 Controller ,出现奇怪的错误?

c# - 使用 XmlSerializer() 反序列化带有&符号的 XML

docker - 在 Docker 容器中运行的 SQL Server 2017 Linux 镜像中从 .cer 和 .p7b 创建证书