gpt4 book ai didi

c# - AspNetCore.SignalR : Cannot start a connection that is not in the Initial state

转载 作者:行者123 更新时间:2023-11-30 12:08:53 26 4
gpt4 key购买 nike

我的 ASP.NET Core SignalR 应用无法正常工作。

我有这个服务器端代码:

public class PopcornHub : Hub
{
private int Users;

public async Task BroadcastNumberOfUsers(int nbUser)
{
await Clients.All.InvokeAsync("OnUserConnected", nbUser);
}

public override async Task OnConnectedAsync()
{
Users++;
await BroadcastNumberOfUsers(Users);
await base.OnConnectedAsync();
}

public override async Task OnDisconnectedAsync(Exception exception)
{
Users--;
await BroadcastNumberOfUsers(Users);
await base.OnDisconnectedAsync(exception);
}
}

其 SignalR Hub 服务配置为:

public void ConfigureServices(IServiceCollection services)
{
...
services.AddSignalR();
...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...

app.UseSignalR(routes =>
{
routes.MapHub<PopcornHub>("popcorn");
});

...
}

在我的客户端(WPF 应用程序)中,我有一项服务:

public class PopcornHubService : IPopcornHubService
{
private readonly HubConnection _connection;

public PopcornHubService()
{
_connection = new HubConnectionBuilder()
.WithUrl($"{Utils.Constants.PopcornApi.Replace("/api", "/popcorn")}")
.Build();
_connection.On<int>("OnUserConnected", (message) =>
{

});
}

public async Task Start()
{
await _connection.StartAsync();
}
}

我的问题是,当我调用 Start() 方法时,出现异常“无法启动不处于初始状态的连接”。该问题发生在本地或 Azure 中。

SignalR 端点正常,但无法建立连接。我错过了什么?

最佳答案

您似乎正在尝试启动一个已经启动的连接。另请注意,从 alpha2 开始,连接不可重新启动 - 即,如果它已停止,您将无法重新启动它 - 您需要创建一个新连接。

编辑

在 alpha2 之后的版本中,连接将可以重新启动。

关于c# - AspNetCore.SignalR : Cannot start a connection that is not in the Initial state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46492131/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com