Add background service and update project configuration
Added a `Worker` class as a hosted background service to log periodic messages. Updated `DigitalData.EmailProfiler.API.csproj` to include `UserSecretsId` for secure development storage and added `Microsoft.Extensions.Hosting` package. Replaced the `Controllers` folder reference with `Properties`. Updated `Program.cs` to register the `Worker` service, enable API exploration, and retain Swagger configuration.
This commit is contained in:
@@ -4,14 +4,16 @@
|
|||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<UserSecretsId>dotnet-DigitalData.EmailProfiler.Service-e7ef6a9a-436f-48a1-b4f6-ec9381c8cb47</UserSecretsId>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Controllers\" />
|
<Folder Include="Properties\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
var builder = WebApplication.CreateBuilder(args);
|
using DigitalData.EmailProfiler.API;
|
||||||
|
|
||||||
// Add services to the container.
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
builder.Services.AddHostedService<Worker>();
|
||||||
|
|
||||||
builder.Services.AddControllers();
|
builder.Services.AddControllers();
|
||||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen();
|
builder.Services.AddSwaggerGen();
|
||||||
|
|
||||||
|
|||||||
16
src/DigitalData.EmailProfiler.API/Worker.cs
Normal file
16
src/DigitalData.EmailProfiler.API/Worker.cs
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
namespace DigitalData.EmailProfiler.API;
|
||||||
|
|
||||||
|
public class Worker(ILogger<Worker> Logger) : BackgroundService
|
||||||
|
{
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
while (!stoppingToken.IsCancellationRequested)
|
||||||
|
{
|
||||||
|
if (Logger.IsEnabled(LogLevel.Information))
|
||||||
|
{
|
||||||
|
Logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||||
|
}
|
||||||
|
await Task.Delay(1000, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user