使用混合生命周期定义的插件,每个 HttpContext 多次实例化

发布于 2024-10-17 20:07:11 字数 1453 浏览 4 评论 0原文

我有一个标准的基于 StructureMap 的 ControllerFactory 设置,并且有具有各种依赖项的控制器,每个依赖项都有一个 IRepository 类型的依赖项,如下所示:

// "web" assembly
public StructureMapRegistry : Registry 
{
    public StructureMapRegistry()
    {
        Scan(scanner =>
                     {
                         scanner.AssemblyContainingType<WidgetController>();
                         scanner.AssemblyContainingType<Repository>();
                         scanner.WithDefaultConventions();
                     });

        For<IRepository>().HybridHttpOrThreadLocalScoped().Use<Repository>();
    }
}

public WidgetController(IService1 service1, IService2 service2, Service3 service3)
{
    // etc, etc
}

// "data" assembly
public Service1 : IService1
{
    public Service1(IRepository repository)
    {
        // etc, etc
    }
}

public Service2 : IService2
{
    public Service2(IRepository repository)
    {
        // etc, etc
    }
}

public Service3
{
    public Service3(IRepository repository)
    {
        // etc, etc
    }
}

使用调试和 GetHashCode()等等,当 WidgetController 时,每个服务中似乎都会实例化一个新的 Repository,而我希望它们都使用相同的实例。

我是否误解了 HybridHttpOrThreadLocalScoped() 的作用?是因为我有时会传递具体的类而不是接口作为依赖项,就像我在上面的代码中使用 Service3 所做的那样(始终使用 IRepository,而不是 Repository 具体类)?我完全做错了吗?

(这段代码显然是从实际代码推断出来的,所以我希望我没有遗漏任何重要的细节。)

注意:我正在使用 StructureMap 2.6.1。

I have a standard StructureMap-based ControllerFactory setup, and I have Controllers that have various dependencies that each have a dependency of type IRepository, like so:

// "web" assembly
public StructureMapRegistry : Registry 
{
    public StructureMapRegistry()
    {
        Scan(scanner =>
                     {
                         scanner.AssemblyContainingType<WidgetController>();
                         scanner.AssemblyContainingType<Repository>();
                         scanner.WithDefaultConventions();
                     });

        For<IRepository>().HybridHttpOrThreadLocalScoped().Use<Repository>();
    }
}

public WidgetController(IService1 service1, IService2 service2, Service3 service3)
{
    // etc, etc
}

// "data" assembly
public Service1 : IService1
{
    public Service1(IRepository repository)
    {
        // etc, etc
    }
}

public Service2 : IService2
{
    public Service2(IRepository repository)
    {
        // etc, etc
    }
}

public Service3
{
    public Service3(IRepository repository)
    {
        // etc, etc
    }
}

Using debugging and GetHashCode() and the like, it seems that there's a new Repository being instantiated in each of the services when the WidgetController, when I am expecting them to all use the the same instance.

Am I misunderstanding what HybridHttpOrThreadLocalScoped() does? Is it because I'm sometimes passing in concrete classes instead of interfaces as dependences, like I did with Service3 in the code above (IRepository is ALWAYS used, never the Repository concrete class)? Am I just completely doing this wrong?

(This code was obviously extrapolated from the actual code, so I hope I didn't leave out any important details.)

NOTE: I am using StructureMap 2.6.1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深空失忆 2024-10-24 20:07:11

您发布的代码是正确的,您应该在整个请求中获得相同的 IRepository 实例。一定还有其他事情发生,您没有包含在您的问题中......

The code you have posted is correct, you should be getting the same instance of IRepository throughout your request. There must be something else going on that you have not included in your question...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文