I used the MVC integration from autofac like this:
...
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
But now I want to recreate the solution with the new Web Api RTM.
And I want to use the new AutofacWebApiDependencyResolver class.
But if I do this with AutofacWebApiDependencyResolver i got this error:
The type Autofac.Integration.WebApi.AutofacWebApiDependencyResolver
does not appear to implement
Microsoft.Practices.ServiceLocation.IServiceLocator.
I read that i have to do this now for setting the resolver:
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
But if I set it with ‘=’, it is not set. Its still the DefaultResolver…
If I use the MVC AutofacDependencyResolver class again, it works.
Are were still problems with autofac and web api rtm? (the current integration is RC version)
ASP.Net Wep.API and ASP.NET MVC uses two different
IDependencyResolver(because they designed the Wep.API to not depend on ASP.NET MVC) so you need to setup both:So the
AutofacDependencyResolveris needed to inject decencies to regular MVCControllerderived controllers.And the
AutofacWebApiDependencyResolveris needed to inject dependencies to the Web.APIApiControllerderived controllers.And don’t forget to register your Api controllers in your container builder with the call (it differs from usual
builder.RegisterControllersmethod):