I'm using .net core 5 with a simple setup:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddSignalR();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<ChatHub>("/chathub");
endpoints.MapControllers();
});
}
With this simple Hub :
public interface IChatClient
{
Task SendMessage( string message);
}
public class ChatHub : Hub<IChatClient>
{
public Task SendMessage(string message)
{
return Clients.All.SendMessage( message);
}
}
This is my simple controller :
public class WeatherForecastController : ControllerBase
{
private readonly IHubContext<ChatHub> _hubContext;
public WeatherForecastController(IHubContext<ChatHub> hubContext)
{
_hubContext = hubContext;
}
[HttpGet]
public async Task<IActionResult> Get()
{
await _hubContext.Clients.All.SendAsync("ReceiveMessage", $"Home page loaded at: {DateTime.Now}");
return Ok();
}
}
(For simplicity, whenever the GET HTTP endpoint is invoked, a message should be sent).
The C# console client is :
async Task Main()
{
var uri = "http://localhost:5000/chatHub";
await using var connection = new HubConnectionBuilder().WithUrl(uri).Build();
await connection.StartAsync();
connection.On<string>("ReceiveMessage", (message) => Console.WriteLine(message));
}
Question:
Both apps are working but I do not see any message in the client-side when I'm refreshing the page. I might be missing something here.