I want to create a Discord bot and both DSharpPlus and Discord.Net use Console applications in their tutorials.
I would like to know if there are any reasons for this because when thinking about it a worker service application seems to fit better, no? I can setup the bot in the StartAsync
method and if needed I can dispose all the things in the StopAsync
method.
So does someone know why console applications are preferred? Perhaps worker services have disadvantages that I am not currently aware of?
Maybe the bot will struggle with the ExecuteAsync
method?
public class Worker : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(1000, stoppingToken);
}
}
}
So does someone know why console applications are preferred? Perhaps worker services have disadvantages that I am not currently aware of?
.NET Code declares a cross-platform support that means you can use the same sources for different target platforms. Because of that, .NET have to use kind of "service" idea for long running background things but without a bind to specific platform. On Linux any console application could be a "service" application (have a look: https://swimburger.net/blog/dotnet/how-to-run-a-dotnet-core-console-app-as-a-service-using-systemd-on-linux). On Windows it is a bit different, but still quite simple (have a look: https://dotnetcoretutorials.com/2019/12/07/creating-windows-services-in-net-core-part-3-the-net-core-worker-way/.
Maybe the bot will struggle with the ExecuteAsync method?
BackgroundService
class is designed as a base class for any "service", so, I think, there is no better candidate to extend :)