Unity与asp.net mvc,用属性注入传递参数
我当前正在我的 mvc 控制器中插入依赖项,如下所示:
public class HomeController : Controller
{
[Dependency]
public IProxyService ProxyService { get; set; }
}
在 global.asax 中,使用注册类型
UnityContainer _container = new UnityContainer();
_container.RegisterType<IProxyService, SystemProxyServiceEx>();
现在,我需要将一些参数传递给 SystemProxyServiceEx 构造函数。其中包括存储在身份验证期间存储的会话变量(HttpSessionStateBase Session)中的一些值。我怎样才能做到这一点?
I am currently inserting a dependency in my mvc controller as shown below:
public class HomeController : Controller
{
[Dependency]
public IProxyService ProxyService { get; set; }
}
In global.asax, the type is registered using
UnityContainer _container = new UnityContainer();
_container.RegisterType<IProxyService, SystemProxyServiceEx>();
Now, I need to pass a few parameters to the SystemProxyServiceEx constructor. These include some values stored in the session variable(HttpSessionStateBase Session) that are stored during authentication. How do I make this happen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
常见的做法是将它们包装在一个类中并基于接口注入它。例如:
现在您可以使您的
SystemProxyServiceEx
依赖于IUserContext
。最后一步是注册它,这当然很简单:The common thing to do is to wrap those in a class and inject it based on an interface. For instance:
Now you can make your
SystemProxyServiceEx
take a dependency onIUserContext
. Last step is to register it, which of course will be easy: