SignalR Hub正在被清除

时间:2012-07-05 17:27:46

标签: signalr

我刚刚将Ninject的依赖注入框架切换到了StructureMap - 由于Ninject产生了非常糟糕的性能!

我遇到的问题是当我连接DependencyResolver时,我的集线器被RouteTable.Routes.MapHubs行清除:

GlobalHost.DependencyResolver = new SignalRDependencyResolver(ObjectFactory.Container);
RouteTable.Routes.MapHubs(GlobalHost.DependencyResolver);

意味着/ SignalR / hubs javascript文件中的hubs对象有一个空的Hubs对象。

我正在像在wiki中的application_start中分配DependencyResolver,而我正在使用SignalR的MVC4和WebAPI依赖性解析器。

导致这种情况的原因是什么?

更新 - 这是DependencyResolver实现:         

public class SignalRDependencyResolver : DefaultDependencyResolver
    {
        private IContainer _container;

        public SignalRDependencyResolver(IContainer container)
        {
            _container = container;
        }

        public override object GetService(Type serviceType)
        {
            object service = null;
            if (!serviceType.IsAbstract && !serviceType.IsInterface && serviceType.IsClass)
            {
                // Concrete type resolution
                service = _container.GetInstance(serviceType);
            }
            else
            {
                // Other type resolution with base fallback
                service = _container.TryGetInstance(serviceType) ?? base.GetService(serviceType);
            }
            return service;
        }

        public override IEnumerable<object> GetServices(Type serviceType)
        {
            var objects = _container.GetAllInstances(serviceType).Cast<object>();
            objects.Concat(base.GetServices(serviceType));
            return objects;
        }
    }

1 个答案:

答案 0 :(得分:2)

以下是最终有效的依赖性解析器实现:

public class SignalRDependencyResolver : SignalR.DefaultDependencyResolver
{
    private readonly IContainer _container;

    public SignalRDependencyResolver(IContainer container)
    {

        if (container == null)
        {
            throw new ArgumentNullException("container");
        }

        _container = container;
    }

    public override object GetService(Type serviceType)
    {
        var instance = _container.TryGetInstance(serviceType) ?? base.GetService(serviceType);
        return instance;
    }

    public override IEnumerable<object> GetServices(Type serviceType)
    {
        IEnumerable<object> instances =  _container.GetAllInstances(serviceType).Cast<object>();
        IEnumerable<object> instances2 = base.GetServices(serviceType);

        return instances2.Concat(instances);
    }
}