I am trying to write a console application that receives changes from my website and does something when triggered. I got it to connect and work just fine, however should the website go down for any reason and for any duration of time, the client never reconnects. Here is my client code:
public class MyBot
{
HubConnection connection;
public async Task RunAsync()
{
try
{
connection = new HubConnectionBuilder()
.WithUrl("https://localhost:7178/MyHub")
.WithAutomaticReconnect()
.Build();
connection.Closed += async (error) =>
{
Console.WriteLine("Connection Lost");
await Task.Delay(new Random().Next(0, 5) * 1000);
await connection.StartAsync();
await connection.InvokeAsync("JoinBots");
};
connection.On<string, string>("DoSomething", (arg1, arg2) =>
{
//Do Something
});
await connection.StartAsync();
await connection.InvokeAsync("JoinBots");
}
catch
{
Console.WriteLine("Failed to connect to Website");
}
}
}
In my console the "Connection Lost" never is written. If fact none of the catch even fire. What am I doing wrong?