3
votes

I've recently upgraded to the new version of the excellent SignalR library, and moved all my Dependency Injection from StructureMap to Ninject, as Ninject seemed to be better supported.

I've got the dependency injection working fine for Server-side notifications using the "Broadcasting over a Hub from outside of a Hub" described here: https://github.com/SignalR/SignalR/wiki/Hubs.

The problem I'm getting is that all SignalR messages originating from the Javascript hub don't seem to be triggering the dependency injection.

I'm also using MVC4 WebAPI which also takes some shoe-horning to get dependency injection working.

Here's my Hub:

public class PresenceHub : Hub, IPresenceHub
{
    private readonly IUserRepository _userRepository;
    private readonly IFormsAuthenticationProvider _formsAuthenticationProvider;

    public PresenceHub(IFormsAuthenticationProvider formsAuthenticationProvider, IUserRepository userRepository)
    {
        _userRepository = userRepository;
        _formsAuthenticationProvider = formsAuthenticationProvider;
    }

    public void PresenceChange(string presence)
    {
        var user = _userRepository.FindById(_formsAuthenticationProvider.GetUserId());
        var rosterEntry = Mapper.Map<User, RosterEntryDto>(user);
        rosterEntry.Presence = presence;
        Clients.updatePresence(rosterEntry);
    }
}

Here's my Ninject Bootstrapper:

Namespace SLx.Web.App_Start
{
using System;
using System.Web;

using Microsoft.Web.Infrastructure.DynamicModuleHelper;

using Ninject;
using Ninject.Web.Common;

public static class NinjectWebCommon 
{
    private static readonly Bootstrapper bootstrapper = new Bootstrapper();

    public static void Start() 
    {
        DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
        DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
        bootstrapper.Initialize(CreateKernel);
    }

    public static void Stop()
    {
        bootstrapper.ShutDown();
    }

    private static IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
        kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

        // SignalR Ninject Resolver
        GlobalHost.DependencyResolver = new SignalR.Ninject.NinjectDependencyResolver(kernel);

        // WebApi Ninject Resolver
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);

        RegisterServices(kernel);
        return kernel;
    }


    private static void RegisterServices(IKernel kernel)
    {

    }        
}

I'm notifying clients on the serverside via a PresenceProxy defined as follows:

public class PresenceHubProxy : IPresenceHubProxy
{
    private readonly IHubContext _hubContext;

    public PresenceHubProxy()
    {
        _hubContext = GlobalHost.ConnectionManager.GetHubContext<PresenceHub>();
    }

    public void NotifyLogin(RosterEntryDto user)
    {
        _hubContext.Clients.updatePresence(user);
    }

    public void NotifyLogout(RosterEntryDto user)
    {
        _hubContext.Clients.updatePresence(user);
    }
}

The Proxy works fine, injected into Controllers or their dependencies, and can send messages to the clients.

When the clients try to call SignalR via Javascript I get the following error:

No parameterless constructor defined for this object.

It looks like Ninject is not being invoked because the dependencies are not being injected into the constructor. What do I need to do to get Dependency Injection working for Javascript calls too?

Update --

Following advice from DFowler, I've replaced the Resolver in PostApplicationStart. Debugging I can see in the Immediate Window that SignalR.GlobalHost.Resolver is of type NinjectDependencyResolver but its still not working I get the same error - no paramaterless constructor.

I've then removed the NinjectDependencyResolver NuGet Library and added the source file to my solution and am using that for debugging purposes. Debugging on GetService and GetServices shows that neither method is ever called in NinjectDependencyResolver.

Any Ideas?

2

2 Answers

3
votes

Problem was I hadn't called RouteTable.Routes.MapHubs:

GlobalHost.DependencyResolver = new SignalRNinjectResolver(NinjectWebCommon.Kernel);
RouteTable.Routes.MapHubs(new SignalRNinjectResolver(NinjectWebCommon.Kernel));
2
votes

From the docs https://github.com/SignalR/SignalR/wiki/Extensibility:

NOTE: DO NOT override the global resolver in PreApplicationStart, it will not work, or it'll work only sometimes. Do it in PostApplicationStart (using WebActivator) or in Global.asax.