Simple Injector Dependency Resolution Error - Could not load file or assembly System.Web.Http

4

I am following onion architecture and using simple injector in the DependencyResolution Project. Here is my architecture:

1-Core
     - Domain Classes
     - Repository Interfaces
     - Service Interfaces
2-Infrastructure
     - Data
     - Dependency Resolution
     - Repository Interfaces Implementation
     - Service Interfaces Implementation
3-WebApi
     - Web Api Project
4-WebClient
     - My AngularJs App
5-Test
     - Test Project

StartUp.cs

public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // Here I am getting error at runtime.
            SimpleInjectorInitializer.Initialize(app);
            ConfigureAuth(app);
        }
    }

SimpleInjectorInitializer.Initialize

public static Container Initialize(IAppBuilder app)
        {
            var container = GetInitializeContainer(app);

            // This is an extension method from the integration package.
            container.RegisterWebApiControllers(GlobalConfiguration.Configuration);

            container.Verify();

            GlobalConfiguration.Configuration.DependencyResolver =
                new SimpleInjectorWebApiDependencyResolver(container);

            return container;
        }

Here I added reference to System.Web, System.Web.Http, System.Web.Http.Host taken from WebApi project.

I put the path of the output of my DependencyResolution project to WebApi bin and set the value of owinstart in web.config.

Here is the error I am getting:

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

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

Edit:

Here is App.Config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
c#
.net
asp.net-web-api
dependency-injection
simple-injector
asked on Stack Overflow Jun 10, 2015 by Usman Khalid • edited Dec 26, 2017 by Steven

2 Answers

4

You are probably missing a binding redirect in the application's configuration file. In most cases, the NuGet package manager will set the binding redirects correctly for you, for sometimes it fails to do so.

The binding redirect you need in your config file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" 
            culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-{version}" newVersion="{version}"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

Note that you need to replace the {version} placeholders for the actual version you are referencing.

answered on Stack Overflow Jun 10, 2015 by Steven
1

Are you sure you are using same version of SimpleInjector in whole projects that you used Dependency Injection? I faced with similar error and I noticed that SimpleInjectors old version was used in one project. It started to work after I update SimpleInjector by nuget to new version.

answered on Stack Overflow Oct 5, 2016 by nzrytmn • edited Nov 21, 2019 by nzrytmn

User contributions licensed under CC BY-SA 3.0