触发事件时,SignalR 集线器客户端为空 [英] SignalR Hubs Clients are null when firing event

查看:37
本文介绍了触发事件时,SignalR 集线器客户端为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个通用集线器,但遇到了一些问题,因此为了调试它,我决定进行一个简单的连接计数,如下所示:

I've written a generic hubs which I'm having some issues with so to debug it I've decided to make a simple connection count like so:

public class CRUDServiceHubBase<TDTO> : Hub, ICRUDServiceHubBase<TDTO>
{
    public const string CreateEventName = "EntityCreated";
    public const string UpdateEventName = "EntityUpdated";
    public const string DeleteEventName = "EntityDeleted";

    protected int _connectionCount = 0;

    public Task Create(TDTO entityDTO)
    {
        return Clients.All.InvokeAsync(CreateEventName, entityDTO);
    }

    public Task Update(TDTO entityDTO)
    {
        return Clients.All.InvokeAsync(UpdateEventName, entityDTO);
    }

    public Task Delete(object[] id)
    {
        return Clients.All.InvokeAsync(DeleteEventName, id);
    }

    public override Task OnConnectedAsync()
    {
        this._connectionCount++;
        return base.OnConnectedAsync();
    }

    public override Task OnDisconnectedAsync(Exception exception)
    {
        this._connectionCount--;
        return base.OnDisconnectedAsync(exception);
    }
}

public class MessagesHub : CRUDServiceHubBase<MessageDTO>
{
    public MessagesHub() : base()
    {
    }
}

我正在注册这个类:

services.AddTransient<ICRUDServiceHubBase<MessageDTO>, MessagesHub>();

我有一个正在使用它的服务,我正在使用它的实现工厂来订阅它的事件:

I have a service who is using this, which I'm using it's implementation factory to subscribing to it's events:

services.AddTransient<IMessageDTOService>( (c) => {

    var context = c.GetRequiredService<DbContext>();
    var adapter = c.GetRequiredService<IAdaptable<Message, IMessageDTO, MessageDTO>>();
    var validator = c.GetRequiredService<IValidator<Message>>();
    var entityMetadataService = c.GetRequiredService<IEntityMetadataService<Message>>();

    var service = new MessageDTOService(context, adapter, validator, entityMetadataService);
    var hub = c.GetService<ICRUDServiceHubBase<MessageDTO>>();
    this.RegisterHubsCreate(service, hub);

    return service;
});

当我去触发事件时,我得到一个空引用:

When I go to fire the event I get a null reference:

Microsoft.AspNetCore.SignalR.Hub.Clients.get 返回 null.

Microsoft.AspNetCore.SignalR.Hub.Clients.get returned null.

我最好的猜测是,因为服务是控制器的依赖项,它是在 signalR 可以初始化它的客户端之前创建的?

My best guess is that because the service is a dependency for the controller, It's created before signalR can initialize It's clients?

有人对我如何注册活动和填充客户端有建议吗?

Does anyone have a suggestion on how I can register my events, and have a client populated?

推荐答案

我发现当我想调用服务器端时,我需要将 IHubContext 注入到我的集线器中才能访问客户端.

I turns out I needed to Inject the IHubContext into my hubs to have access to the clients when I want to invoke server side.

protected IHubContext<CRUDServiceHubBase<TDTO>> _context;

public CRUDServiceHubBase(IHubContext<CRUDServiceHubBase<TDTO>> context)
{
    this._context = context;
}

public Task Create(TDTO entityDTO)
{
    return this._context.Clients.All.InvokeAsync(CreateEventName, entityDTO);
}

这篇关于触发事件时,SignalR 集线器客户端为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆