aspnet / AspNetWebStack

ASP.NET MVC 5.x, Web API 2.x, and Web Pages 3.x (not ASP.NET Core)
Other
858 stars 354 forks source link

Newtonsoft.Json (>= 6.0.4) conflicting version #241

Closed gittadesushil closed 5 years ago

gittadesushil commented 5 years ago

Hi,

I am getting below error :

System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation. —-> System.IO.FileLoadException : Could not load file or assembly ‘Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Actually I have IdentityModel 3.10.6 which uses Newtonsoft.Json (>= 11.0.1) and Microsoft.AspNet.WebApi.OwinSelfHost 5.2.7 which uses Newtonsoft.Json (>= 6.0.4).

Newtonsoft.Json used by Microsoft.AspNet.WebApi.OwinSelfHost 5.2.7 that I can't control. I tried Assembly binding redirect concept and hope this issue may solve but still it's giving me the error and I am blocked.

reZach commented 5 years ago

I am using a HttpClient.PostAsJsonAsync() from a .NET 4.5 project whose Microsoft.AspNet.WebApi.Client version is 5.2.7; I received the following error.

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

I will try downgrading this package and see if I can resolve and post here if that works.

edit Downgrading to all stable packages (non -preview, -beta, -rc, etc) up to 4.0.30506 and each showed a dependency on Newtonsoft.Json (not all packages depended on 6.0.0.0 but that's not the point). As the OP said, I also tried an assembly binding and that did not work either.

reZach commented 5 years ago

This issue is very similar to this one for Twilio: https://github.com/twilio/twilio-csharp/issues/422.

gittadesushil commented 5 years ago

I am using a HttpClient.PostAsJsonAsync() from a .NET 4.5 project whose Microsoft.AspNet.WebApi.Client version is 5.2.7; I received the following error.

Could not load file or assembly 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.

I will try downgrading this package and see if I can resolve and post here if that works.

edit Downgrading to all stable packages (non -preview, -beta, -rc, etc) up to 4.0.30506 and each showed a dependency on Newtonsoft.Json (not all packages depended on 6.0.0.0 but that's not the point). As the OP said, I also tried an assembly binding and that did not work either.

For me also assembly binding did not work just for the sake of workaround I tried AssemblyResolve event.

reZach commented 5 years ago

@gittadesushil I found a fix, at least for me.

The WebApi.Client we are referencing was occurring in a nuget package we manage, and so I added a Newtonsoft.Json PackageReference to the HttpClient's .csproj like this.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFrameworks>netstandard2.0;net45;</TargetFrameworks>
    <Authors>[authors]</Authors>
    <Product>[product]</Product>
    <Description>[desc]</Description>
    <Version>2.1.3</Version>
  </PropertyGroup>

  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
    <DocumentationFile>bin\Release\netstandard2.0\ApiClient.xml</DocumentationFile>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNet.WebApi.Client" Version="5.2.7" />
    <PackageReference Include="Newtonsoft.Json" Version="12.0.1" /> <!-- added this -->
  </ItemGroup>

</Project>

After adding this, I rebuilt the solution and updated the package on Nuget. Referencing this updated package solved the error for me.

gittadesushil commented 5 years ago

Above solution doesn't work for me so I used AssemblyResolve event and load the dll which solve my issue. Please find Solution 2: Override AssemblyResolve for side-by-side loading (No need for strong names) in below link: https://michaelscodingspot.com/how-to-resolve-net-reference-and-nuget-package-version-conflicts/

Right now I am closing this issue.