Using Simple Injector with SignalR

前端 未结 6 535
迷失自我
迷失自我 2020-12-04 10:50

I thought using my own IoC would be pretty straight forward with SignalR and maybe it is; most likely I\'m doing something wrong. Here\'s my code I have so far:



        
6条回答
  •  無奈伤痛
    2020-12-04 11:21

    Well, I tried yesterday and I've found a solution. According to me, the only moment where I want dependency injection in SignalR is for my hubs: I don't care about how SignalR is working inside ! So instead of replacing the DependencyResolver, I created my own implementation of IHubActivator :

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

    That I can register like this (in Application_Start) :

    var activator = new SimpleInjectorHubActivator(container);
    GlobalHost.DependencyResolver.Register(typeof(IHubActivator), () => activator);
    RouteTable.Routes.MapHubs();
    

提交回复
热议问题