5

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 エンドポイントは問題ありませんが、接続を確立できません。私は何が欠けていますか?

4

1 に答える 1