![](/img/trans.png)
[英]How to handle "InvalidOperationException Sequence contains no elements"
[英]SignalR - Client connection closes straight after starting - InvalidOperationException: Sequence contains no elements
我有一個 SignalR 客戶端,它似乎在啟動后立即關閉,我得到的錯誤消息是:
“服務器關閉連接,出現以下錯誤:連接關閉,出現錯誤。InvalidOperationException:序列不包含元素”
SignalR 客戶端用於 ASP.Net Core Web API 項目(在 API 控制器內)。
我使用的 Nuget 包稱為 Microsoft.AspNetCore.SignalR.Client (v 1.1.0)
我的代碼如下所示:
try
{
//SEND MESSAGE TO HUB
var connection = new HubConnectionBuilder()
.WithUrl("https://sample.azurewebsites.net/ChatHub")
.Build();
connection.Closed += async (error) =>
{
//log error - this is getting called straight after StartAsync
};
await connection.StartAsync();
await connection.InvokeAsync("SendToATeam", "x", "y");
await connection.StopAsync();
}
catch (Exception ex)
{
//log error
}
在您的服務器上,您可以通過以下方式打開詳細錯誤:
services.AddSignalR(o =>
{
o.EnableDetailedErrors = true;
})
這將在客戶端上為您提供更詳細的錯誤消息
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.