c# - 在 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
中描述的方式加载插件。
解决方案
推荐阅读
- powershell - 如何让写输出等到函数的输出准备好在powershell中发布?
- arrays - 从矩阵中删除行
- java - 创建优惠券机器时遇到问题
- java - 无法在 Mercurytravels.co.in 网站中导航日期选择器
- html - 从 html 按钮制作 csv 文件
- session - 如何使用 API 访问护照 js 会话?
- python - 无法在 GCP 中以编程方式限制计费:“无法禁用计费,可能需要检查权限”
- office365 - 在合规中心的哪里可以找到组角色分类列表查看器和数据分类内容查看器?
- javascript - 检查小数位的值
- sharepoint-online - 使用 REST API 查询 Sharepoint Online 并在正文中传递查询 (POST)