Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP Web API and Autofac Integration Exception

I've been trying without success to get ASP Web API / Autofac integration working. I've created a simple project with one Controller returning a string and am able to run the application successfully. However, when I integrate Autofac into the solution, at runtime I receive a System.EntryPointNotFoundException: Entry point was not found.

  • Visual Studio Pro 2013
  • .NET 4.5

Here are the library versions included in the project when I create it on my machine (minus the Autofac libs). I get the Autofac libs from Nuget, all other libs are locally installed.

  • System.Web 4.0
  • System.Web.Extensions 4.0
  • System.Web.Http 5.0
  • System.Web.Http.WebHost 5.0
  • System.Web.Services 4.0
  • Autofac 3.0 / Description says 3.1.5
  • Autofac.Integration.WebApi 3.0 / Description says 3.1

I put the Autofac code in my Register method like this:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        //Other code left out for brevity

        var builder = new ContainerBuilder();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // Set the dependency resolver to be Autofac.
        var container = builder.Build();
        config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
    }
}

I receive an EntryPointNotFoundException in the Application_Start method:

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

So then I upgrade ALL the packages via Nuget and get these versions:

  • System.Web 4.0
  • System.Web.Extensions 4.0
  • System.Web.Http 5.2.2
  • System.Web.Http.WebHost 5.2.2
  • System.Web.Services 4.0
  • Autofac 3.5 / Description says 3.5.2
  • Autofac.Integration.WebApi 3.0 / Description says 3.1.0

And this section is now inserted into the web.config file:

  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>

When I run the application again, I still receive System.EntryPointNotFoundException: Entry point was not found.

What am I missing here to get Autofac and Web API to play nicely together?

Thanks, Kyle

like image 827
Kyle Novak Avatar asked Jan 09 '23 13:01

Kyle Novak


1 Answers

I figured out the issue. You need to make sure the version of Autofac is compatible with the version of Web API. In my case, I'm using the Web API 2.2 libs but the Autofac version labeled Autofac ASP.NET Web API Integration. Once I switched to Autofac ASP.NET Web API 2.2 Integration, then my solution worked. A subtle difference no doubt and easy to assume you're using the right version.

Kyle

enter image description here

like image 54
Kyle Novak Avatar answered Jan 23 '23 07:01

Kyle Novak