Register NotificationService as HostedService so it instantiates early

This commit is contained in:
Mr-Quin 2024-03-29 20:15:29 -07:00
parent 8c025feaba
commit 4f9424ff3d
2 changed files with 15 additions and 2 deletions

View File

@ -95,6 +95,7 @@ public partial class App : Application
// My Services
services.AddSingleton<TaskTriggerDispatcher>();
services.AddSingleton<NotificationService>();
services.AddHostedService<NotificationService>(sp => sp.GetRequiredService<NotificationService>());
services.AddSingleton<NotifierManager>();
// Configuration

View File

@ -9,10 +9,12 @@ using BetterGenshinImpact.Service.Notifier;
using BetterGenshinImpact.Service.Notifier.Exception;
using BetterGenshinImpact.Service.Notifier.Interface;
using System.Text.Json;
using Microsoft.Extensions.Hosting;
using System.Threading;
namespace BetterGenshinImpact.Service.Notification;
public class NotificationService
public class NotificationService : IHostedService
{
private static NotificationService? _instance;
@ -37,6 +39,16 @@ public class NotificationService
return _instance;
}
public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
private HttpContent TransformData(INotificationData notificationData)
{
// using object type here so it serializes the interface correctly
@ -84,4 +96,4 @@ public class NotificationService
{
await _notifierManager.SendNotificationToAllAsync(TransformData(notificationData));
}
}
}