ASP Web API Exception and Autofac Integration

I tried unsuccessfully to get ASP Web API / Autofac support. I created a simple project with one controller returning a string and can run the application successfully. However, when I integrate Autofac into the solution, at runtime I getSystem.EntryPointNotFoundException: Entry point was not found.

  • Visual Studio Pro 2013
  • .NET 4.5

Below are the versions of the library included in the project when I build it on my machine (excluding Autofac libs). I get Autofac files from Nuget, all other libraries 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 get EntryPointNotFoundException

in the Application_Start method:

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

      

So, I update ALL packages via Nuget and get the following 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 app again, I still get System.EntryPointNotFoundException: Entry point was not found.

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

Thanks, Kyle

+3


source to share


1 answer


I figured out the question. You need to make sure the Autofac version is compatible with the Web API version. In my case, I am using Web API 2.2 Libraries, but the Autofac version is marked with 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 is no doubt and it is easy to assume that you are using the correct version.

Kyle



enter image description here

+6


source







All Articles