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

1

On macos. This is driving me crazy This issue seems to be caused by Google.Cloud.Storage.V1

This is my 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

Locally I can run dotnet run -p ./myapp/myapp.csproj without issue. Ran dotnet nuget locals all --clear and run again and it still works.

I try to launch this in a docker container and I get:

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 dockefile right from official docs:

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 the hell is happening! I can exec into the container and confirm the Newtonsoft dll is there.

EDIT: left this out because I didnt 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.

The RUN dotnet restore works after running nuget add source and even dotnet restore --no-cache --force runs without error. but I get that runtime error.

Why is my private nuget source causing this issue? restore succeeds and I see the dll. The private nuget source is an artifactory repo mirroring microsoft repos. Shouldnt it just fail if it cant find the right package?

c#
docker
asp.net-core
json.net
nuget
asked on Stack Overflow Sep 24, 2020 by red888 • edited Sep 24, 2020 by Jeff

1 Answer

0

Have you tried adding <RestoreProjectStyle>PackageReference</RestoreProjectStyle> to your .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.

Cred to Hanselman

answered on Stack Overflow Sep 29, 2020 by SofterThoughts

User contributions licensed under CC BY-SA 3.0