AutoFac Web Api 2 Integration Issues

I just updated all my Autofac packages to the latest version to get web api 2 support. In my api controller I created a constructor requesting an instance of a service level class, this is identical to the way I use autofac with all my mvc controllers and everything works fine .

In my ioc configuration that runs when the application starts, I registered the web api controller.

builder.RegisterType<UsersApiController>().InstancePerApiRequest();

I also tried

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

But autofac does not work with my api controller, the constructor that asks for a class of service is not even executed. I also get an error if I do not include a less default constructor in the api controller.

Is there a known issue with web api 2 and auto fac, or am I missing something?

+4
source share
1 answer

This blog worked for me.

http://danielhindrikes.se/cloud/azure/azure-mobile-services-and-autofac/

So now my code looks

public static void Register(){

            ConfigOptions options = new ConfigOptions();

            //Prepare Dependency Injection Container
            var configBuilder = new ConfigBuilder(options, DependencyInjectionConfig);


            // Use this class to set WebAPI configuration options
            HttpConfiguration config = ServiceConfig.Initialize(configBuilder);
}

private static void DependencyInjectionConfig(ContainerBuilder containerBuilder)
{
            containerBuilder.Register(component => new EFProductRepository()).As<IProductRepository>().SingleInstance();
            containerBuilder.RegisterType<ProductsManager>().As<IProductsManager>();
}
-1
source

All Articles