Daniel
Daniel

Reputation: 2534

Could not load file or assembly 'System.Web.Mvc

I have a solution with differents projects in it, One those is an MVC 3 project, and the other is a test project.

From the test project, I'm trying to test my controllers behavior. But when I'm running a test the system throws an error telling me this:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
WRN: Assembly binding logging is turned OFF.To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.

The code from the test os the following:

[TestMethod]
public void TestMethod()
{
    var myService = new Mock<IMyService>();
    myService.Setup(a => a.ReadAuthorizationRequest())
        .Returns<EndUserAuthorizationRequest>(null);
    int error = 0;
    var controller = new myController(myService.Object);

    try
    {
        var actionResult = controller.Authorize();
    }
    catch (HttpException exception)
    {
        error = exception.GetHttpCode();
    }
    Assert.AreEqual(403, error);
}

the controller:

public class myController: Controller
{
    private readonly IMyService _service;

    public OAuthController(IMyService service)
    {
        _service = service;
    }

    [Authorize, AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public ActionResult Authorize()
    {
        ClientApplication requestingClient;
        var request = _service.ReadAuthorizationRequest();
        if (request == null)
        {
            throw new HttpException((int) HttpStatusCode.BadRequest,
                "Missing authorization request.");
        }
     }
}

As you can see, I'm trying to test the controller behavior, so that, whenever the service doesn't read the request the controller should throw an httpException with httpStatusCode 403.

What is wrong with this. Thank you in advance.

Upvotes: 6

Views: 19064

Answers (4)

Sean Kendle
Sean Kendle

Reputation: 3609

In case anyone is looking for a solution to this problem in Visual Studio 2012, this website helped me out:

http://www.yeronimo.nl/post/2013/03/27/Error-1-Referencesvcmap-Could-not-load-file-or-assembly-xxxxxxx-etc

(also: http://www.christowles.com/2012/08/could-not-load-file-or-assembly.html)

Right-click on the web service reference (Reference.svcmap) in the project solution explorer (under App_WebReferences > [Name of your Web Service] > Reference.svcmap) and click "Configure Service Reference...", then uncheck the checkbox that reads "Reuse types in referenced assemblies".

Click OK, then rebuild the project! It worked for me!

Upvotes: 2

baktash
baktash

Reputation: 21

I had the same error after I installed VS 2012, none of the projects in my solution references the System.Web.Mvc. The issue was resolved for me when I added the System.Web.Mvc.dll to the GAC.

Here are the steps I did:

  1. Download System.Web.Mvc 1.0 dll file.
  2. Click Start, click All Programs, click Visual Studio, click Visual Studio Tools, and then click Visual Studio Command Prompt.
  3. Type in the following: GacUtil -I {Folder where you downloaded the System.Web.Mvc.dll} i.e. C:\Users\Your name\Desktop\System.Web.Mvc.dll

I hope this helps.

Upvotes: 2

Antony Denyer
Antony Denyer

Reputation: 1611

Or you can add a reference to the correct dll version in the test project.

Upvotes: 1

vcsjones
vcsjones

Reputation: 141588

From your exception:

System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0

It is trying to load MVC version 1. You probably need to do an assembly binding redirect in your test project as well, same as the one in the web.config:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

Place that in the configuration section of the app.config of your test assembly.

Upvotes: 12

Related Questions