SignalR 2 Dependency Injection with Ninject 3.2.0, MVC 5.2.3 & .NET 4.5.1

265 views Asked by At

I cannot get both the Hub and my repository to work at the same time. I believe that it is because two different kernals are used to do the binding. When I do the suggested fix referenced below (one kernal), the client side stops processing the broadcast messages.

I built an application based off of SignalR Stock Ticker. It worked as expected. I then followed Dependency Injection in SignalR to incorporate DI with Ninject. This worked fine as well. I then added my service project to the solution. Added my bindings to NinjectWebCommon to include my service's repositories. I modified my singleton class to include the repository.

    public class AndonDisplay : IAndonDisplay
{
    private readonly IReadOnlyRepository _repo;
    private readonly AndonDisplayMsgSet _msgSet;

    private readonly object _updateAndonDisplayLock = new object();
    private volatile bool _updatingAndonDisplay = false;

    public AndonDisplay(IHubConnectionContext<dynamic> clients, IReadOnlyRepository repo)
    {
        _repo = repo;
        Clients = clients;
        _msgSet = new AndonDisplayMsgSet { Advisory = "", Maintenance = "<div class='lblDT'>OK</div>", Supervisor = "OK", Materials = "OK" };
    }

At this point, the singleton is never called. No error messages that I could see. Simply not called. I then did the suggested changes in answer with the most votes in the post linked below, "original question...". This fixed everything but the Clients.All broadcast. It was now not being processed on the client side. I have a repo that works in the singleton. I see Clients.All being called. The script on the client side is never executed.

I found this developers question... Original question posted. As the developer states in the comments of the last 'answer' posted...

"... I don't know how to put the two pieces together. The Stock Ticker sample has you create a kernel in the Startup.cs, but I'm also creating a kernel in NinjectWebCommon, and they don't know about each other. – Dismissile Jan 22 '14 at 21:31"

Does anyone have a solution for this?

Update:

If I remove .InSingletonScope() from my binding statement:

            kernel.Bind<IAndonDisplay>().To<AndonDisplay>(); //.InSingletonScope();  // Make it a singleton object.

then my hub that is injected with IAndonDisplay has a fully functional repository?!?!

Here is my NinjectWebCommon RegisterServices

        private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<AndonVistechContext>().ToSelf().InRequestScope();
        kernel.Bind<IRepository>().To<EntityFrameworkRepository<AndonVistechContext>>().InRequestScope();
        kernel.Bind<IReadOnlyRepository>().To<EfReadOnlyRepository.EntityFrameworkReadOnlyRepository<AndonVistechContext>>().InRequestScope();
        kernel.Bind<IAndonDisplay>().To<AndonDisplay>(); //.InSingletonScope();  // Make it a singleton object.
        kernel.Bind(typeof(IHubConnectionContext<dynamic>)).ToMethod(context => GlobalHost.DependencyResolver.Resolve<IConnectionManager>().GetHubContext<AndonDisplayHub>().Clients).WhenInjectedInto<IAndonDisplay>();
    }

I'm sure I'm missing something obvious.

0

There are 0 answers