diff --git a/JetHerald/JetHeraldBot.cs b/JetHerald/JetHeraldBot.cs index 0e1ff15..c607b5b 100644 --- a/JetHerald/JetHeraldBot.cs +++ b/JetHerald/JetHeraldBot.cs @@ -3,10 +3,11 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System.Threading; +using Microsoft.Extensions.Hosting; namespace JetHerald { - public partial class JetHeraldBot + public partial class JetHeraldBot : IHostedService { Db Db { get; set; } Options.Telegram TelegramConfig { get; } @@ -29,13 +30,13 @@ namespace JetHerald CancellationTokenSource HeartbeatCancellation; Task HeartbeatTask; - public async Task Init() + public async Task StartAsync(CancellationToken token) { await InitTelegram(); await InitDiscord(); } - public async Task Stop() + public async Task StopAsync(CancellationToken token) { await DiscordBot.DisconnectAsync(); TelegramBot.StopReceiving(); @@ -103,5 +104,6 @@ namespace JetHerald } catch (Exception e) { Log.LogError(e, $"Error while sending message \"{formatted}\" to {chat}"); } } + } } diff --git a/JetHerald/Startup.cs b/JetHerald/Startup.cs index 68de981..8c3121a 100644 --- a/JetHerald/Startup.cs +++ b/JetHerald/Startup.cs @@ -25,6 +25,7 @@ namespace JetHerald services.Configure(Configuration.GetSection("Timeout")); services.AddSingleton(); services.AddSingleton(); + services.AddHostedService(s => s.GetService()); services.AddSingleton(); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0); @@ -33,8 +34,6 @@ namespace JetHerald // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { - var bot = app.ApplicationServices.GetService(); - bot.Init().GetAwaiter().GetResult(); app.UsePathBase(Configuration.GetValue("PathBase")); if (env.IsDevelopment()) {