Я создал 2 проекта. Первый проект. net core mvc приложение работает с SignalR, а второй - клиент. Таким образом, на моем сервере работает, однако, когда я хочу подключиться из console.app к signalR, я получил некоторую ошибку.
Вот коды. (Клиент)
HubConnection conn = new HubConnectionBuilder()
.WithUrl("http://localhost:5001/userHub")
.Build();
conn.StartAsync().ContinueWith(t=>{
if(t.IsFaulted)
Console.WriteLine(t.Exception.GetBaseException());
else
Console.WriteLine("Connected to Hub");
}).Wait();
Сервер:
public class UserHub : Microsoft.AspNetCore.SignalR.Hub
{
public override Task OnConnectedAsync()
{
System.Console.WriteLine($"New Connection {Context.ConnectionId}");
Clients.All.SendAsync("ReceiveMessage","New Connection Id", Context.ConnectionId);
return base.OnConnectedAsync();
}
public async Task SendMessage(string user, string message)
{
await Clients.All.SendAsync("ReceiveMessage", user, message);
}
}
Сервер (конфигурация)
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSignalR();
}
app.UseEndpoints(endpoints =>
{
endpoints.MapHub<UserHub>("/userHub");
});
введите описание изображения здесь