简体   繁体   English

AspNetCore.SignalR:无法启动未处于Initial状态的连接

[英]AspNetCore.SignalR : Cannot start a connection that is not in the Initial state

I have troubles to get my ASP.NET Core SignalR app working. 我有麻烦让我的ASP.NET Core SignalR应用程序工作。

I have this server-side code : 我有这个服务器端代码:

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);
    }
}

whose SignalR Hub service is configured as : 其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");
    });

    ...
}

In my client-side (WPF app), I have a service : 在我的客户端(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();
    }
}

My issue is that, when I call Start() method, I get the exception "Cannot start a connection that is not in the Initial state". 我的问题是,当我调用Start()方法时,我得到异常“无法启动未处于初始状态的连接”。 The issue occurs either locally or in Azure. 该问题发生在本地或Azure中。

The SignalR endpoint is fine but no connection can be established. SignalR端点很好,但无法建立连接。 What am I missing? 我错过了什么?

You seem to be trying to start a connection that has already been started. 您似乎正在尝试启动已经启动的连接。 Also note that as of alpha2 the connection is not restartable - ie if it is stopped you cannot restart it - you need to create a new connection. 另请注意,从alpha2开始,连接不可重新启动 - 即如果它已停止,则无法重新启动 - 您需要创建新连接。

EDIT 编辑

In post alpha2 versions the connection will be restartable. 在后alpha2版本中,连接将可重新启动。

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

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