Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Greetings from Cairo, Egypt. You can [sponsor](https://github.com/sponsors/dodyg
| [gRPC](/projects/grpc) (including grpc-Web) | 12 | |
| [Health Check](/projects/health-check) | 6 | |
| [IHttpClientFactory](/projects/httpclientfactory) | 4 | |
| [IHostedLifeCycleService](/projects/i-hosted-lifecycle-service) | 1 | .NET8 |
| [IHostedService](/projects/ihosted-service) | 2 | |
| [Logging](/projects/logging) | 4 | |
| [Localization and Globalization](/projects/localization) | 6 | |
Expand Down
70 changes: 70 additions & 0 deletions projects/i-hosted-lifecycle-service/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Console;
using Microsoft.Extensions.DependencyInjection;

HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddHostedService<ExampleHostedService>();
builder.Services.AddLogging(opt =>
{
opt.AddSimpleConsole(c =>
{
c.SingleLine = true;
c.TimestampFormat = "[HH:mm:ss] ";
});
});

using IHost host = builder.Build();

//Host will issue a cancel token 10 seconds after StartedAsync()
await host.RunAsync((new CancellationTokenSource(10000)).Token);

public class ExampleHostedService : IHostedLifecycleService
{
private readonly ILogger _logger;

public ExampleHostedService(ILogger<ExampleHostedService> logger){
_logger = logger;
}

public async Task StartingAsync(CancellationToken cancellationToken){

_logger.LogInformation("Step #1: StartingAsync, will take 5 seconds");

//simulate the delay of starting the service up.
await Task.Delay(5000);

_logger.LogInformation("Step #2: End StartingAsync");
}

public async Task StartAsync(CancellationToken cancellationToken){
_logger.LogInformation("Step #3: StartAsync");
await Task.Yield();
}

public async Task StartedAsync(CancellationToken cancellationToken){
_logger.LogInformation("Step #4: StartedAsync");
await Task.Yield();
}

public async Task StoppingAsync(CancellationToken cancellationToken){

_logger.LogInformation("Step #5: StoppingAsync, will take 2 seconds");

//simulate delay when gracefully stopping the service.
cancellationToken.WaitHandle.WaitOne(2000);

_logger.LogInformation("Step #6: End StoppingAsync");
await Task.Yield();
}

public async Task StopAsync(CancellationToken cancellationToken){
_logger.LogInformation("Step #7: StopAsync");
await Task.Yield();
}

public async Task StoppedAsync(CancellationToken cancellationToken){
_logger.LogInformation("Step #8: StoppedAsync");
await Task.Yield();
}
}
6 changes: 6 additions & 0 deletions projects/i-hosted-lifecycle-service/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# IHostedLifeCycleService

Show example show new lifecycle events on top of what already available in IHostedService.

dotnet8

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>true</ImplicitUsings>
</PropertyGroup>
</Project>