Dotnet core 3.1 Identityserver Missing Newtonsoft.Json 12.0.0

0

I got a problem while trying to run Identityserver4 in my dotnet core 3.1 application

I followed the basic configuration guide for Identityserver and wrote this to my configureService method in the Startup file.

  services.AddIdentityServer((x) =>
         {
             x.PublicOrigin = Environment.GetEnvironmentVariable("baseUrl");
         })
         .AddInMemoryApiResources(Config.Apis())
         .AddDeveloperSigningCredential()
         .AddCorsPolicyService<CorsPolicyProvider>()
         .AddInMemoryClients(Config.Clients)
         .AddExtensionGrantValidator<AzureLoginGrant>()
         .AddProfileService<ExtendedProfileService>();

Running this application locally from VS it runs perfectly no problem at all. But when I build a container image from the application and trying to run it it doesn't even start. Getting this error message.

Unhandled exception. System.IO.FileLoadException: 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 Microsoft.Extensions.DependencyInjection.IdentityServerBuilderExtensionsCrypto.AddDeveloperSigningCredential(IIdentityServerBuilder builder, Boolean persistKey, String filename, RsaSigningAlgorithm signingAlgorithm)
   at infradev_auth_service.Startup.ConfigureServices(IServiceCollection services) in /src/infradev-auth-service/Startup.cs:line 63
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services)
   at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services)
   at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
   at Microsoft.Extensions.Hosting.HostBuilder.Build()
   at infradev_auth_service.Program.Main(String[] args) in /src/infradev-auth-service/Program.cs:line 27

Any idea what can be the reason?

Running latest from each package. .csproj

  <ItemGroup>
    <PackageReference Include="IdentityServer4" Version="3.1.2" />
    <PackageReference Include="MediatR" Version="8.0.1" />
    <PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="8.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.4" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.4" />
    <PackageReference Include="Serilog" Version="2.9.0" />
    <PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
    <PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
  </ItemGroup>  
.net-core
identityserver4

1 Answer

2

Newtonsoft.Json 12.0.0 has been removed in ASP.NET Core 3.1 and replaced with System.Text.Json. This could be causing the headaches.

Installing the 'Newtonsoft.Json 12.0.0' NuGet package manually into the project should resolve the issue.

If this doesn't solve your issue then try replacing System.Text.Json with Newtonsoft.Json as the projects default JSON service.

Use the following steps for this.

So if you’re in the same boat as me and just need to get something out the door. The first thing you need is to install the following Nuget package :

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

Then update your Starup.cs using the following.

services
        .AddControllers()
        .AddNewtonsoftJson(x => x.SerializerSettings.Converters.Add(new StringEnumConverter())).AddControllersAsServices().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

This worked for us but it depends on your project setup. The instructions here give the following solution.

https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/

services.AddMvc().AddNewtonsoftJson();
services.AddControllers().AddNewtonsoftJson();
services.AddControllersWithViews().AddNewtonsoftJson();
services.AddRazorPages().AddNewtonsoftJson();

https://docs.microsoft.com/en-us/dotnet/core/compatibility/2.2-3.1#authentication-newtonsoftjson-types-replaced

answered on Stack Overflow Jun 11, 2020 by CountZero

User contributions licensed under CC BY-SA 3.0