23

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)

Vincent
  • 22,366
  • 18
  • 58
  • 61
user437899
  • 8,879
  • 13
  • 51
  • 71

2 Answers2

56

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:

var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = 
     new AutofacWebApiDependencyResolver(container);

So the AutofacDependencyResolver is needed to inject decencies to regular MVC Controller derived controllers.

And the AutofacWebApiDependencyResolver is needed to inject dependencies to the Web.API ApiController derived controllers.

And don't forget to register your Api controllers in your container builder with the call (it differs from usual builder.RegisterControllers method):

builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • I use AutofacDependencyResolver again for my work. Thank you for the information. Are there some reference links? Did microsoft / autofac explained this somethere? Is use the ApiControler's but if I want to cast the DependecyResolver to AutofacWebApiDependencyResolver, it fails. – user437899 Sep 05 '12 at 12:33
  • It seems to be if I access the resolver by DependencyResolver.Curren, I get the MVC resolver. And the WebApiResolver semms to be responsible for inject parameters into ApiControllers. If I want to resolve any type within a controller method, I have to use the MVC Resovler – user437899 Sep 05 '12 at 12:43
  • @Max sorry I was late both times and the other community members rejected your edit because altough it added a good point but you should have left a comment instead (I know that you don't have enough reputation but it is not far away) because the policy of handling answer edits are quite strict and the reviewers felt that you want to change my answer too much. Anyway thanks for the help I've edited in your suggestion. – nemesv Apr 30 '13 at 13:48
0

Following is a very good example

Using Autofac with Web Api

Swinkaran
  • 1,207
  • 1
  • 12
  • 19