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>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.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>..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.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>..\packages\Microsoft.AspNet.WebApi.Client.5.2.2\lib\net45\System.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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM