Skip to content

Commit

Permalink
prevent dispose errors from causing ungraceful shutdowns
Browse files Browse the repository at this point in the history
  • Loading branch information
loukylor committed Dec 21, 2024
1 parent 93c025c commit 11cadbb
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion TrickFireDiscordBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ private static async Task Main(string[] args)
return;
}

ILogger logger = host.Services.GetRequiredService<ILogger<Program>>();

try
{
await host.StartAsync();
Expand All @@ -44,9 +46,20 @@ private static async Task Main(string[] args)
{
host.Services.GetService<DiscordClient>()?.Dispose();
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to dispose of discord client on shutdown");
}
finally
{
host.Dispose();
try
{
host.Dispose();
}
catch (Exception ex)
{
logger.LogError(ex, "Failed to dispose of host on shutdown");
}
}
}
}
Expand Down

0 comments on commit 11cadbb

Please sign in to comment.