SignalR集线器客户端无法在SignalR管道外部正确解析

时间:2019-07-19 09:15:34

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

请帮助,这使我发疯!

我的服务器-> SignalR(JS)客户端方法的执行可以通过SignalR管道正常运行,但不能通过后台服务调用。我正在使用ASP.NET MVC /温莎城堡DI

我使用在Owin启动期间注册的自定义依赖项解析器

我观察到,通过NotificationHub(SignalR管道),客户端被解析为HubConnectionContext类:

enter image description here

但是,客户端通过我的后台服务解析为HubConnectionContextBase类: enter image description here

...所以我很确定这是一个DI问题。我只是看不到我在做什么错。此外,任何调试技巧将不胜感激。谢谢

应用开始:

bootstrapper = ContainerBootstrapper.Bootstrap();
this.container = bootstrapper.Container;
var resolverSignalR = new DependencyResolverSignalR(container);
GlobalHost.DependencyResolver = resolverSignalR;

OwinConfig:

app.MapSignalR(url, DependencyResolverSignalR.CreateHubConfiguration());

DependencyResolverSignalR:

public class DependencyResolverSignalR : DefaultDependencyResolver
{
    public static HubConfiguration CreateHubConfiguration()
    {
        var signalrDependencyResolver = new DependencyResolverSignalR(_container);
        var configuration = new HubConfiguration {
            EnableDetailedErrors = true,
            Resolver = signalrDependencyResolver
        };
        return configuration;
    }

    private static IWindsorContainer _container;
    public DependencyResolverSignalR(IWindsorContainer container)
    {
        if (container == null)
        {
            throw new ArgumentNullException(nameof(container));
        }
        _container = container;
    }
    public override object GetService(Type serviceType)
    {
        return _container.Kernel.HasComponent(serviceType) ? _container.Resolve(serviceType) : base.GetService(serviceType);
    }
    public override IEnumerable<object> GetServices(Type serviceType)
    {
        return _container.Kernel.HasComponent(serviceType) ? _container.ResolveAll(serviceType).Cast<object>() : base.GetServices(serviceType);
    }
}

NotificationService: (每10秒通过一次循环运行-在客户端连接后)

// Not working
var hubContext = GlobalHost.ConnectionManager.GetHubContext<NotificationHub>();
var clients = hubContext.Clients;
clients.All.receiveMessage(testMessage);

NotificationHub:

public override Task OnConnected()
{
    var connectionId = Context.ConnectionId;
    ...
    ...

    // Working fine
    base.Clients.All.receiveMessage(testMessage);
    return base.OnConnected();
}

客户

omitted since it works fine via the signalr pipeline

我希望通过NotificationService调用的receiveMessage()客户端方法将在客户端上以与通过SignalR管道调用时完全相同的方式在客户端上执行。相反,娜达。没有错误消息,呼叫只是默默地执行任何操作。

我什至尝试遵循此处的指南(尽管它是针对Ninject的)SignalR document,以直接解析客户端(IHubConnectionContext)。完全一样的结果。

代码:

var resolverSignalR = new DependencyResolverSignalR(container);
            container.Register(Component
                .For<IHubConnectionContext<dynamic>>()
                .UsingFactoryMethod(() => 

resolverSignalR.Resolve<IConnectionManager().GetHubContext<NotificationHub>().Clients));

1 个答案:

答案 0 :(得分:1)

解决了!这是HubConfiguration的问题-我不确定确切是什么。但是只是绕开它(可能是因为我已经在Application_Start()中替换了DependencyResolver)解决了这个问题。

旧版OwinConfig:

app.MapSignalR(url, DependencyResolverSignalR.CreateHubConfiguration());

替换为

app.MapSignalR();

但是请确保在Application_Start()中(或在初始化DI容器的任何位置)都具有类似的内容

var resolverSignalR = new DependencyResolverSignalR(container);
GlobalHost.DependencyResolver = resolverSignalR;