Callum Linington
Callum Linington

Reputation: 14417

Autofac WebAPI Common Service Locator

So reading Autofac documentation on using it with WebApi, it says:

A common error in OWIN integration is use of the GlobalConfiguration.Configuration

I have a BaseController which uses the IMediator and to get an instance from the container I use the GlobalConfiguration.Configuration.DependencyResolver.GetService().

If I don't use GlobalConfiguration what other options do I have to inject the IMediator.

It's nice not to use constructor injection because then other api controllers don't need to have a constructor, and because of IMediator rarely will.

I've looked at property injection but I couldn't understand how to integrate it in the web api scenario, I use the builder.RegisterApiControllers and it doesn't look as though integrating property injection on the base controller fits this approach.

Upvotes: 0

Views: 465

Answers (1)

Vitaliy Tsvayer
Vitaliy Tsvayer

Reputation: 773

builder.RegisterApiControllers() returns IRegistrationBuilder which means you can continue customizing Controller registration from there. For instance if you need to apply property injection to your Controllers you could use the following code:

builder.RegisterApiControllers().PropertiesAutowired();

Upvotes: 1

Related Questions