简体   繁体   English

如何将新的/更新的数据发送到SignalR for NET Core 2.2上的所有客户端

[英]How to send new/updated data to all clients on SignalR for NET Core 2.2

I'm not too familiar with signalr2 on asp.net-core pardon me, am trying to create a POC on how to implement a real-time CRUD application with admin and client using signalr2, but am having issues with signalr data push, I keep getting NullReferenceException: Object reference not set to an instance of an object. 我对asp.net-core上的signalr2不太熟悉,请原谅我如何使用signalr2创建一个关于如何使用admin和客户端实现实时CRUD应用程序的POC,但是我遇到了信号器数据推送的问题,我继续获取NullReferenceException: Object reference not set to an instance of an object. on this line await Clients.All.SendAsync("BroadcastData", data); 在这一行await Clients.All.SendAsync("BroadcastData", data);

Below is how I setup my Hub using some online examples I looked-up: 以下是我使用我查找的一些在线示例设置Hub的方法:

public class OddPublisher : Hub
    {

        private readonly IOddServices _oddService;

        public OddPublisher(IOddServices oddService)
        {
            _oddService = oddService;
        }

        public async Task BroadcastData()
        {
            var data = _oddService.ClientQueryOdds();
            await Clients.All.SendAsync("BroadcastData", data); //breaks here
        }

    }

and this is triggered by admin, on submiting and saving the data sucessfully I call the BroadcastData() 这是由管理员触发的,在成功提交和保存数据时我调用了BroadcastData()

    public class BaseController : Controller
    {
        public IOddServices _oddService;
        public readonly OddPublisher _publisher;
        public BaseController(IOddServices oddService, )
        {
            _oddService = oddService;
            _teamService = teamService;

            _publisher = new OddPublisher(oddService);
        }


        [HttpPost]
        public async Task<IActionResult> odd_entry(CreateOdd dto)
        {
            //somecode here...

            var results = _validator.Validate(dto);

            if(!results.IsValid)
            {
                results.AddToModelState(ModelState, null);

                return View(dto);
            }

            _oddService.CreateOddAndTeam(dto);

            await _publisher.BroadcastData(); //Breaks

            return RedirectToAction(nameof(index));
        }
    }

Folled all the instructions as adviced in Microsoft Asp.Net Core Signalr Documentation, my Startup has all required sevices added. 根据Microsoft Asp.Net核心信号器文档中提供的所有说明,我的Startup已添加了所有必需的服务。

here is the client and the JS file, 这是客户端和JS文件,

"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/oddPublisher").build();

connection.on("BroadcastData", data => {

    console.table(data);
    //getAll();
})


connection.start().then(function () {
    getAll();
}).catch(function (err) {
    return console.error(err.toString());
});


function getAll() {
    var model = $('#dataModel');
    $.ajax({
        url: '/home/GetLatestOddData',
        contentType: 'application/html ; charset:utf-8',
        type: 'GET',
        dataType: 'html',
        success: function (result) { model.empty().append(result); }
    });
}

Need some help here guys, I still don't know what am doing wrong, I'll really appreciate if I can get any help on this. 在这里需要一些帮助的人,我仍然不知道我做错了什么,如果我能得到任何帮助,我将非常感激。

Thank you in advance. 先感谢您。

事实证明我需要将IHubContext注入我的集线器才能访问客户端。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM