user437899
user437899

Reputation: 9279

AutofacWebApiDependencyResolver usage

What is the usage of the AutofacWebApiDependencyResolver?

I set it like this:

GlobalConfiguration.Configuration.DependencyResolver = 
    new AutofacWebApiDependencyResolver(container);

But how can I access it in my ApiControllers?

DependencyResolver.Current is not the AutofacWebApiDependencyResolver, its the MVC version.

.Current is available under AutofacDependencyResolver but not under AutofacWebApiDependencyResolver. How can I access the web api version in the web api?

Upvotes: 1

Views: 2691

Answers (2)

Pavel Gatilov
Pavel Gatilov

Reputation: 7661

Try the straightforward way: this.Configuration.DependencyResolver, where this is an ApiController instance.

Upvotes: 0

Filip W
Filip W

Reputation: 27187

As you showed yourself... use GlobalConfiguration.Configuration.DependencyResolver to access this resolver, same way as you did when you set it initially (it's a static object can be accessed from anywhere in Web API).

Or, use the request dependency scope - wherever you have access to current request (i.e. in MessageHandler):

var myservice = request.GetDependencyScope().GetService(typeof(IMyService)) as IMyService

this will resolve IMyService using the currently set AutofacWebApiDependencyResolver

Upvotes: 1

Related Questions