Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot load file after installing CORS

I have created a basic ASP.NET Web Api Application inside VS Express 2013 using all of the defaults. I added a controller and it returns XML just as I want.

As soon as I install the CORS package:

Install-Package Microsoft.AspNet.WebApi.Cors -Pre

I can't even run the application anymore:

An exception of type 'System.IO.FileLoadException' occurred in mscorlib.dll and wasn't handled before a managed/native boundary

Additional information: Could not load file or assembly 'System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

I followed all of the instructions on this blog post: http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api which says nothing about this issue.

I tried updating Web API with:

Install-Package Microsoft.AspNet.WebApi -Pre

But still the problem persists unfortunately

In the project I am using WebApi.Hal which depends on Web.Api 5.0 and greater. Are there any breaking changes perhaps?

Absolute Minimum To Reproduce the Problem

  1. Create new, completely empty, Web API project n VS Express for Web 2013
  2. Add a controller and test it works
  3. Install CORS
  4. File load error - controller doesn't work anymore
  5. Upgrade Web Api - fixes problem

    Install-Package Microsoft.AspNet.WebApi -Pre

  6. Install WebApi.Hal - breaks with same error again

Can I use an old version of the CORS library that doesn't kill everything else?

Detailed Stack trace

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
 (Fully-specified)
LOG: Appbase = file:///C:
LOG: Initial PrivatePath = C
Calling assembly : WebApi.Hal, Version=2.2.0.18, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:
LOG: Using host configuration file: C:\
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: System.Net.Http.Formatting, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
LOG: Attempting download of new URL file:///C: Files/root/aa0e7960/9dfdb45e/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C: Files/root/aa0e7960/9dfdb45e/System.Net.Http.Formatting/System.Net.Http.Formatting.DLL.
LOG: Attempting download of new URL file:///C:/CORSTest/CORSTest/bin/System.Net.Http.Formatting.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.
like image 275
user2668128 Avatar asked Mar 20 '14 21:03

user2668128


2 Answers

Two step to fix the problem

  1. Add

    dependentAssembly assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" dependentAssembly

to web.config

  1. Go tool/Library Packed Manager/Manage Nuget Package For solution and then Click Microsoft ASP.Net MVC and then click Update.

Hope it help

Tam Nguyen

like image 142
Tam Nguyen Avatar answered Oct 17 '22 09:10

Tam Nguyen


The problem is due to version conflicts of System.Net.Http.Formatting as well as System.Web.Http assemblies. If you see your output window after building your code, it gives you the solution to the problem. All you have to do is add the following into your Web.config file:

<dependentAssembly><assemblyIdentity name="System.Net.Http.Formatting" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly>

<dependentAssembly><assemblyIdentity name="System.Web.Http" culture="neutral" publicKeyToken="31bf3856ad364e35" /><bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" /></dependentAssembly>
like image 34
sumit Avatar answered Oct 17 '22 10:10

sumit