【发布时间】:2016-02-18 18:40:31
【问题描述】:
我有一个使用 StructureMap 的 ASP.NET MVC 应用程序。
我创建了一个名为 SecurityContext 的服务,它具有静态 Current 属性。简化版如下所示:
public class SecurityContext : ISecurityContext
{
public bool MyProperty { get; private set; }
public static SecurityContext Current
{
get
{
return new SecurityContext() { MyProperty = true };
}
}
}
我已将其连接到我的 StructureMap 注册表中,如下所示:
For<ISecurityContext>().Use(() => SecurityContext.Current);
我对Use方法的这个Linq表达式重载的理解是,返回的具体对象对于整个HTTP请求范围都是一样的。
但是,我已经设置了一个测试用例,其中我的上下文接口被注入到两个地方,一次是在控制器的构造函数中,一次是在我的视图继承的基类中使用 SetterProperty 属性。
在调试时,我观察到Current 静态方法被命中两次,很明显我的假设是错误的。谁能纠正我在这里所做的事情?我希望这个请求范围的原因是因为我正在将某些数据从数据库加载到我的上下文类中,所以我不希望在给定页面加载时这种情况发生多次。
提前致谢。
【问题讨论】:
标签: c# asp.net dependency-injection structuremap