I've got an ASP .Net Core 2.2 Web API with a .Net 4.8 WPF client consuming the API. Both are running locally on my Windows 10 PC in Visual Studio 2019 Community. I'm trying to connect the client to the API's SignalR Core Hub. There is only one-way communication from the server to the clients. The clients will never send data to the server.
On the server I have a model:
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; }
}
and a strongly typed hub:
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);
}
}
And in one of the API's controllers, I want to send data to all connected clients:
[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);
}
And in Startup.cs:
services.AddSignalR(options =>
{
options.EnableDetailedErrors = true;
});
app.UseSignalR(route =>
{
route.MapHub<StronglyTypedChatHub>("/chathub");
});
On the client I have:
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();
)
);
}
I configure the Visual Studio solution to start up both projects and I hit F5. The API runs. Then the client runs but hangs on await hubConnection.StartAsync();
No error messages or anything. It just hangs there. When the API's controller method triggers, and sends data to the hub, the client doesn't receive anything. Nothing is triggered on the client. I guess because it's hung on await hubConnection.StartAsync();
I'm brand new to SignalR. Any ideas where I'm going wrong would be greatly appreciated. Thank you!