Ninject Dependency Injection for SignalR
Asked Answered
F

1

5

In my NinjectWebCommon.cs file, under CreateKernel method I am applying the injection like this

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

        RegisterServices(kernel);

        // Web API Injection
        GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);                     

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

        return kernel;
    }

I was thinking that should do it for me but i keep getting an error at the SignalR injection that

"Cannot implicitly convert type 'SignalR.Ninject.NinjectDependencyResolver' to 'Microsoft.AspNet.SignalR.IDependencyResolver'"

Any idea what the problem is?

Fossette answered 5/2, 2013 at 11:31 Comment(7)
I'm not sure if the two are compatible, but you could attempt to typecast the dependency resolver to (Microsoft.AspNet.SignalR.IDependencyResolver) when you perform the assignment.Detective
It does remove the error, but the ninject dependency injection doesnt work. From the posts i have seen, this typecasting should never have been necessary in the first place.....dunno what the problem isFossette
Are you sure that your NinjectDependencyResolver inherits from Microsoft.AspNet.SignalR.IDependencyResolver? Can you paste the code?Acidulant
I am not familiar with SignalR at all, you are using an "I"nterface on one side of the cast, but a "real object" on the other. Is there an "I"NinjectDependencyResolver?Footrest
@AlexG SignalR.Ninject.NinjectDependencyResolver is being provided by SignalR.Ninject library, so am just using that. And yes, it inherits from DefaultDependencyResolver which in turn inherits from IDependencyResolverFossette
@Footrest isnt that the purpose of Dependency injection? using interfaces as real objectsFossette
In that case my only suggestion is to check that you are using compatible versions of each libraryAcidulant
S
8

I am not sure if you have found the answer or not. They have changed the way DI works in SignalR as of version 1.0.0 and the SignalR.Ninject packages doesn't work - I have written a small blog post on this: http://myrandomcodesnippets.wordpress.com/2013/03/29/ninject-dependency-injection-with-signalr-1-0-1/

Basically you need to create your own implementation of this which is the same as the SignalR.Ninject implementation:

GlobalHost.DependencyResolver = new SignalRNinjectDependencyResolver(kernel);

When you create the new class it will want to inherit from IDependancyResolver don't bother with this and use:

public class SignalRNinjectDependencyResolver : DefaultDependencyResolver
{
    private readonly IKernel _kernel;

    public SignalRNinjectDependencyResolver(IKernel kernel)
    {
        _kernel = kernel;
    }

    public override object GetService(Type serviceType)
    {
        return _kernel.TryGet(serviceType) ?? base.GetService(serviceType);
    }

    public override IEnumerable<object> GetServices(Type serviceType)
    {
        return _kernel.GetAll(serviceType).Concat(base.GetServices(serviceType));
    }
}

Hope this helps.

Scrawly answered 29/3, 2013 at 14:53 Comment(2)
SignalRNinjectDependencyResolver good, but its not enough for using Modules in the solution. PLease make update ?Huberty
SignalR has moved on a lot since I wrote this. You may want to look at how you can implement SignalR with Owin/Post a question with your problem.Scrawly

© 2022 - 2024 — McMap. All rights reserved.