StructureMap和HTTP请求范围的服务-为什么在单个作用域中两次创建我的服务? [英] StructureMap and HTTP request-scoped services - why is my service created twice in a single scope?

查看:56
本文介绍了StructureMap和HTTP请求范围的服务-为什么在单个作用域中两次创建我的服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用StructureMap的ASP.NET MVC应用程序.

I have an ASP.NET MVC application using StructureMap.

我创建了一个名为SecurityContext的服务,该服务具有静态的Current属性.简化版本如下:

I have created a service called SecurityContext which has a static Current property. A simplified version looks like this:

public class SecurityContext : ISecurityContext
{
    public bool MyProperty { get; private set; }

    public static SecurityContext Current
    {
        get
        {
            return new SecurityContext() { MyProperty = true };
        }
    }
}

我已将其连接到我的StructureMap注册表中,如下所示:

I've hooked this up in my StructureMap registry as follows:

For<ISecurityContext>().Use(() => SecurityContext.Current);

我对Use方法的Linq表达式重载的理解是,对于整个HTTP请求范围,返回的具体对象都是相同的.

My understanding of this Linq expression overload of the Use method is that the returned concrete object is the same for the entire HTTP request scope.

但是,我已经建立了一个测试用例,其中我的上下文接口被注入了两个位置,一次是在控制器的构造函数中,另一次是在我的视图所继承的基类中使用 SetterProperty 属性.

However, I've set up a test case where my context interface is injected in two places, once in the controller's constructor and again using the SetterProperty attribute in the base class my view inherits from.

在调试时,我观察到 Current 静态方法被击中两次,因此很明显我的假设是错误的.有人可以纠正我在这里做什么吗?我希望这个请求范围的原因是因为我正在从数据库中将某些数据加载到上下文类中,所以我不希望在给定的页面加载中多次发生这种情况.

When debugging I observe the Current static method being hit twice so clearly my assumptions are wrong. Can anyone correct what I'm doing here? The reason I want this request-scoped is because I'm loading certain data into my context class from the database so I don't want this to happen multiple times for a given page load.

提前谢谢.

推荐答案

配置的默认生命周期是瞬态的,因此对ISecurityContext的每个请求都将创建一个SecurityContext的新实例.我想您想要的是使用旧版HttpContext生命周期.

The default lifecycle for a configuration is Transient, thus each request for an ISecurityContext will create a new instance of SecurityContext. What I think you want is to use the legacy HttpContext lifecycle.

包括StructureMap.Web nuget包.然后将您的配置更改为以下内容:

Include the StructureMap.Web nuget package. Then change your configuration to the following:

For<ISecurityContext>()
    .Use(() => SecurityContext.Current)
    .LifeCycleIs<HttpContextLifecycle>();

有关生命周期的更多信息,可以在此处找到.

More information on lifecyles can be found here.

HttpContextLifecycle已过时,但是我不知道是否或何时将其删除.StructureMap团队建议不要使用此较旧的ASP.Net生命周期.他们在文档中指出,大多数现代Web框架每个请求都使用嵌套容器来完成相同的作用域.有关嵌套容器的信息可以在此处中找到.

The HttpContextLifecycle is obsolete, however I do not know if or when it will be removed. The StructureMap team does recommend against using this older ASP.Net lifecycle. They state in the documentation that most modern web frameworks use a nested container per request to accomplish the same scoping. Information about nested containers can be found here.

我不知道您使用的ASP.Net MVC版本是否被认为是现代Web框架.我怀疑这是因为ASP.Net Core 1.0确实是ASP.Net系列中第一个完全使用DI的应用程序.但是,我将在此使用@jeremydmiller.

I don't know if the version of ASP.Net MVC you are using is considered a modern web framework. I doubt it is because ASP.Net Core 1.0 is the really the first in the ASP.Net line to fully embrace the use of DI. However, I will defer to @jeremydmiller on this one.

这篇关于StructureMap和HTTP请求范围的服务-为什么在单个作用域中两次创建我的服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆