From f7123ccc332d778cdf92431bf1a2e08aa2009eef Mon Sep 17 00:00:00 2001 From: am phan Date: Tue, 7 May 2024 23:28:14 -0400 Subject: [PATCH 1/2] Code sample for IHostedLifecycleService --- .../i-hosted-lifecycle-service/Program.cs | 71 +++++++++++++++++++ .../i-hosted-lifecycle-service.csproj | 6 ++ 2 files changed, 77 insertions(+) create mode 100644 projects/i-hosted-lifecycle-service/Program.cs create mode 100644 projects/i-hosted-lifecycle-service/i-hosted-lifecycle-service.csproj diff --git a/projects/i-hosted-lifecycle-service/Program.cs b/projects/i-hosted-lifecycle-service/Program.cs new file mode 100644 index 00000000..f8bc518b --- /dev/null +++ b/projects/i-hosted-lifecycle-service/Program.cs @@ -0,0 +1,71 @@ +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(); +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 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(); + } +} \ No newline at end of file diff --git a/projects/i-hosted-lifecycle-service/i-hosted-lifecycle-service.csproj b/projects/i-hosted-lifecycle-service/i-hosted-lifecycle-service.csproj new file mode 100644 index 00000000..eb2d835a --- /dev/null +++ b/projects/i-hosted-lifecycle-service/i-hosted-lifecycle-service.csproj @@ -0,0 +1,6 @@ + + + net8.0 + true + + \ No newline at end of file From ef46743408019de07822afee57a629460070b02c Mon Sep 17 00:00:00 2001 From: am phan Date: Tue, 7 May 2024 23:47:26 -0400 Subject: [PATCH 2/2] modified formats and add readme. --- README.md | 1 + projects/i-hosted-lifecycle-service/Program.cs | 13 ++++++------- projects/i-hosted-lifecycle-service/README.md | 6 ++++++ 3 files changed, 13 insertions(+), 7 deletions(-) create mode 100644 projects/i-hosted-lifecycle-service/README.md diff --git a/README.md b/README.md index e582ceab..2e13b1db 100644 --- a/README.md +++ b/README.md @@ -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 | | diff --git a/projects/i-hosted-lifecycle-service/Program.cs b/projects/i-hosted-lifecycle-service/Program.cs index f8bc518b..303c8ca4 100644 --- a/projects/i-hosted-lifecycle-service/Program.cs +++ b/projects/i-hosted-lifecycle-service/Program.cs @@ -6,13 +6,13 @@ HostApplicationBuilder builder = Host.CreateApplicationBuilder(args); builder.Services.AddHostedService(); builder.Services.AddLogging(opt => - { - opt.AddSimpleConsole(c => - { - c.SingleLine = true; - c.TimestampFormat = "[HH:mm:ss] "; - }); +{ + opt.AddSimpleConsole(c => + { + c.SingleLine = true; + c.TimestampFormat = "[HH:mm:ss] "; }); +}); using IHost host = builder.Build(); @@ -44,7 +44,6 @@ public async Task StartAsync(CancellationToken cancellationToken){ public async Task StartedAsync(CancellationToken cancellationToken){ _logger.LogInformation("Step #4: StartedAsync"); - await Task.Yield(); } diff --git a/projects/i-hosted-lifecycle-service/README.md b/projects/i-hosted-lifecycle-service/README.md new file mode 100644 index 00000000..8757d092 --- /dev/null +++ b/projects/i-hosted-lifecycle-service/README.md @@ -0,0 +1,6 @@ +# IHostedLifeCycleService + +Show example show new lifecycle events on top of what already available in IHostedService. + +dotnet8 +