gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 19:44:54 25 4
gpt4 key购买 nike

在 macOS 上。这个问题似乎是由 Google.Cloud.Storage.V1 引起的。
这是我的 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>
代码就是这样做的:
var storage = StorageClient.Create();
foreach (var bucketobj in storage.ListObjects("mybucket", "")) { }
查看 myapp.deps.json我看到 Newtonsoft.Json 在任何依赖项中的唯一版本是 12.0.3
在本地我可以运行 dotnet run -p ./myapp/myapp.csproj没有问题。冉 dotnet nuget locals all --clear并再次运行,它仍然有效。
我尝试在 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()
从官方文档中提取 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"]
运行容器我得到这个:
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()
怎么了?我可以执行到容器中并确认 Newtonsoft dll 在那里。
编辑
我忽略了这一点,因为我认为它不相关,但我发现删除我自己的 nuget 源可以解决问题 - 但我需要添加该源来获取我的自定义包。 RUN dotnet restore在运行 nuget add source 甚至 dotnet restore --no-cache --force 后工作运行没有错误。但我得到那个运行时错误。
为什么我的私有(private) nuget 源会导致此问题?恢复成功,我看到了 DLL。私有(private) nuget 源是镜像 Microsoft 存储库的 Artifactory 存储库。如果找不到合适的包,它不应该失败吗?

最佳答案

您是否尝试添加 <RestoreProjectStyle>PackageReference</RestoreProjectStyle>到你的 .csproj?

<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
...
</PropertyGroup>
当我在运行 Docker 容器时遇到这些错误时,它帮助我解决了类似的问题,但在 Mac 上从 VS 调试时却没有。
Cred to Hanselman

关于c# - 在容器 dotnet 核心容器中运行时无法加载文件或程序集 'Newtonsoft.Json,版本 = 12.0.0.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64049796/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com