SignalR 2 Dependency Injection with Ninject

前端 未结 3 772
野趣味
野趣味 2020-12-08 23:16

I have an existing MVC application that is using Dependency Injection with Ninject. I installed the Ninject.MVC3 nuget package and it creates a class called NinjectWebCommon

3条回答
  •  春和景丽
    2020-12-08 23:46

    There is a problem with the singleton scope. I don´t know who should get the blame here (Ninject, SignalR, MVC, etc...), but it works if you use ToConstant:

    var binding = Bind().ToConstant(new MustBeSingleton());
    

    I had the same problem, and I found the solution: SignalR, WebAPI and MVC sharing the same dependency resolver kernel

    I shared a complete solution with MVC, WebAPI and SignalR using the same Ninject kernel: https://drive.google.com/file/d/0B52OsuSSsroNX0I5aWFFb1VrRm8/edit?usp=sharing

    That example web app, contains a single page that shows the AppDomain and GetHashCode of an object that is supposed to be unique across the three frameworks, giving a result similar to:

    Dependency Test
    
    Framework   IMySingletonService instance
    MVC         AppDomainId:2 / HashCode:5109846
    WebAPI      AppDomainId:2 / HashCode:5109846
    SignalR     AppDomainId:2 / HashCode:5109846
    

    I hope this helps.

提交回复
热议问题