1

我在我的 Web API 中创建了一个集线器。这很简单:

public class DashboardHub : Hub
{
    public async Task SendMessage(InfoSummary infoSummary)
    {
        await Clients.All.SendAsync("ReceiveMessage", infoSummary);
    }
}

当数据更新时,我正在尝试从同一 Web API 中的控制器向集线器发送消息。

我已经看到了 100 个不同的答案,但没有任何效果。基本上我的控制器中的集线器对象为空,我似乎无法实例化它。

    private readonly IRepository _repo;
    private readonly Helpers.Convert _convert;
    private readonly CoreContext _context;
    private readonly IMapper _mapper;
    private readonly NotifyService _service;
    private readonly DashboardHub _hub;

    public MyController(IRepository repo, 
                                CoreContext context, 
                                IMapper mapper)
        {
            _convert = new Helpers.Convert(repo, mapper);
            _repo = repo;
            _context = context;
            _mapper = mapper;
            _hub = new DashboardHub();
            _service = new NotifyService(_hub);
        }

    [HttpPost("updatestatus")]
    public async Task<IActionResult> UpdateStatus(Header header) {

        var returnVal = await _repo.ChangeStatus(header.HeaderId, header.Status);

        headerSummary = _convert.ToReturnStatusHeader( await _repo.GetHeader(header.HeaderId));
        // await _service.SendNotificationAsync(headerSummary);
        await _hub.SendMessage(headerSummary);        

        return Ok(returnVal);
    }

我有

services.AddSignalR();
services.AddScoped(typeof(DashboardHub));

endpoints.MapHub<DashboardHub>("/Hubs/DashboardHub");

在 startup.cs 文件的适当部分

我知道我错过了一些非常小的东西,但我很想知道它是什么。

我也尝试过创建一个强类型集线器,但这引入了更多问题。

提前致谢。

4

3 回答 3

0

好吧,我觉得自己是一个完整而彻底的新手。修复非常简单。您必须添加 using 语句,告诉控制器您要使用 SignalR。OMG ..我几乎不好意思提出这个问题,但希望它对其他人有所帮助。

使固定:

using Microsoft.AspNetCore.SignalR;

:facepalm

于 2020-01-08T16:27:07.373 回答
0

你在那里做了四个错误。

  1. 您不需要此行出现在Startup.cs的ConfigureServices方法中。services.AddScoped(typeof(DashboardHub));

    去掉它。就收下吧services.AddSignalR();

  2. 为什么要使用新的关键字,因为 .net 核心提供了内置的依赖注入服务。删除下面的行。

    _hub = new DashboardHub();
    _service = new NotifyService(_hub); 
    

    而是为NotifyService.cs创建一个新接口INotifyService.cs在Startup.cs的ConfigureServices方法中注册此服务。

    services.AddScoped<INotifyService, NotifyService>();

  3. 您的MyController.cs应该如下所示

    添加这一行。 using Microsoft.AspNetCore.SignalR;

    private readonly IRepository _repo;
    private readonly Helpers.Convert _convert;
    private readonly CoreContext _context;
    private readonly IMapper _mapper;
    private readonly INotifyService _service;
    private readonly IHubContext<DashboardHub> _hubContext

    public MyController(IRepository repo, CoreContext context, IMapper mapper,INotifyService service,IHubContext<DashboardHub> hubContext)
        {
            _convert = new Helpers.Convert(repo, mapper);
            _repo = repo;
            _context = context;
            _mapper = mapper;
            _service = service;
            _hubContext = hubContext;
        }

    [HttpPost("updatestatus")]
    public async Task<IActionResult> UpdateStatus(Header header) {

        var returnVal = await _repo.ChangeStatus(header.HeaderId, header.Status);

        headerSummary = _convert.ToReturnStatusHeader( await _repo.GetHeader(header.HeaderId));
        // await _service.SendNotificationAsync(headerSummary);

        await hubContext.Clients.All.SendAsync("ReceiveMessage", headerSummary);

        return Ok(returnVal);
    }
  1. 如果您在NotifyService.cs 中发送消息,请使用相同的概念。
于 2020-01-10T13:09:35.040 回答
-1

你可以做的是在你的控制器中使用依赖注入来注入你的集线器。您不能像您正在做的那样在控制器中实例化集线器,我也会将其更改为Singleton

services.AddSingleton(typeof(DashboardHub));
internal DashboardHub DashboardHub
{
    get
    {
        return this.HttpContext.RequestServices.GetRequiredService<DashboardHub>();
    }
}
于 2020-01-08T12:47:10.480 回答