Mango.Nop.Libraries/Mango.Nop.Services/MgBackgroundServiceBase.cs

37 lines
1.3 KiB
C#

using Microsoft.Extensions.Hosting;
using Nop.Services.Logging;
namespace Mango.Nop.Services;
public abstract class MgBackgroundServiceBase(ILogger logger, IServiceProvider service) : BackgroundService
{
protected abstract Task OnExecuteAsync();
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
try
{
await logger.InformationAsync($"MgBackgroundServiceBase.ExecuteAsync(); Processing ExecuteAsync");
await OnExecuteAsync();
}
catch (Exception ex)
{
await logger.ErrorAsync($"MgBackgroundServiceBase.ExecuteAsync(); Failure while processing ExecuteAsync", ex, null);
}
}
}
public override async Task StartAsync(CancellationToken cancellationToken)
{
await logger.InformationAsync("MgBackgroundServiceBase.ExecuteAsync(); Starting MgBackgroundServiceBase");
await base.StartAsync(cancellationToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
await logger.InformationAsync("MgBackgroundServiceBase.StopAsync(); Stopping MgBackgroundServiceBase");
await base.StopAsync(cancellationToken);
}
}