如何使AutoFac使用每个顶级对象嵌套依赖同一个实例? (每个集线器SignalR依赖注入) [英] How to make AutoFac use same instance of nested dependency per top-level object? (SignalR dependency injection per hub)

查看:570
本文介绍了如何使AutoFac使用每个顶级对象嵌套依赖同一个实例? (每个集线器SignalR依赖注入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立我AutoFac注册以这样的方式,本次测试通过:

I am trying to set up my AutoFac registration in such a way that this test passes:

[Test]
public void Autofac_registration_test()
{
    // Given
    var builder = new ContainerBuilder();
    RegisterServices(builder);
    var container = builder.Build();

    // When
    var firstHub = container.Resolve<Hub>();
    var secondHub = container.Resolve<Hub>();

    // Then
    firstHub.Should().NotBe(secondHub);
    firstHub.FooRepo.Context.Should().Be(firstHub.BarRepo.Context);
    firstHub.FooRepo.Context.Should().NotBe(secondHub.FooRepo.Context);
}



即。我想使用相同的上下文对象一路下跌中的单个中心,而是用一个不同的时新的中心创建。

i.e. I want to use the same Context object all the way down within a single Hub, but use a different one when a new Hub is created.

RegisterServices 目前只是

private void RegisterServices(ContainerBuilder builder)
{
    builder.RegisterType<MyHub>();
    builder.RegisterType<FooRepo>();
    builder.RegisterType<BarRepo>();
    builder.RegisterType<Context>(); // How should I scope this?
}



在哪 firstHub.FooRepo.Context.Should失败(内容)。(firstHub.BarRepo.Context); ,因为上下文是瞬时范围

但每作用域一生方面也失败了,这次在 firstHub.FooRepo.Context.Should()NotBe(secondHub.FooRepo.Context);

But scoping context per lifetime also fails, this time at firstHub.FooRepo.Context.Should().NotBe(secondHub.FooRepo.Context);.

这感觉就像这是想要做一个合理的事情,所以我失去了什么明显的外装即用在这里?
或我将不得不做一些手工跟踪集线器创造?

It feels like this is a reasonable thing to want to do, so am I missing anything obvious out-of-the-box here? Or will I have to do something manual to track Hub creation?

(对于背景下,这是一个 SignalR 的应用程序。集线器是按SignalR要求创建的,所以这是一个尝试匹配单元 - 的工作在正常情况下,丝网的HTTP请求的生命周期)。

(For context, this is for a SignalR app. Hubs are created per SignalR request, so this was an attempt to match the unit-of-work lifetime of an HTTP request in normal webby situations).

推荐答案

什么@Steven在他的评论中说的是正确的,我需要每个对象的图形化的生活方式。

What @Steven said in his comment was correct, I needed a per-object-graph lifestyle.

Castle.Windsor 支持这一点,所以我swicthed使用,对我的依赖注入,而不是AutoFac。注册现在看起来像:

Castle.Windsor supports this, so I swicthed to using that for my dependency injection instead of AutoFac. The registration now looks like:

container.Register(Component.For<Hub>().LifestyleTransient());
container.Register(Component.For<FooRepo>().LifestyleTransient());
container.Register(Component.For<BarRepo>().LifestyleTransient());
container.Register(Component.For<Context>().LifestyleBoundTo<Hub>()); // Important bit

有关详细信息,请参阅:的 http://docs.castleproject.org/Windsor.LifeStyles.ashx?HL=scope#Bound_8

For more information, see: http://docs.castleproject.org/Windsor.LifeStyles.ashx?HL=scope#Bound_8

这篇关于如何使AutoFac使用每个顶级对象嵌套依赖同一个实例? (每个集线器SignalR依赖注入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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