title: Newtonsoft.Json dependency in a .Net Standard 2 package causes runtime error in ASP.Net Core MVC 2.0 app
body: <p>Sorry about the long title! I'm quite new to dotnet core so I suspect I'm running into dependency issues here, but I can't quite work out why. Here goes:</p>
  
  <p>I've rebuilt a .Net 4.5.2 library as a .Net Standard 2.0 library. The library relies on Newtonsoft.Json 11.0.1. I packaged the whole project up as a (local) nuget package, as that seems like the appropriate way to make it available to multiple projects. Here's the repo: <a href="https://github.com/oliver-wood/realex-hpp-dotnet-standard" rel="nofollow noreferrer">https://github.com/oliver-wood/realex-hpp-dotnet-standard</a>.</p>
  
  <p>The idea is to reference this package in a dotnet core 2.0 web project. So I fired up Visual Studio and chose a vanilla ASP.Net Core MVC 2.0 app. A quick debug shows that it fires up OK. <a href="https://github.com/oliver-wood/realex-test-dotnetcore" rel="nofollow noreferrer">https://github.com/oliver-wood/realex-test-dotnetcore</a></p>
  
  <p>Then I installed the nuget package, and the web project builds with no issues. BUT, as soon as I start a debug, I get a runtime error:</p>
  
  <pre><code>System.TypeLoadException has been thrown
  "Could not load type 'System.Runtime.Serialization.SerializationBinder' 
  from assembly 'netstandard, Version=2.0.0.0, Culture=neutral, 
  PublicKeyToken=cc7b13ffcd2ddd51'."
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/nsGsM.png" rel="nofollow noreferrer">Picture of Runtime error</a></p>
  
  <pre><code>at Newtonsoft.Json.JsonSerializer..ctor()
  at Newtonsoft.Json.JsonSerializer.Create(JsonSerializerSettings settings)
  at Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider..ctor(IDataProtectionProvider dataProtectionProvider, ILoggerFactory loggerFactory, IOptions`1 options)
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitIEnumerable(IEnumerableCallSite enumerableCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitSingleton(SingletonCallSite singletonCallSite, ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
  at Microsoft.Extensions.DependencyInjection.ServiceProvider.&lt;&gt;c__DisplayClass22_0.&lt;RealizeService&gt;b__0(ServiceProvider provider)
  at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
  at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
  at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
  at Microsoft.AspNetCore.Builder.MvcApplicationBuilderExtensions.UseMvc(IApplicationBuilder app, Action`1 configureRoutes)
  at realex_test_dotnetcore.Startup.Configure(IApplicationBuilder app, IHostingEnvironment env) in /Users/oliver/Dev/realex-test-dotnetcore/realex-test-dotnetcore/Startup.cs:line 41
  --- End of stack trace from previous location where exception was thrown ---
  at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
  at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app)
  at Microsoft.AspNetCore.Hosting.Internal.AutoRequestServicesStartupFilter.&lt;&gt;c__DisplayClass0_0.&lt;Configure&gt;b__0(IApplicationBuilder builder)
  at Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication()
  at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
  at realex_test_dotnetcore.Program.BuildWebHost(String[] args) in /Users/oliver/Dev/realex-test-dotnetcore/realex-test-dotnetcore/Program.cs:line 21
  at realex_test_dotnetcore.Program.Main(String[] args) in /Users/oliver/Dev/realex-test-dotnetcore/realex-test-dotnetcore/Program.cs:line 17
  </code></pre>
  
  <p>Looking down the stack, Json.Net is implicated, and it looks like it's being called under the hood by the Startup.cs call to <code>app.UseMvc()</code>. </p>
  
  <p>It looks very much like there is a conflict between Newtonsoft.Json and the built-in dotnet core libraries, but I'm not sure how to narrow it down and rectify it. Any thoughts on how to resolve, or approach resolution, would be much appreciated!  </p>
  
  <p><strong>EDIT on 17 March after comment @will-x below</strong></p>
  
  <p>I have rebuilt the nuget package on netstandard 1.6. This involved installing a couple more packages to fill in gaps in corefx and re-writing some of the code appropriately. Newtonsoft.Json remains the latest version, 11.0.1. </p>
  
  <p>This is hardly ideal in my eyes but gets my development continuing. I'm fairly certain my customer doesn't give a monkey's but I do; I'd prefer to be using the latest versions of everything if possible, but if there are issues, I guess pragmatism wins out.</p>