2

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

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

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

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

我在 application_start 中分配 DependencyResolver,就像在 wiki 中一样,我在 SignalR 之后执行我的 MVC4 和 WebAPI Dependency Resolvers。

这可能是什么原因造成的?

更新 - 这是 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;
        }
    }
4

1 回答 1

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);
    }
}
于 2012-07-09T14:13:19.043 回答