Search code examples
asp.netasp.net-mvcasp.net-mvc-5ninjectninject.web.mvc

FileLoadException after installing Ninject.MVC5


I am trying to learn ASP.NET MVC with Adam Freeman's "Pro ASP.NET MVC 5" book. Unfortunately all projects using Ninject throw the same error

An exception of type 'System.IO.FileLoadException' occurred in Ninject.dll but was not >handled in user code

Additional information: Could not load file or assembly 'System.Web.Mvc, >Version=3.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)

This is exactly the same problem which was discussed in this thread,

Issues after installing ninject mvc 3 in mvc 5 project

but the offered solutions don't work for me.

I have tried target platforms 4.5 and 4.5.1, Ninject MVC3 and MVC5. I have also inserted this snippet

<runtime>
  <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
  </dependentAssembly>
</runtime>

in the Web.config file, without any effect.

Problem must be in the Ninject MVC3 and MVC5 packages. Whenever these packages are installed, any call to Ninject.StandardKernel() throws the exception, regardless if made from the Global.asaxor the new NinjectWebCommon.cs or from any other piece of code (of course, this should not matter for this kind of exception anyway, but in this thread Error using Ninject with ASP.NET V4 it was suggested that the error might have something to do with using the Global.asax for connecting Ninject to the application).

I have run out of ideas. Can anyone help?


Solution

  • Add to your Web.config (i.e. SportsStore.WebUI project):

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

    It should work with this -> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">