How to make AutoFac use same instance of nested dependency per top-level object? (SignalR dependency injection per hub)

后端 未结 1 520
孤城傲影
孤城傲影 2021-01-07 10:19

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 buil         


        
相关标签:
1条回答
  • 2021-01-07 10:44

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

    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
    

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

    0 讨论(0)
提交回复
热议问题