SignalR 2依赖注入与Ninject [英] SignalR 2 Dependency Injection with Ninject

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

问题描述

我有一个使用依赖注入与Ninject现有的MVC应用程序。我安装了Ninject.MVC3的NuGet软件包,它在我的App_Start,这完全隔离内核和注册所有我的绑定创建一个名为NinjectWebCommon类:

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 in my App_Start, which completely isolates the kernel and registers all of my bindings:

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

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

private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IFoo>().To<Foo>();
}

我们有我们认为SignalR将能够满足新的要求,因此我们安装SignalR 2的NuGet包到项目中。我创建了一个集线器,并做了关于如何实现依赖注入到项目的一些搜索和发现表明,建立一个SignalRDependencyResolver的文章。 <一href=\"http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection\">http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection

We have a new requirement that we thought SignalR would be able to satisfy, so we installed SignalR 2 nuget package into the project. I created a Hub and did some searching on how to implement Dependency Injection into the project and found an article that suggests creating a SignalRDependencyResolver. http://www.asp.net/signalr/overview/signalr-20/extensibility/dependency-injection

这篇文章有您创建的文件Startup.cs内核被用于OWIN注册SignalR:

The article has you creating a kernel in the Startup.cs file that is used for registering SignalR in OWIN:

public class Startup
{
    public void Configuration(IAppBuilder app)
    {

        var kernel = new StandardKernel();
        var resolver = new NinjectSignalRDependencyResolver(kernel);

        kernel.Bind<IStockTicker>()
            .To<Microsoft.AspNet.SignalR.StockTicker.StockTicker>()  // Bind to StockTicker.
            .InSingletonScope();  // Make it a singleton object.

        kernel.Bind<IHubConnectionContext>().ToMethod(context =>
            resolver.Resolve<IConnectionManager>().GetHubContext<StockTickerHub>().Clients
            ).WhenInjectedInto<IStockTicker>();

        var config = new HubConfiguration()
        {
            Resolver = resolver
        };

        app.MapSignalR(config);

    }
}

问题是,这种方法我创建了两个不同的内核,他们似乎有自己的一套,他们知道如何解决的依赖关系。如果我有NinjectWebCommon定义的依赖,集线器不知道如何解决的依赖。不暴露我的NinjectWebCommon内核,什么是使用Ninject.MVC3包添加到DI SignalR的正确方法?

The problem is that this approach has me creating two different kernels and they seem to have their own set of dependencies that they know how to resolve. If I have a dependency defined in NinjectWebCommon, the Hub doesn't know how to resolve that dependency. Without exposing my kernel in NinjectWebCommon, what is the proper way to add DI into SignalR using the Ninject.MVC3 package?

推荐答案

我遇到详细的解释是约在的本文由Tom杜邦。我在MVC解决方案与SignalR 2.0,Ninject测试这和它的作品。

I came across detailed explanation about registering IHubActivator with DependencyResolver at this article by Tom Dupont. I tested this with SignalR 2.0, Ninject in an MVC solution and it works.

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

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