使用StructureMap依赖项解析器时,SignalR无法调用客户端函数

时间:2013-09-03 09:44:33

标签: c# asp.net-mvc dependency-injection signalr structuremap

当我在signalR的global.asax中包含一个结构图依赖解析器时,对客户端函数的任何调用都不再到达浏览器,尽管我可以在日志记录管道中看到它们。

这是我的global.asax的摘录:

var container = ObjectFactory.Container;

// Now configure SignalR, MVC, ASP.Net and SharpRepository

GlobalHost.HubPipeline.AddModule(new ErrorHandlingPipelineModule());
GlobalHost.HubPipeline.AddModule(new LoggingPipelineModule());

// TODO: Work out why this breaks the connection between the server and the client.

GlobalHost.DependencyResolver = 
    ObjectFactory.GetInstance<IDependencyResolver>();
RouteTable.Routes.MapHubs();

DependencyResolver.SetResolver(
    new StructureMapDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = 
    new StructureMapDependencyResolver(container);
RepositoryDependencyResolver.SetDependencyResolver(
    new SharpRepository.Ioc.StructureMap.StructureMapDependencyResolver(container));

这里是结构图解析器的实现:

public class StructureMapSignalRDependencyResolver 
    : DefaultDependencyResolver
{
    private readonly IContainer _container;

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

        _container = container;
    }

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

        try
        {
            result = 
                !serviceType.IsAbstract &&
                !serviceType.IsInterface &&
                serviceType.IsClass
                    ? _container.GetInstance(serviceType)
                    : (_container.TryGetInstance(serviceType) 
                        ?? base.GetService(serviceType));
        }
        catch (Exception ex)
        {
            Tracing.Error(
                "[StructureMapSignalRDependencyResolver]",
                 Tracing.SerializeException(ex));
        }

        if (result == null)
        {
            Tracing.Information(
                "[StructureMapSignalRDependencyResolver]",
                "Could retrieve object of type {0}",serviceType.ToString());
        }

        return result;
    }

    public override IEnumerable<object> GetServices(
        Type serviceType)
    {
        IEnumerable<object> result = null;

        try
        {
            result = _container.GetAllInstances(serviceType)
                .Cast<object>().Concat(
                    base.GetServices(serviceType));
        }
        catch (Exception ex)
        {
            Tracing.Error(
                "[StructureMapSignalRDependencyResolver]", 
                Tracing.SerializeException(ex));
        }

        if (result == null)
        {
            Tracing.Information(
                "[StructureMapSignalRDependencyResolver]", 
                "Could retrieve object of type {0}", serviceType.ToString());
        }

        return result;
    }

    public override void Register(Type serviceType, 
        Func<object> activator)
    {
        Tracing.Information(
            "[StructureMapSignalRDependencyResolver]", 
            "Registering object of type {0}", 
            serviceType.ToString());

        base.Register(serviceType, activator);
    }

    public override void Register(Type serviceType, 
        IEnumerable<Func<object>> activators)
    {
        Tracing.Information(
            "[StructureMapSignalRDependencyResolver]", 
            "Registering object of type {0}", 
            serviceType.ToString());

        base.Register(serviceType, activators);
    }
}

这是StructureMap注册表的实现(还有更多包括应用程序集)。

[RegistryOrder(Order = 6)]
public class SignalRRegistry : Registry
{
    public SignalRRegistry()
    {
        For<IDependencyResolver>().Singleton()
            .Use<StructureMapSignalRDependencyResolver>();
        For<IHubConnectionContext>().Singleton()
            .Use(GlobalHost.ConnectionManager
                .GetHubContext<BOSSHub>().Clients);     
    }

    //public void Configure()
    //{
    //    GlobalHost.DependencyResolver = 
    //        ObjectFactory.GetInstance<IDependencyResolver>();
    //    RouteTable.Routes.MapHubs();
    //}
}

我已经回滚了Hub类,因此它不再具有依赖关系; WhatDoIHave的输出似乎有一些Microsoft.AspNet.SignalR的条目,但没有任何具体的关系。

我想知道我是否错过了我的注册表中的一个步骤?我在任何地方都没有例外;它只是停止工作,如果我从global.asax中注释掉GlobalHost.DependencyResolver行就可以了。

我希望有人可以与我分享一个为他们工作的signalR的DI实现。

非常感谢

更新:感谢您为我整理出错误的格式 - 感谢。

更新:我已经整理了一个显示问题的小型测试项目。我很惊讶,如果我是诚实的我能够重现它作为我正在处理它的应用程序相当大和复杂的结构地图的东西正在进行。我已经上传到GitHub:https://github.com/johnk27stars/SignalRTest.git - 感谢任何可以花一分钟时间看一眼的人。

1 个答案:

答案 0 :(得分:0)

在Jeremy Miller的一些诚实建议之后,我们切换到AutoFac并且工作正常。