Signalr 从Web API在信号器中发送消息
我已经在我的Web API中创建了一个中心。这很简单:Signalr 从Web API在信号器中发送消息,signalr,signalr-hub,webapi,Signalr,Signalr Hub,Webapi,我已经在我的Web API中创建了一个中心。这很简单: public class DashboardHub : Hub { public async Task SendMessage(InfoSummary infoSummary) { await Clients.All.SendAsync("ReceiveMessage", infoSummary); } } 当数据更新时,我试图从同一Web API中的控制器向集线器发送消息 我已经看到了100个不同
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);
}
及
endpoints.MapHub(“/Hubs/DashboardHub”);
在startup.cs文件的适当部分中
我知道我错过了一些很小的东西,但我很想知道它是什么
我也尝试过创建一个强类型中心,但这带来了更多的问题
提前感谢。您可以做的是在控制器中使用注入集线器。您不能像现在这样在控制器中实例化集线器,我也会将其更改为一个
Singleton
services.AddSingleton(typeof(DashboardHub));
内部仪表板中心仪表板中心
{
得到
{
返回此.HttpContext.RequestServices.GetRequiredService();
}
}
嗯,我觉得自己是个十足的新手。解决方法非常简单。您必须添加using语句,告诉控制器您要使用signar。天啊。。我几乎不好意思把这个贴出来,但希望它能帮助其他人
修正:
:facepalm您在那里犯了至少四个错误
services.AddSignalR()代码>
_hub = new DashboardHub();
_service = new NotifyService(_hub);
而是为NotifyService.cs创建一个新接口INotifyService.cs。
在Startup.cs的ConfigureServices方法中注册此服务
services.addScope()代码>
使用Microsoft.AspNetCore.signal代码>
私人只读易趣回购;
私有只读帮助程序。Convert\u Convert;
私有只读CoreContext\u上下文;
专用只读IMapper\u映射器;
私有只读INotifyService_服务;
私有只读IHubContext\u hubContext
公共MyController(IRepository repo、CoreContext context、IMapper映射器、INotifyService服务、IHubContext hubContext)
{
_convert=新助手。convert(repo、mapper);
_回购=回购;
_上下文=上下文;
_映射器=映射器;
_服务=服务;
_hubContext=hubContext;
}
[HttpPost(“更新状态”)]
公共异步任务UpdateStatus(标头){
var returnVal=等待回购变更状态(header.HeaderId,header.Status);
headerSummary=\u convert.ToReturnStatusHeader(wait\u repo.GetHeader(header.HeaderId));
//wait_service.SendNotificationAsync(headerSummary);
wait hubContext.Clients.All.SendAsync(“ReceiveMessage”,headerSummary);
返回Ok(returnVal);
}
我试试看。谢谢:)
services.AddSingleton(typeof(DashboardHub))代码>。不需要这条线@BinaraThambugala当您将请求传递给管理器时,需要这一行,因此您可以在那里注入中心。谢谢!我将进行这些额外的更改。我想那是我找到的密码。正如我所说,我已经尝试了很多事情。我更喜欢注射,不知道为什么我一开始就不这样做。我遗漏的一件大事是using语句(facepalm)
endpoints.MapHub<DashboardHub>("/Hubs/DashboardHub");
using Microsoft.AspNetCore.SignalR;
_hub = new DashboardHub();
_service = new NotifyService(_hub);
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);
}