无法使用SignalR从数据库获取更新的值

时间:2019-03-29 10:06:37

标签: c# angularjs dependency-injection signalr

我正在研究.Net Core和基于angular js的项目。我正在使用signalr从数据库中获取数据并将其显示在UI中。我添加了一个计时器以从数据库中获取更新的数据,并每20秒使用signalr将其推送到客户端。 错误:

  

无法访问已处置的对象

。当计时器执行已定义的操作时,出现此错误。

Angular js

initSignalRConnection(){
    this._hubConnection = new HubConnectionBuilder()
    .configureLogging(signalR.LogLevel.Debug)
    .withUrl(url), {
      skipNegotiation: true,
      transport: signalR.HttpTransportType.WebSockets
    })
    .build();

  this._hubConnection
    .start()
    .then(() => console.log('Connection started!'))
    .catch(err => console.log('Error while establishing connection :('));       
    this._hubConnection.on("SendData", (response) => {                  
        console.log(response);
    });
}

C#:

    public async Task<IActionResult> Get()
    {
        try
        {              
            var timer = new TimerManager(() => _hub.Clients.All.SendAsync("SendData",  FetchDashboardData(csqname))); --> here i'm getting the error which i mentioned above
            return Ok(new { Message = "Request Completed" });
        }
        catch (Exception ex)
        {
            Logger.LogError($"Error on Get Dashboard data Details {ex.Message}, {ex} ");
            return BadRequest();
        }
    }

TimerManager:

  public class TimerManager
  {
    private S.Timer _timer;
    private S.AutoResetEvent _autoResetEvent;
    private Action _action;

    public DateTime TimerStarted { get; }

    public TimerManager(Action action)
    {
        _action = action;
        _autoResetEvent = new S.AutoResetEvent(false);
        _timer = new S.Timer(Execute, _autoResetEvent, 1000, 20000);
        TimerStarted = DateTime.Now;
    }

    public void Execute(object stateInfo)
    {
        _action();
    }
}

0 个答案:

没有答案