SignalR:解析IMessageBus

时间:2015-08-03 17:25:11

标签: asp.net signalr structuremap signalr-hub

我正在使用this方法将依赖项注入我的Hub类:

public class StructureMapDependencyResolver : DefaultDependencyResolver
{
        private readonly IContainer _container;
        public StructureMapDependencyResolver(IContainer container)
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }
            _container = container;
        }
        public override object GetService(Type serviceType)
        {
            return !serviceType.IsAbstract && !serviceType.IsInterface && serviceType.IsClass
                               ? _container.GetInstance(serviceType)
                               : (_container.TryGetInstance(serviceType) ?? base.GetService(serviceType));
        }
        public override IEnumerable<object> GetServices(Type serviceType)
        {
            return _container.GetAllInstances(serviceType).Cast<object>().Concat(base.GetServices(serviceType));
        }
 }

为了使用它,我在Global.asax

中有这些代码行
SmObjectFactory.Container.Configure(x =>
            {
                x.For<IDependencyResolver>().Singleton().Add<StructureMapDependencyResolver>();
            });
            GlobalHost.DependencyResolver = SmObjectFactory.Container.GetInstance<IDependencyResolver>();

它在SignalR版本2.1.2中的功能就像一个魅力,但升级到SignalR版本2.2.0后,我收到此错误:

No default Instance is registered and cannot be automatically determined for type 'Microsoft.AspNet.SignalR.Messaging.IMessageBus'

There is no configuration specified for Microsoft.AspNet.SignalR.Messaging.IMessageBus

1.) new AckSubscriber(*Default of IMessageBus*, *Default of IAckHandler*)
2.) Microsoft.AspNet.SignalR.Infrastructure.AckSubscriber
3.) Instance of Microsoft.AspNet.SignalR.Infrastructure.AckSubscriber
4.) Container.GetInstance(Microsoft.AspNet.SignalR.Infrastructure.AckSubscriber)

它说它无法解决IMessageBus!我尝试过这个解决方案(+),但它并没有解决问题 我应该为该依赖项提供实现吗?例如:

x.For<IMessageBus>().Use<MessageBus>();

有什么想法吗?

1 个答案:

答案 0 :(得分:4)

谢谢@VahidN,其实我应该先从基类解决(+):

public override object GetService(Type serviceType) {
    if (serviceType == null) return null;

    var service = base.GetService(serviceType);
    if (service != null) return service;

    return (!serviceType.IsAbstract && !serviceType.IsInterface && serviceType.IsClass) ? _container.GetInstance(serviceType) : _container.TryGetInstance(serviceType);
}