public class SignalRService
{
HubConnection connection;
public void ConnectToSignalR()
{
try
{
connection = new HubConnectionBuilder()
.WithUrl("http://localhost:5176/chat")
.Build();
Console.WriteLine("Connect to signalr");
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
public async Task SendMessage(string message)
{
try
{
await connection.StartAsync();
await connection.InvokeAsync("Broadcast", message);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
services.AddSingleton<SignalRService>();
public class Program
{
public static void Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
var service = host.Services.GetRequiredService<SignalRService>();
service.ConnectToSignalR();
host.Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
});
}
[HttpPost]
public async Task<IActionResult> AddVehicleAsync(Root _root)
{
<Тут вызываю метод ....SendMessage(message) >
}