System.Net.Http.Formatting的nuget版本冲突问题

 

已经添加了nuget Microsoft.AspNet.WebApi.Client

调用System.Net.Http.HttpClient.PostAsJsonAsync的时候报如下的错误:

Could not load file or assembly 'System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference.

 

怎么回事那?

【分析】

查看了主网站的项目文件(.csproj)和DLL的项目文件(.csproj),发现两个项目中引用的.cspro版本不一致。

DLL的项目的引用如下:

<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">

<SpecificVersion>False</SpecificVersion>

<HintPath>..packagesMicrosoft.AspNet.WebApi.Client.5.2.3lib et45System.Net.Http.Formatting.dll</HintPath>

</Reference>

 

主项目中的引用如下:

<Reference Include="System.Net.Http.Formatting, Version=5.2.2.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">

<SpecificVersion>False</SpecificVersion>

<HintPath>..packagesMicrosoft.AspNet.WebApi.Client.5.2.2lib et45System.Net.Http.Formatting.dll</HintPath>

</Reference>

 

这样就导致实际放到bin目录下的是5.2.2版本,这样DLL 项目就找不到5.2.3版本了。

 

【解决方法】

这几个地方5.2.2版本都要改成5.2.3.

  1. 主项目文件。

<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">

<SpecificVersion>False</SpecificVersion>

<HintPath>..packagesMicrosoft.AspNet.WebApi.Client.5.2.2lib et45System.Net.Http.Formatting.dll</HintPath>

</Reference>

  1. 主项目的引用,可能默认引用到了是系统的.net framework目录下的,要改成和DLL项目中一样的路径(也就是nuget中的路径)
  2. 主项目的web.config。

<dependentAssembly>

<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />

<bindingRedirect oldVersion="0.0.0.0-5.2.2.0" newVersion="5.2.3.0" />

</dependentAssembly>

 

原文地址:https://www.cnblogs.com/time-is-life/p/5594737.html