【问题标题】:Connecting to .Net Core 2.2 Web API SignalR hub from .Net 4.8 WPF client hangs从 .Net 4.8 WPF 客户端连接到 .Net Core 2.2 Web API SignalR 集线器挂起
【发布时间】:2019-06-11 15:23:50
【问题描述】:

我有一个 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 的新手。任何我出错的想法将不胜感激。谢谢!

【问题讨论】:

  • 你的代码使用了await,但是它所在的方法没有标记async,所以这不会编译。实际代码在做什么?
  • 谢谢斯蒂芬,我完全错过了。我今晚会试试,让你知道......

标签: .net wpf signalr asp.net-core-webapi signalr.client


【解决方案1】:

在客户端更改您的处理程序 (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();
       )
    ); 

并且不要在非异步方法上使用 await。如果您需要结果,请在任务上调用“.Result”。

【讨论】:

  • 谢谢卢克,这是有道理的。我今晚会试试,让你知道......
  • 没问题。我希望它能解决您的问题。
猜你喜欢
  • 2019-10-07
  • 2019-12-16
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
  • 2015-08-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多