开发者

Using Structuremap how do i inject a property of an MVC Controller into the Constructor of a Service the Controller uses

开发者 https://www.devze.com 2023-02-13 12:31 出处:网络
I\'m new to using StructureMap as an IOC container for asp.MVC. One of my controllers takes an IStreamService interface in the constructor.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消