Using an existing IoC Container in SignalR 2.0

后端 未结 1 1231
被撕碎了的回忆
被撕碎了的回忆 2021-02-10 06:49

How can I use an existing IoC with SignalR 2.0?

From the tutorial, it seems I need to setup a class to be called from OWIN via an attribute:

using Micros         


        
1条回答
  •  长情又很酷
    2021-02-10 07:27

    In my case I have created a custom hub activator which uses a shared container between my app and signalR (by constructor injection) that way you´ll have single composite root for the whole application.

    try the following:

    public class CustomHubActivator : IHubActivator
        {
            private readonly Container _container;
    
            public MseHubActivator(Container container)
            {
                _container = container;
            }
    
            public IHub Create(HubDescriptor descriptor)
            {
                return _container.GetInstance(descriptor.HubType) as IHub;
            }
        }
    

    register your custom hub activator when you´re bootstrapping your app (maybe the global.asax)

     GlobalHost.DependencyResolver.Register(typeof (IHubActivator),
                                                       () => new CustomHubActivator(Container));
    

    that´s much simplier solution rather than to configure again the signalR dependencyResolver

    0 讨论(0)
提交回复
热议问题