简体   繁体   English

在容器 dotnet 核心容器中运行时,无法加载文件或程序集“Newtonsoft.Json,版本 = 12.0.0.0”

[英]Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0 when running in container dotnet core container

On macOS.在 macOS 上。 This issue seems to be caused by Google.Cloud.Storage.V1.此问题似乎是由 Google.Cloud.Storage.V1 引起的。

This is my csproj:这是我的 csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <RootNamespace>devopsapp_gke</RootNamespace>
    <!-- Add these BindingRedirects settings makes no difference -->
    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Google.Cloud.Storage.V1" Version="3.3.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="4.1.1" />
    <PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="3.1.0" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.1" />
    <PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="5.6.1" />
  </ItemGroup>
</Project>

The code is just doing this:代码只是这样做:

var storage = StorageClient.Create();
foreach (var bucketobj in storage.ListObjects("mybucket", "")) { }

Looking in myapp.deps.json I see the ONLY version of Newtonsoft.Json in any dependency is 12.0.3查看myapp.deps.json我看到任何依赖项中 Newtonsoft.Json 的唯一版本是 12.0.3

Locally I can run dotnet run -p ./myapp/myapp.csproj without issue.在本地,我可以dotnet run -p ./myapp/myapp.csproj运行dotnet run -p ./myapp/myapp.csproj Ran dotnet nuget locals all --clear and run again and it still works.运行dotnet nuget locals all --clear并再次运行,它仍然有效。

I try to launch this in a Docker container and I get:我尝试在 Docker 容器中启动它,我得到:

Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
File name: 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
   at Google.Apis.Json.NewtonsoftJsonSerializer..ctor()
   at Google.Apis.Json.NewtonsoftJsonSerializer..cctor()

Pulled Dockerfile right from official docs:从官方文档中直接提取 Dockerfile:

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS build-env
WORKDIR /app

COPY . ./

RUN dotnet nuget add source "https://myrepo" --name mysource --username "myuser" -p "mypass" --store-password-in-clear-text
RUN dotnet restore

RUN dotnet publish -c Release -o out --no-restore

FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
WORKDIR /app
COPY --from=build-env /app/out .
ENTRYPOINT ["dotnet", "myapp.dll"]

Running the container I get this:运行容器我得到这个:

Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'. The located assembly's manifest definition does not match the assembly reference. (0x80131040)
File name: 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
   at Google.Apis.Json.NewtonsoftJsonSerializer..ctor()
   at Google.Apis.Json.NewtonsoftJsonSerializer..cctor()

What is happening?怎么了? I can exec into the container and confirm the Newtonsoft dll is there.我可以执行到容器中并确认 Newtonsoft dll 在那里。

Edit编辑

I left this out because I didn't think it was related but I found removing my own nuget source solves the problem- but I need to add that source to get my custom packages.我忽略了这一点,因为我认为它不相关,但我发现删除我自己的 nuget 源可以解决问题 - 但我需要添加该源以获取我的自定义包。

The RUN dotnet restore works after running nuget add source and even dotnet restore --no-cache --force runs without error. RUN dotnet restore在运行 nuget add source 后工作,甚至dotnet restore --no-cache --force运行没有错误。 but I get that runtime error.但我得到了那个运行时错误。

Why is my private nuget source causing this issue?为什么我的私人 nuget 源会导致此问题? Restore succeeds and I see the DLL.恢复成功,我看到了 DLL。 The private nuget source is an Artifactory repo mirroring microsoft repos.私有 nuget 源是镜像 Microsoft 存储库的 Artifactory 存储库。 Shouldn't it just fail if it can't find the right package?如果找不到合适的包,它不应该只是失败吗?

Have you tried adding <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to your .csproj?您是否尝试将<RestoreProjectStyle>PackageReference</RestoreProjectStyle>到您的 .csproj 中?

<PropertyGroup>
   <RestoreProjectStyle>PackageReference</RestoreProjectStyle>
   ...
</PropertyGroup>

It helped me with a similar problem, when I got those errors running a Docker container, but not when debugging from VS on Mac.它帮助我解决了类似的问题,当我在运行 Docker 容器时遇到这些错误,但在 Mac 上从 VS 调试时却没有。

Cred to Hanselman 对汉塞尔曼的信任

暂无
暂无

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

相关问题 无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 12.0.0.0? - Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0? 无法加载文件或程序集“NewtonSoft.Json,版本=12.0.0.0” - Could not load file or assembly 'NewtonSoft.Json, Version=12.0.0.0' System.IO.FileNotFoundException: &#39;无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 12.0.0.0, - System.IO.FileNotFoundException: 'Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0, 无法使用 Google.Apis 加载文件或程序集 'Newtonsoft.Json,版本 = 12.0.0.0 问题。* - Could not load file or assembly 'Newtonsoft.Json, Version=12.0.0.0 issue using Google.Apis.* 无法加载文件或程序集“Newtonsoft.Json”版本=11.0.0.0 - Could not load file or assembly 'Newtonsoft.Json' Version=11.0.0.0 无法加载文件或程序集'Newtonsoft.Json,版本 = 3.5.0.0 - Could not load file or assembly 'Newtonsoft.Json, Version=3.5.0.0 无法在 Docker 容器 ASP.NET 6 中加载文件或程序集 Newtonsoft.Json - Could not load file or assembly Newtonsoft.Json in Docker container, ASP.NET 6 无法加载文件或程序集Newtonsoft.json版本6.0.0.0 - Could not load file or assembly Newtonsoft.json Version 6.0.0.0 Newtonsoft.Json版本8.0.2无法加载文件或程序集错误 - Newtonsoft.Json version 8.0.2 Could not load file or assembly Error 无法加载文件或程序集 &#39;Newtonsoft.Json,版本 = 10.0.0.0 - Could not load file or assembly 'Newtonsoft.Json, Version=10.0.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM