How to inject various dependencies into a WebApi controller using StructureMap

I currently have an MVC application that also contains WebApi controllers.

I installed StructureMap for initialization using default conventions that handle dependencies for MVC and WebApi. It all works great.

However, I have one authentication service dependency that needs to be introduced for WebApi and another implementation for MVC. Since StructureMap has the same bootstrap code, how can I switch depending on whether the request is in the WebApi endpoint or the Mvc controller endpoint?

+3
source share
1 answer

, , ObjectFactory.Configure , SetResolver Mvc DependencyResolver.SetResolver WebApi GlobalConfiguration.Configuration.ServiceResolver.SetResolver.

.

 ObjectFactory.Configure(x => x.For<IAuthenticationService>() 
                        .Use(s => s.GetInstance<IMvcAuthenticationService>()));

 ObjectFactory.Configure(x => x.For<IAuthenticationService>() 
                        .Use(s => s.GetInstance<IWebApiAuthenticationService>()));
+1

All Articles