Reputation: 10206
I've just started getting this odd Exception being thrown and I'm completely unsure how to move forward and solve it.
[A]System.Net.Http.Headers.MediaTypeHeaderValue cannot be cast to [B]System.Net.Http.Headers.MediaTypeHeaderValue.
Type A originates from 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'.
Type B originates from 'System.Net.Http, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' in the context 'Default' at location 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Net.Http\v4.0_2.0.0.0__b03f5f7f11d50a3a\System.Net.Http.dll'
The error occurs in my WebAPI on the client side on this line:
var data = responsecontent.ReadAsAsync<List<MyClass>>().Result;
I've checked every single reference to this DLL both in my web site which hosts the API client and in the API solution. They are all referencing the exact same dll:
C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5\System.Net.Http.dll
I have noticed that the API is using a newer version of the System.Net.Http.Formatting
dll but its only minor version increment and it was updated as a dependency to something else so I'm loathed to try and "downgrade" and create another issue in the process.
API System.Net.Http.Formatting is:
\Microsoft.AspNet.WebApi.Client.4.1.0-alpha-121112\lib\net40\System.Net.Http.Formatting.dll
Web Site System.Net.Http.Formatting is:
\Microsoft.AspNet.WebApi.Client.4.0.30506.0\lib\net40\System.Net.Http.Formatting.dll
I do find it concerning that NuGet will download alpha packages as dependencies even with "Stable Only" selected in the drop down menu.
Upvotes: 3
Views: 4766
Reputation: 10206
Turns out that something had gone very wrong with some NuGet updates and it had created a rogue binding redirects in my web.config file on the client side.
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
Changing this back to the previous version:
<dependentAssembly>
<assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
Fixed everything on the client side.
Upvotes: 11