Ninject依赖注入SignalR [英] Ninject Dependency Injection for SignalR

查看:156
本文介绍了Ninject依赖注入SignalR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 NinjectWebCommon.cs 文件,在 CreateKernel 方法,我将注入像这样

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;
    }

我在想,应该为我做的,但我一直在SignalR注入得到一个错误

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

无法隐式转换类型'SignalR.Ninject.NinjectDependencyResolver'到'Microsoft.AspNet.SignalR.IDependencyResolver'。

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

任何想法是什么问题?

推荐答案

我不知道你是否已经找到了答案与否。他们已经改变DI工​​作在SignalR为1.0.0版本和SignalR.Ninject包不工作的方式 - 我写这个小博客文章:<一href=\"http://myrandom$c$csnippets.word$p$pss.com/2013/03/29/ninject-dependency-injection-with-signalr-1-0-1/\" rel=\"nofollow\">http://myrandom$c$csnippets.word$p$pss.com/2013/03/29/ninject-dependency-injection-with-signalr-1-0-1/

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/

基本上你需要创建自己实现这个是一样的SignalR.Ninject实现:

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

GlobalHost.DependencyResolver = new SignalRNinjectDependencyResolver(kernel);

在创建新类将需要从IDependancyResolver继承不同意这个和使用麻烦:

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));
    }
}

希望这有助于。

这篇关于Ninject依赖注入SignalR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆