Search code examples
c#asp.net-web-apisimple-injector

Simple Injector exception: cannot load dlls when web api project starts


I got this exception "Could not load file or assembly System.Web.Http, Version=4.0.0.0" when starting my WebApi application, I register in the AppStart method.

    protected void Application_Start()
    {
        RegisterInjector();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);           

    }
    private void RegisterInjector()
    {

        var container = new Container();
        container.Verify();
        GlobalConfiguration.Configuration.DependencyResolver =
     new SimpleInjectorWebApiDependencyResolver(container);
    }

Solution

  • The Simple Injector integration package for Web API references the lowest required version of the System.Web.Http assembly and the accepted way of upgrading to a newer version is by configuring a bindingRedirect in your config file that forces .NET to use the newer System.Web.Http assembly. When installing packages through Nuget, these binding redirects are automatically managed for you (or at least - most of the time) and in that case it is not something you have to do manually.

    In other cases, you will have to add the binding direct manually to your application's configuration file:

    <configuration>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="System.Web.Http" publicKeyToken="b03f5f7f11d50a3a" 
                culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0"/>
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>