Dec 04 2023
public class SomeService : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Start Service");
await Task.Delay(60000); //1 minute
}
public async Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Stop Service");
}
}
builder.Services.AddHostedService<SomeService>();
builder.Services.Configure<HostOptions>(options =>
{
options.ServicesStartConcurrently = true;
options.ServicesStopConcurrently = true;
});
builder.Services.AddHostedService<SomeService>();
public class SomeService : IHostedLifecycleService
{
public async Task StartAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Start Service");
}
public async Task StartedAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Started Service");
}
public async Task StartingAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Starting Service");
}
public async Task StopAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Stop Service");
}
public async Task StoppedAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Stopped Service");
}
public async Task StoppingAsync(CancellationToken cancellationToken)
{
Console.WriteLine("Stopping Service");
}
}
Join 13,250+ subscribers to improve your .NET Knowledge.
Go-to resource for understanding the core concepts of design patterns without the overwhelming complexity. In this concise and affordable ebook, I've distilled the essence of design patterns into an easy-to-digest format. It is a Beginner level. Check out it here.
Every Monday morning, I share 1 actionable tip on C#, .NET & Arcitecture topic, that you can use right away.
Subscribe to the TheCodeMan.net and be among the 13,250+ subscribers gaining practical tips and resources to enhance your .NET expertise.