Using Structuremap how do i inject a property of an MVC Controller into the Constructor of a Service the Controller uses
I'm new to using StructureMap as an IOC container for asp.MVC. One of my controllers takes an IStreamService interface in the constructor.
This is easily linked to a concrete class implementation of StreamService like so
For<IStreamService>().HttpContextScoped().Use<StreamService>();
The problem i'm facing is that t开发者_StackOverflowhe concrete class constuctor takes an IPrincipal parameter, which needs to be injected. I want to pass the User property of the instantiating Controller into the Concrete Service. Could someone please point me in the right direction?
No problem, just add this line to your configuration:
For<IPrincipal>().Use(() => HttpContext.Current.User);
The use of a lambda causes this to be evaluated every time the dependency is requested (as opposed to being a single instance at configuration time.
精彩评论