Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 一段时间后,信号R连接状态为0_C#_Angular_Asp.net Core_Signalr_Devextreme - Fatal编程技术网

C# 一段时间后,信号R连接状态为0

C# 一段时间后,信号R连接状态为0,c#,angular,asp.net-core,signalr,devextreme,C#,Angular,Asp.net Core,Signalr,Devextreme,我正在使用@aspnet/signal连接到集线器,并从集线器调用该方法以返回一些数据 当我第一次初始化服务器时,连接正常建立,但如果我刷新窗口3或4次,客户端将停止向服务器发送连接请求 我尝试记录HUB连接,连接状态起初等于1,但问题出现后,连接状态始终等于0 以下是我如何构建HUB连接: buildConnection() { this.hubConnection = new HubConnectionBuilder() .withUrl(this.tradesService.getStoc

我正在使用
@aspnet/signal
连接到集线器,并从集线器调用该方法以返回一些数据

当我第一次初始化服务器时,连接正常建立,但如果我刷新窗口3或4次,客户端将停止向服务器发送连接请求

我尝试记录
HUB连接
连接状态
起初等于
1
,但问题出现后,
连接状态
始终等于
0

以下是我如何构建HUB连接:

buildConnection() {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.tradesService.getStockQuotationsHubUrl())
.build();
this.hubConnection.serverTimeoutInMilliseconds = 1000 * 10;
this.hubConnection.onclose(() => setTimeout(() => this.startSignalRConnection(), 2000));}
startConnection() {
    this.hubConnection
      .start()
      .then(() => {
      this.hubConnection.on('updateMethod', (data: any) => {
        
          this.store.push([
            { type: 'update', key: data.stockID, data },
          ]);
         
        });
      this.dataSource = new DataSource({
          store: this.store,
          reshapeOnPush: true,
        });
      });}
以下是我如何启动HUB连接:

buildConnection() {
this.hubConnection = new HubConnectionBuilder()
.withUrl(this.tradesService.getStockQuotationsHubUrl())
.build();
this.hubConnection.serverTimeoutInMilliseconds = 1000 * 10;
this.hubConnection.onclose(() => setTimeout(() => this.startSignalRConnection(), 2000));}
startConnection() {
    this.hubConnection
      .start()
      .then(() => {
      this.hubConnection.on('updateMethod', (data: any) => {
        
          this.store.push([
            { type: 'update', key: data.stockID, data },
          ]);
         
        });
      this.dataSource = new DataSource({
          store: this.store,
          reshapeOnPush: true,
        });
      });}
注意:我在我的页面中同时列出了5个集线器,但唯一有问题的是这个集线器

[更新]

问题来自服务器,因为当我重新启动服务器时,客户端和服务器之间的连接会重新建立,但是如果客户端多次刷新或退出页面,集线器甚至不会尝试连接到客户端

public class StockQuotationsHub : Microsoft.AspNetCore.SignalR.Hub
  {
    private string SectorID { get; set; }
    private int TradingSession { get; set; }
    private int MarketID { get; set; }

public static StockQuotationExt stockQuotationExt { get; set; }
private readonly StockQuotationTicker _stockTicker;

public StockQuotationsHub(StockQuotationTicker stockTicker){
    _stockTicker = stockTicker;
}

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

public IEnumerable<StockQuotation> GetAllStockQuotations(string[] stockID, string sectorID, int tradingSession, int marketType){
    return _stockTicker.
    GetAllStocks(Context.ConnectionId, stockID, sectorID, tradingSession, marketType);
        }

public override async Task OnDisconnectedAsync(Exception exception){
    await base.OnDisconnectedAsync(exception);
}
公共类股票报价shub:Microsoft.AspNetCore.signal.Hub
{
私有字符串SectorID{get;set;}
private int TradingSession{get;set;}
私有数据{get;set;}
公共静态StockQuotationExt StockQuotationExt{get;set;}
私人只读股票报价器;
公开股票报价书{
_stockTicker=stockTicker;
}
公共覆盖任务OnConnectedAsync(){
返回base.OnConnectedAsync();
}
public IEnumerable GetAllStockQuotes(字符串[]stockID,字符串sectorID,int tradingSession,int marketType){
返回股票代码。
GetAllStocks(Context.ConnectionId、stockID、sectorID、tradingSession、marketType);
}
公共覆盖异步任务OnDisconnectedAsync(异常){
等待base.OnDisconnectedAsync(异常);
}
这是我的股票代码课:

public IEnumerable<StockQuotation> GetAllStocks(string connectionId, string[] stockID, string sectorID, int tradingSession, int marketType)
    {
        _stocks = new List<StockQuotation>();
        _stocks = Task.Run(async () => await GetStockQuotationModelAsync("", 0, 1, 0)).Result.ToList();
        this.MaxTimeStamp = _stocks.Max(s => s.TStamp);
        this.SectorID = sectorID;
        this.TradingSession = tradingSession;
        this.MarketID = marketType;

        AddToGroups(connectionId, stockID);

        if (_timer==null)
            _timer = new Timer(UpdateStockPrices, null, _updateInterval, _updateInterval);

        if (stockID.Length == 0)
        {
            return _stocks;
        }
        else
        {
            var stocksList = new List<StockQuotation>();
            foreach (var stock in stockID)
            {
                stocksList.AddRange(_stocks.Where(s => s.StockID == stock).ToList());
            }
            return stocksList;
        }
    }

private void AddToGroups(string connectionId, string[] stockID)
        {
            if (_stocks.Count > 0)
            {
                if (stockID.Length == 0)
                {
                    Hub.Groups.AddToGroupAsync(connectionId, "ALL");
                }
                else
                {
                    foreach (var stock in stockID)
                    {
                        Hub.Groups.AddToGroupAsync(connectionId, stock);
                        var s = _stocks.FirstOrDefault(s => s.StockID == stock);
                        if(s != null)
                        {
                        s.Snapshots = new List<double>(GetStockQuotationSnapshots(stock));
                        }
                    }
                }
            }
        }
private void AddToGroups(string connectionId, string[] stockID)
        {
            if (_stocks.Count > 0)
            {
                if (stockID.Length == 0)
                {
                    Hub.Groups.AddToGroupAsync(connectionId, "ALL");
                }
                else
                {
                    foreach (var stock in stockID)
                    {
                        Hub.Groups.AddToGroupAsync(connectionId, stock);
                        var s = _stocks.FirstOrDefault(s => s.StockID == stock);
                        if(s != null)
                        {
                        s.Snapshots = new List<double>(GetStockQuotationSnapshots(stock));
                        }
                    }
                }
            }
        }
public IEnumerable GetAllStocks(字符串connectionId、字符串[]stockID、字符串sectorID、int tradingSession、int marketType)
{
_股票=新名单();
_stocks=Task.Run(async()=>await-GetStockQuotationModelAsync(“,0,1,0)).Result.ToList();
this.MaxTimeStamp=\u stocks.Max(s=>s.TStamp);
this.SectorID=SectorID;
this.TradingSession=TradingSession;
this.MarketID=市场类型;
AddToGroup(connectionId、stockID);
如果(_timer==null)
_计时器=新计时器(UpdateStockPrices,null,_updateInterval,_updateInterval);
if(stockID.Length==0)
{
股票收益率;
}
其他的
{
var stocksList=新列表();
foreach(库存ID中的var库存)
{
stocksList.AddRange(_stocks.Where(s=>s.StockID==stock.ToList());
}
返回股票列表;
}
}
私有void addToGroup(字符串connectionId,字符串[]stockID)
{
如果(_stocks.Count>0)
{
if(stockID.Length==0)
{
Hub.Groups.AddToGroupAsync(connectionId,“ALL”);
}
其他的
{
foreach(库存ID中的var库存)
{
Hub.Groups.AddToGroupAsync(connectionId,stock);
var s=_stocks.FirstOrDefault(s=>s.StockID==stock);
如果(s!=null)
{
s、 快照=新列表(GetStockQuotationSnapshots(stock));
}
}
}
}
}
私有void addToGroup(字符串connectionId,字符串[]stockID)
{
如果(_stocks.Count>0)
{
if(stockID.Length==0)
{
Hub.Groups.AddToGroupAsync(connectionId,“ALL”);
}
其他的
{
foreach(库存ID中的var库存)
{
Hub.Groups.AddToGroupAsync(connectionId,stock);
var s=_stocks.FirstOrDefault(s=>s.StockID==stock);
如果(s!=null)
{
s、 快照=新列表(GetStockQuotationSnapshots(stock));
}
}
}
}
}

我真的很感谢您的帮助。

最终问题是项目类型是
MVC
,所以我将其更改为
webApi

即使你修复了它,一切都成功了。我建议你添加以下代码,以便准确地知道断开连接的根本原因

首先,在服务配置中启用扩展调试信息:

services.AddSignalR(o =>
            {
                o.EnableDetailedErrors = true;
            });
此外,您还需要附加到HubConnection中关闭的事件,如下所示:

hubConnection.Closed += (exception) =>
            {
                if (exception == null)
                {
                    Console.WriteLine("Connection closed without error.");
                }
                else
                {
                    Console.WriteLine($"Connection closed due to an error: {exception}");
                }
                return null;
            };

首先,请将您的包从
@aspnet/signal
更新为
@microsoft/signal
,因为这是新包,而旧包现在不受支持。您是否正在破坏ngDestroy hook的连接?如果没有,您将尝试破坏它。我是否调用
this.hubConnection.stop()
或只需执行
this.hubConnection=null
?@Kiril1512感谢您的建议,我确实升级到了
@microsoft/signal
,但问题仍然存在same@Kardon63是的,这不会解决问题,但至少您正在使用最新版本的Signal,现在您还可以使用AutomaticReconnect功能。