3

Currently I have an MVC application which also contains WebApi controllers.

I've set-up StructureMap to initialize using default conventions which handles service dependencies for both MVC and WebApi. This all works perfectly.

However, I have one Authentication service dependency which should be injected for WebApi and a different implementation for MVC. Since StructureMap has the same initialization bootstrap code, how do I switch depending on whether the request coming in is a WebApi endpoint or and Mvc controller endpoint?

1 Answer 1

1

Don't know if this is the best way of achieving this but I use the ObjectFactory.Configure method to override the initialization registries on boot-up but do this inside each SetResolver on Mvc's DependencyResolver.SetResolver and on WebApi's GlobalConfiguration.Configuration.ServiceResolver.SetResolver.

e.g.

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

and

 ObjectFactory.Configure(x => x.For<IAuthenticationService>() 
                        .Use(s => s.GetInstance<IWebApiAuthenticationService>()));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.