简体   繁体   中英

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 the 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM