简体   繁体   English

无法调试 do.net 核心 GenericHost docker 容器

[英]Unable to debug dotnet core GenericHost docker container

I am using Linux containers on the latest Windows build Windows 10 2004 and enabled WSL 2 and Docker Desktop 2.3.0.3 (45519).我在最新的 Windows build Windows 10 2004 上使用 Linux 容器并启用了 WSL 2 和 Docker Desktop 2.3.0.3 (45519)。

I right click the docker-compose file, and select Set as Startup Project .我右键单击 docker-compose 文件,并将 select Set as Startup Project

I then hit F5 to debug.然后我按 F5 进行调试。

I can see the image running with docker ps however breakpoints are not being hit.我可以看到使用docker ps运行的图像,但是没有遇到断点。

I cannot view Logs (on the Visual Studio Containers window) as it says:我无法查看日志(在 Visual Studio 容器窗口中),因为它说:

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

I have installed the SDKs from the link given above.我已经从上面给出的链接安装了 SDK。

Build output is below:构建 output 如下:

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 ==========

My Dockerfile is below:我的 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"]

I am unable to hit any breakpoints or even confirm if the app is running.我无法命中任何断点,甚至无法确认该应用程序是否正在运行。 I have a Generic Host app, and I don't even hit the breakpoint of the first line within public static void Main(string[] args) .我有一个通用主机应用程序,我什至没有在public static void Main(string[] args)中到达第一行的断点。

Pointers much appreciated.非常感谢指针。

UPDATE AND FIX更新和修复

So this was the smoking gun, in particular as this was a console application, and not a AspNetCore app.所以这是确凿的证据,特别是因为这是一个控制台应用程序,而不是 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

I found one of my referenced libraries had a reference to Grpc.AspNetCore .我发现我引用的库之一引用了Grpc.AspNetCore

Once i moved this code out, it was able to run (I can confirm before the container instance was not running), with full debugging.一旦我将这段代码移出,它就能够运行(我可以在容器实例未运行之前确认),并进行全面调试。

It compiled OK, the container ran, the application within the container never seemed to launch.它编译正常,容器运行,容器内的应用程序似乎从未启动。

QUESTION I would like to get to the bottom of why, as i don't fully understand how this fixed everything and what can be done to avoid in future (seems any console app that references a library that accidentally or otherwise references an AspNetCore dependency could have the same issue).问题我想深入了解原因,因为我不完全理解这是如何解决所有问题的,以及将来可以做什么来避免(似乎任何引用库的控制台应用程序意外或以其他方式引用 AspNetCore 依赖项都可以有同样的问题)。

I had run into a similar problem... well, I think that the reasons are quite similar, in my case, I could not do anything, I could not even build the project, I had compiling errors, 'simple' ones... lol... let's say that using system;我遇到了类似的问题......好吧,我认为原因非常相似,就我而言,我什么都做不了,我什至无法构建项目,我有编译错误,“简单”的...... .大声笑...假设using system; was erring and some other libraries that are there by default.是错误的以及默认情况下存在的其他一些库。

After a lot of investigation, I found out that my project was in net standard 2.0, but one of the nuget packages was for net standard 2.1, removing that package and finding a way to fix that part of the code resolved the problem.经过大量调查,我发现我的项目使用的是 net standard 2.0,但是其中一个 nuget 包适用于 net standard 2.1,删除了 package 并找到了修复该部分代码的方法解决了问题。

The reason I believe was similar to what you are after, when adding the libraries, you are adding external code, external code that you don't control, so if the external library was referencing to another library that was not compatible with your project, like in my case, so I believe that the libraries were fighting to decide if it was meant to be a console or a web server app, have you tried to see the process that was created?我相信的原因与您所追求的相似,在添加库时,您正在添加外部代码,您无法控制的外部代码,因此如果外部库引用了与您的项目不兼容的另一个库,就像我的情况一样,所以我相信图书馆正在努力决定它是作为控制台还是 web 服务器应用程序,您是否尝试查看创建的进程?

I also had a look at the library that you are mentioning ( https://www.nuget.org/packages/Grpc.AspNetCore ) and then click on dependencies and then there is this package:我还查看了您提到的库( https://www.nuget.org/packages/Grpc.AspNetCore )然后单击依赖项,然后是这个 ZEFE90A8E604A7F640D88

Grpc.AspNetCore.Server.ClientFactory

which may have caused the crash.这可能导致了崩溃。

Be careful with nuget packages, they are pretty cool and 'useful' but look at the dependencies, sometimes it is better to do your own code, that was my lesson.小心 nuget 包,它们非常酷且“有用”,但看看依赖关系,有时最好自己编写代码,这是我的教训。 For more details: https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/dependencies有关更多详细信息: https://docs.microsoft.com/en-us/dotnet/standard/library-guidance/dependencies

QUESTION I would like to get to the bottom of why, as i don't fully understand how this fixed everything and what can be done to avoid in future (seems any console app that references a library that accidentally or otherwise references an AspNetCore dependency could have the same issue).问题我想深入了解原因,因为我不完全了解这如何解决所有问题以及将来可以采取哪些措施来避免(似乎任何引用意外或以其他方式引用 AspNetCore 依赖项的库的控制台应用程序都可以有同样的问题)。

In Visual Studio Solution Explorer shows us the dependency tree with Nuget Packages and above that is the Frameworks listing Microsoft.NETCore.App :在 Visual Studio 解决方案资源管理器中向我们展示了包含 Nuget 包及以上的依赖关系树,即列出Microsoft.NETCore.App框架

在此处输入图像描述

This is how we can see the GitHub Repo references AspNetCore:这就是我们可以看到GitHub Repo引用 AspNetCore 的方式:

<FrameworkReference Include="Microsoft.AspNetCore.App" />

How can I stop this from happening?我怎样才能阻止这种情况发生?

  1. Check Solution Explorer for Frameworks.检查框架的解决方案资源管理器。
  2. Have Unit Tests on the config and dependencies.对配置和依赖项进行单元测试。
  3. Always review the Package Dependancies you import.始终查看您导入的 Package 依赖项。

If you're looking for the canonical story it's over here .如果你正在寻找经典的故事,它就在这里


The problem Iria discusses can be prevented with NuGet Version ranges and wildcards notation.使用 NuGet 版本范围和通配符表示法可以防止 Iria 讨论的问题。 When referring to package dependencies, NuGet supports using interval notation for specifying version ranges, summarized as follows:在提到 package 依赖项时,NuGet 支持使用区间表示法指定版本范围,总结如下:

+-----------+---------------+-------------------------------------------------------+
| Notation  | Applied rule  |                      Description                      |
+-----------+---------------+-------------------------------------------------------+
| 1.0       | x ≥ 1.0       | Minimum version, inclusive                            |
| (1.0,)    | x > 1.0       | Minimum version, exclusive                            |
| [1.0]     | x == 1.0      | Exact version match                                   |
| (,1.0]    | x ≤ 1.0       | Maximum version, inclusive                            |
| (,1.0)    | x < 1.0       | Maximum version, exclusive                            |
| [1.0,2.0] | 1.0 ≤ x ≤ 2.0 | Exact range, inclusive                                |
| (1.0,2.0) | 1.0 < x < 2.0 | Exact range, exclusive                                |
| [1.0,2.0) | 1.0 ≤ x < 2.0 | Mixed inclusive minimum and exclusive maximum version |
| (1.0)     | invalid       | invalid                                               |
+-----------+-------------

When you're adding Packages enforce a version, this way you keep track of your dependancies with different versions (and ultimately versions of the .net framework).当您添加包时强制执行一个版本,这样您就可以跟踪不同版本的依赖关系(最终是 .net 框架的版本)。

Change the BASE image to use as.net instead of runtime .将 BASE 图像更改为使用as.net而不是runtime The runtime image does not contain the AS.NET libs. runtime映像不包含 AS.NET 库。

runtime:3.1-buster-slim

to

as.net:3.1-buster-slim

This also works for newer releases of .NET.这也适用于 .NET 的较新版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 dotnet core docker容器不在kubernetes上工作 - dotnet core docker container not working on kubernetes Dotnet Core Docker 容器泄漏 Linux 上的 RAM 并导致 OOM - Dotnet Core Docker Container Leaks RAM on Linux and causes OOM 是否可以在 Windows docker 容器中运行的 dotnet 核心应用程序中使用 EPPlus? - Is it possible to use EPPlus in dotnet core app running in a windows docker container? 如何使用 Docker.DotNet (NET CORE) 启动容器 - How to start container with Docker.DotNet (NET CORE) 如何使用 Docker.DotNet (NET CORE) 连接到工作容器 - How to attach to working container with Docker.DotNet (NET CORE) vs buildtools容器“ dotnet测试”无法找到Dotnet Core测试项目的测试 - vs buildtools container 'dotnet test' unable to find tests for Dotnet Core test project Dotnet Core Docker 环境变量 - Dotnet Core Docker Environment Variables Convert.ToDecimal 在 dotnet core docker 容器内抛出“System.FormatException:输入字符串格式不正确” - Convert.ToDecimal throws “System.FormatException: Input string was not in a correct format” inside dotnet core docker container 如何代理在docker容器中运行的dotnet核心应用程序的每个请求 - How do I proxy every request from a dotnet core app running in a docker container Elasticsearch、Kibana、dotnet 核心和 docker - Elasticsearch, Kibana, dotnet core and docker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM