从 .Net 4.8 WPF 客户端连接到 .Net Core 2.2 Web API SignalR hub 挂起

Connecting to .Net Core 2.2 Web API SignalR hub from .Net 4.8 WPF client hangs

我有一个 ASP .Net Core 2.2 Web API 和一个使用 API 的 .Net 4.8 WPF 客户端。两者都是 运行 在我的 Windows 10 PC 本地 Visual Studio 2019 社区。我正在尝试将客户端连接到 API 的 SignalR Core Hub。从服务器到客户端只有单向通信。客户端永远不会向服务器发送数据。

在服务器上我有一个模型:

public partial class Reading
{
    public int Id { get; set; }
    public int? BaseId { get; set; }
    public double? Frequency { get; set; }
    public byte? Modulation { get; set; }
    public byte? Agc1 { get; set; }
    public byte? Agc2 { get; set; }
    public DateTime TimeStamp { get; set; }
}

和强类型集线器:

public interface IChatClient
{
    Task ReceiveMessage(int id, int? baseId, double? frequency, byte? modulation, byte? agc1, byte? agc2, DateTime timeStamp);
}

public class StronglyTypedChatHub : Hub<IChatClient>
{
    public async Task SendMessage(Reading reading)
    {
        await Clients.All.ReceiveMessage(reading.Id, reading.BaseId, reading.Frequency, reading.Modulation, reading.Agc1, reading.Agc2, reading.TimeStamp);
    }

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

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

并且在 API 的控制器之一中,我想向所有连接的客户端发送数据:

[Route("api/Readings")]
public class ReadingsController : Controller
{
    private readonly DbContext _context;
    private readonly IHubContext<StronglyTypedChatHub, IChatClient> _hubContext;

    public ReadingsController(DbContext context, IHubContext<StronglyTypedChatHub, IChatClient> hubContext)
    {
        _context = context;
        _hubContext = hubContext;
    }

    [HttpPost]
    public async Task<IActionResult> PostReading([FromBody] Reading reading)
    {
        _context.Readings.Add(reading);
        await _context.SaveChangesAsync();

        await _hubContext.Clients.All.ReceiveMessage(reading.Id, reading.BaseId, reading.Frequency, reading.Modulation, reading.Agc1, reading.Agc2, reading.TimeStamp);

        return CreatedAtAction("GetReading", new { id = reading.Id }, reading);
    }

在Startup.cs中:

    services.AddSignalR(options =>
    {
        options.EnableDetailedErrors = true;
    });

    app.UseSignalR(route =>
    {
        route.MapHub<StronglyTypedChatHub>("/chathub");
    });

在我的客户端上:

    using Microsoft.AspNetCore.SignalR.Client;

    private HubConnection hubConnection { get; set; }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        hubConnection = new HubConnectionBuilder()
        .WithUrl("http://localhost:44368/chatHub")
        .Build();

        hubConnection.Closed += async (error) =>
        {
            await Task.Delay(new Random().Next(0, 5) * 1000);
            await hubConnection.StartAsync();
        };

        await hubConnection.StartAsync();

        hubConnection.On<int, int?, byte?, double?, byte?, byte?, byte?, DateTime>("SendMessage", (id, baseId, frequency, modulation, agc1, agc2, timeStamp) =>
           this.Dispatcher.Invoke(() =>
               CallSomeMethod();
           )
        ); 
    }

我配置 Visual Studio 解决方案来启动两个项目,然后按 F5。 API 运行。然后客户端运行但挂在 await hubConnection.StartAsync(); 没有错误消息或任何东西。它只是挂在那里。当 API 的控制器方法触发并将数据发送到集线器时,客户端不会收到任何东西。客户端不会触发任何内容。我猜是因为它挂在 await hubConnection.StartAsync();

我是 SignalR 的新手。任何我出错的想法都将不胜感激。谢谢!

更改客户端的处理程序 (hubConnection.On)。你注册的方法不对。在您的集线器上,您正在向所有客户端发送 "ReceiveMessage",而在您的客户端上,您注册了 "SendMessage"。选择 1,但我建议使用 "SendMessage" 因为它更有意义,因为您是从集线器发送的。

hubConnection.On<int, int?, byte?, double?, byte?, byte?, byte?, DateTime>("SendMessage"//change this, (id, baseId, frequency, modulation, agc1, agc2, timeStamp) =>
       this.Dispatcher.Invoke(() =>
           CallSomeMethod();
       )
    ); 

并且不要在 non-async 方法上使用 await。如果需要结果,请在任务上调用“.Result”。