2
votes

I have two projects in one solution, Project 1 and Project 2.

Project 1. Target Framework is .NET Core 2.1. It's an MVC project with controllers and views. It has a reference to Project 2. Nuget packages include:

  • Microsoft.AspNetCore.App
  • Microsoft.NETCore.App

Project 2. Target Framework is .NET Framework 4.6.1. It is a class library that gets data using NEST/ElasticSearch. I'm on a time crunch so I'm basically copying over this project from a .NET Framework 4.5.1 project that runs just fine. Nuget packages include:

  • NEST 1.7.1

  • ElasticSearch.Net 1.7.1

  • Newtonsoft.Json 7.0.1

  • Microsoft.AspNet.WebApi.Client 5.2.3

Error in Project 2:

enter image description here

I am not sure why I am getting this error if I am using the exact same code as the old 4.5.1 project. The only real difference is it is in the same solution as a .NET Core 2.1 project now.

Things I've considered:

  • I tried upgrading the old project from .NET Framework 4.5.1 to .NET Framework 4.6.1. and it still runs fine, so I don't think it has to do with that.
  • My hunch is that the ASP.NET Core project uses Newtonsoft.Json 10/11, while the .NET Framework 4.6.1 project uses Newtonsoft.Json 7.0.1 and that is somehow causing the problem. But if this is the case, what is the solution? Upgrading Project 2 to Newtonsoft.Json 10/11 also requires you to update NEST and ElasticSearch.Net which introduce a ton of breaking changes. Ideally I wouldn't have to touch the code in Project 2 very much. I would really like to get this to work but keep Project 1 in ASP.NET Core 2.1 and not have both projects in the old .NET Framework.
1
In Json.NET 7.0.1, DefaultContractResolver had a parameterized constructor taking a bool argument. It was marked as obsolete but did exist. But in Commit 1914b91 on Jan 28, 2017 that constructor was removed. It's clear that, for some reason, your Project 2 is expecting the constructor to still exist. - dbc
@dbc, Since the second project is using Newtonsoft.Json 7.0.1, I think it is normal that it expects that, no? - Hasan Emrah Süngü
But honestly it's not clear what you are doing. Are you trying to combine .Net core and .Net framework DLLs, each using different versions of Json.NET, into one executable? - dbc
@EmrahSüngü - well not necessarily because there was also a default constructor. So either ElasticContractResolver was using the parameterized constructor, or the call to the default constructor was optimized into a call to the parameterized constructor (since the default constructor did in fact call the parameterized) when Project 2 was built. - dbc
@dbc, I see thanks! - Hasan Emrah Süngü

1 Answers

0
votes

It looks like your hunch is correct! You have a slight case of DLL HELL

There are a few things that you can do and here is the article that you can choose from

I personally like to edit app.config file so I will suggest that.

<dependentAssembly>  
  <assemblyIdentity name="YOUR JSON ASSEMBLY HERE"  
    publicKeyToken="32ab4ba45e0a69a1"  
    culture="en-us" />  
  <bindingRedirect oldVersion="0.0.0.0" newVersion="11.0.0.0" />  
</dependentAssembly>