Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 653
Daniel Avatar asked Jan 17 '12 22:01

Daniel


2 Answers

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.

like image 98
vcsjones Avatar answered Sep 19 '22 10:09

vcsjones


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.

like image 45
baktash Avatar answered Sep 21 '22 10:09

baktash