首页 > 解决方案 > 在 Docker 容器中访问 Nuget 包

问题描述

我需要在 Docker 容器中构建和运行我的 .Net Core 控制台应用程序。VS sln 的构建工作,并且 nuget 包恢复工作,但控制台应用程序无法运行,因为它找不到 nuget 依赖项 - 即使它们已恢复并存在。整个问题可能归结为需要一种方法来控制探查 nuget 包的位置或方式。

我正在使用mcr.microsoft.com/dotnet/core/sdk:3.1作为我的基本图像。这是一个多阶段 docker 构建,这意味着第 1 阶段需要复制控制台应用程序二进制文件以及在第 0 阶段恢复的所有 nuget 文件。我想这意味着我不希望 nuget 包加载到通常的nuget 缓存位置。假设,我认为解决此问题的最简单方法是在第 0 阶段直接恢复控制台应用程序的目标构建文件夹下的所有 nuget 包。

此外,我需要明确定位 x86,因此我的 docker 文件构建“PlatformExtract”控制台应用程序,如下所示:

RUN dotnet restore --packages .\PlatformExtract\bin\x86\Release\netcoreapp3.1 NeoAgent.sln
RUN dotnet build --no-restore --configuration Release -p:Platform=x86 NeoAgent.sln

构建完成后,正如预期的那样,所有 nuget 包都可以在控制台应用程序下方的文件夹中找到。但是,当我在上述容器中运行应用程序时,它会失败。我的控制台应用程序解决方案具有控制台应用程序在运行时加载的“连接器”插件项目(即 DLL)。以下是加载插件但未找到关联的 nuget 依赖项时导致的错误示例:

Unhandled exception. System.InvalidOperationException: Dependency resolution failed for component C:\PlatformConnect\SqliteConnector.dll with error code -2147450740. Detailed error: Error:
  An assembly specified in the application dependencies manifest (SqliteConnector.deps.json) was not found:
    package: 'SQLitePCLRaw.lib.e_sqlite3', version: '2.0.2'
    path: 'runtimes/win-x86/native/e_sqlite3.dll'

我查看了上述路径中指定的文件夹,DLL 在那里。因此,运行时根本不会费心查看控制台应用程序 exe 正下方的文件夹。我暂时选择了删除上面的“SqliteConnector”插件,这只会导致下一个插件在无法加载 nuget 依赖项时无法加载:

Unhandled exception. System.InvalidOperationException: Unable to load types from assembly AzureBlobConnector, Version=1.
0.0.0, Culture=neutral, PublicKeyToken=null. Unable to load one or more of the requested types.
Could not load file or assembly 'Azure.Core, Version=1.2.2.0, Culture=neutral, PublicKeyToken=92742159e12e44c8'. The system cannot find the file specified.
 ---> System.Reflection.ReflectionTypeLoadException: Unable to load one or more of the requested types.

如何配置应用程序,以便它直接在包含主可执行文件的文件夹下查找所有 nuget 依赖项?

此外,如果它是相关的,我正在使用本文AssemblyLoadContext中描述的方式加载插件。

标签: c#visual-studiodocker.net-corenuget

解决方案


推荐阅读