Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net core build error when building docker image missing Newtonsoft.Json

I'm trying to build my .net core project onto a docker container but it seems like it can't find Newtonsoft.Json which I thought was included in the asp.net core

Here is my docker file:

FROM microsoft/dotnet:2.1-aspnetcore-runtime AS base
WORKDIR /app
EXPOSE 58373
EXPOSE 44370

FROM microsoft/dotnet:2.1-sdk AS build
WORKDIR /src
COPY ["TestCode/TestCode.csproj", "TestCode/"]
RUN dotnet restore "TestCode/TestCode.csproj"
COPY . .
WORKDIR "/src/TestCode"
RUN dotnet build "TestCode.csproj" -c Release -o /app

FROM build AS publish
RUN dotnet publish "TestCode.csproj" -c Release -o /app

FROM base AS final
WORKDIR /app
COPY --from=publish /app .
ENTRYPOINT ["dotnet", "TestCode.dll"]

I'm getting the following error when trying to build Error Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

like image 598
dumbkam Avatar asked Nov 27 '18 22:11

dumbkam


People also ask

Is there a JSON DLL file for the DotNet build?

The prob­lem here is that – un­like .NET pro­jects for the reg­u­lar .NET Frame­work – the build process for a .NET Core pro­ject ( dotnet build) does not copy any de­pen­den­cies into the out­put folder. If you look into bin\Debug etcoreapp1.0 you’ll find no Newtonsoft.Json.dll file there. There’s a sec­ond prob­lem (or more an in­con­ve­nience).

What is the NewtonSoft JSON error?

This error mainly occurs when the DNN website has an incorrect version of the Newtonsoft.Json package in its configuration. At Bobcares, we often get requests to fix DNN errors, as a part of our Server Management Services. Today, let’s see how our Support Engineers fix the error for our customers. When does the Newtonsoft.Json Error occur?

How to fix ‘could not load file or assembly NewtonSoft JSON’ error?

If the Newtonsoft.Json Package is outdated then the ‘Could not load file or assembly Newtonsoft.Json’ error may appear. So we check the version of the Newtonsoft.Json Package and update it to the latest one.

How do I create a dockerfile in ASP NET Core?

Create a Dockerfile for an ASP.NET Core application Method 1: Create a Dockerfile in your project folder. Add the text below to your Dockerfile for either Linux or Windows Containers. The tags below are multi-arch meaning they pull either Windows or Linux containers depending on what mode is set in Docker Desktop for Windows.


1 Answers

In my case, When used JsonProperty in a class,

Visual Studio intellicode auto-filled,

using Newtonsoft.Json;

Then during docker build,

warning MSB3245: Could not resolve this reference. Could not locate the assembly "Newtonsoft.Json". Check to make sure the assembly exists on disk.

When I checked, found out that the Visual Studio added an Assembly Reference to Newtonsoft.Json (you can find this by expanding Dependencies node in the solution explorer in Visual Studio). And I was using Linux images.

So in order to solve this, I removed the Assembly Reference, and added nuget package Newtonsoft.Json, then the docker build was successful.

like image 167
Abhith Avatar answered Sep 30 '22 01:09

Abhith