已經添加了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.
- 主項目文件。
<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> |
- 主項目的引用,可能默認引用到了是系統的.net framework目錄下的,要改成和DLL項目中一樣的路徑(也就是nuget中的路徑)
- 主項目的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> |
