{"id":17197183,"url":"https://github.com/thomaslevesque/extensions.hosting.asyncinitialization","last_synced_at":"2025-04-09T20:12:39.559Z","repository":{"id":35032200,"uuid":"198643019","full_name":"thomaslevesque/Extensions.Hosting.AsyncInitialization","owner":"thomaslevesque","description":"Async initialization for .NET 6.0+ apps using the generic host (e.g. ASP.NET Core apps)","archived":false,"fork":false,"pushed_at":"2024-02-26T02:56:23.000Z","size":56,"stargazers_count":50,"open_issues_count":1,"forks_count":7,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-09T20:12:33.785Z","etag":null,"topics":["asp-net-core","async-initialization","csharp","dotnet","generic-host"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thomaslevesque.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-07-24T13:39:49.000Z","updated_at":"2025-03-12T12:45:43.000Z","dependencies_parsed_at":"2025-01-31T17:37:29.592Z","dependency_job_id":"c309a951-d5c9-4923-911a-c1ef231fd38e","html_url":"https://github.com/thomaslevesque/Extensions.Hosting.AsyncInitialization","commit_stats":{"total_commits":12,"total_committers":3,"mean_commits":4.0,"dds":"0.33333333333333337","last_synced_commit":"4820a0c9d8319ecec8f31d8ec3ab2442e43c2851"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FExtensions.Hosting.AsyncInitialization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FExtensions.Hosting.AsyncInitialization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FExtensions.Hosting.AsyncInitialization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thomaslevesque%2FExtensions.Hosting.AsyncInitialization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thomaslevesque","download_url":"https://codeload.github.com/thomaslevesque/Extensions.Hosting.AsyncInitialization/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248103872,"owners_count":21048245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","async-initialization","csharp","dotnet","generic-host"],"created_at":"2024-10-15T01:55:39.857Z","updated_at":"2025-04-09T20:12:39.514Z","avatar_url":"https://github.com/thomaslevesque.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Extensions.Hosting.AsyncInitialization\n\n[![NuGet version](https://img.shields.io/nuget/v/Extensions.Hosting.AsyncInitialization.svg?logo=nuget)](https://www.nuget.org/packages/Extensions.Hosting.AsyncInitialization)\n[![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/thomaslevesque/Extensions.Hosting.AsyncInitialization/build.yml?branch=master\u0026logo=github)](https://github.com/thomaslevesque/Extensions.Hosting.AsyncInitialization/actions/workflows/build.yml)\n\nA simple helper to perform async application initialization and teardown for the generic host in .NET 6.0 or higher (e.g. in ASP.NET Core apps).\n\n## Basic usage\n\n1. Install the [Extensions.Hosting.AsyncInitialization](https://www.nuget.org/packages/Extensions.Hosting.AsyncInitialization/) NuGet package:\n\n    Command line:\n   \n    ```PowerShell\n    dotnet add package Extensions.Hosting.AsyncInitialization\n    ```\n\n    Package manager console:\n    ```PowerShell\n    Install-Package Extensions.Hosting.AsyncInitialization\n    ```\n\n\n2. Create a class (or several) that implements `IAsyncInitializer`. This class can depend on any registered service.\n\n    ```csharp\n    public class MyAppInitializer : IAsyncInitializer\n    {\n        public MyAppInitializer(IFoo foo, IBar bar)\n        {\n            ...\n        }\n\n        public async Task InitializeAsync(CancellationToken cancellationToken)\n        {\n            // Initialization code here\n        }\n    }\n    ```\n\n3. Register your initializer(s) in the same place as other services:\n\n    ```csharp\n    services.AddAsyncInitializer\u003cMyAppInitializer\u003e();\n    ```\n\n4. In the `Program` class, replace the call to `host.RunAsync()` with `host.InitAndRunAsync()`:\n\n   ```csharp\n   public static async Task Main(string[] args)\n   {\n       var host = CreateHostBuilder(args).Build();\n       await host.InitAndRunAsync();\n   }\n   ```\n   \n   This will run each initializer, in the order in which they were registered.\n\n## Teardown\n\nIn addition to initialization, this library also supports performing cleanup tasks when the app terminates. To use this, make your initializer implement `IAsyncTeardown`, and implement the `TeardownAsync` method:\n\n\n```csharp\npublic class MyAppInitializer : IAsyncTeardown\n{\n    public MyAppInitializer(IFoo foo, IBar bar)\n    {\n        ...\n    }\n\n    public async Task InitializeAsync(CancellationToken cancellationToken)\n    {\n        // Initialization code here\n    }\n\n    public async Task TeardownAsync(CancellationToken cancellationToken)\n    {\n        // Cleanup code here\n    }\n}\n```\n\nWhen you run the application with `InitAndRunAsync`, each initializer that supports teardown will be invoked in reverse registration order, i.e.:\n\n- initializer 1 performs initialization\n- initializer 2 performs initialization\n- application runs\n- initializer 2 performs teardown\n- initializer 1 performs teardown\n\n## Lifetime and state considerations\n\nWhen you create an initializer that also performs teardown, keep in mind that it will actually be resolved twice:\n- once for initialization\n- once for teardown\n\nInitialization and teardown each runs in its own service provider scope. This means that a different instance of your initializer will be used for initialization and teardown, so your initializer cannot keep state between initialization and teardown, unless it's registered as a singleton.\n\nIf you do register your initializer as a singleton, keep in mind that it must not depend on any scoped service, otherwise the scoped service will live for the whole lifetime of the application; this is an anti-pattern known as [captive dependency](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-guidelines#captive-dependency).\n\n## Advanced usage\n\nIf, for some reason, you need more control over the application execution process, you can manually call the `InitAsync` and `TeardownAsync` methods on the host. If you do that, keep in mind that `TeardownAsync` cannot be called after `host.RunAsync()` completes, because the host will already have been disposed:\n\n```csharp\n// DO NOT DO THIS\nawait host.InitAsync();\nawait host.RunAsync();\nawait host.TeardownAsync(); // Will fail because RunAsync disposed the host\n```\n\nSo you will need to manually call `StartAsync` and `WaitForShutdownAsync`, and call `TeardownAsync` _before_ you dispose the host:\n\n```csharp\nawait using (var host = CreateHostBuilder(args).Build())\n{\n    await host.InitAsync();\n    await host.StartAsync();\n    await host.WaitForShutdownAsync();\n    await host.TeardownAsync();\n}\n```\n\n## Cancellation\n\nThe `InitAndRunAsync`, `InitAsync` and `TeardownAsync` all support passing a cancellation token to abort execution if needed. Cancellation will be propagated to the initializers.\n\nIn the following example, execution (including initialization, but not teardown) will be aborted when `Ctrl + C` keys are pressed :\n```csharp\npublic static async Task Main(string[] args)\n{\n    using CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();\n\n    // The following line will hook `Ctrl` + `C` to the cancellation token.\n    Console.CancelKeyPress += (source, args) =\u003e cancellationTokenSource.Cancel();\n\n    var host = CreateHostBuilder(args).Build();\n    await host.InitAndRunAsync(cancellationTokenSource.Token);\n}\n```\n\nAs mentioned above, when using `InitAndRunAsync`, the cancellation token will *not* be passed to the teardown. This is because when your application is stopped, you typically still want the teardown to occur. Instead, teardown will run with a default timeout of 10 seconds (you can also specify a timeout explicitly).\n\nIf you don't want this behavior, you can manually call `InitAsync` and `TeardownAsync` as explained in the previous section.\n\n### Migration from 2.x or earlier\n\nIf you were already using this library prior to version 3.x, your code would typically look like this:\n\n```csharp\nawait host.InitAsync();\nawait host.RunAsync();\n```\n\nThis will still work without changes. Just keep in mind that, as explained in the Advanced Usage section, adding a call to `host.TeardownAsync()` after `host.RunAsync()` *will not work*. If you need teardown, the simplest way is to remove the explicit call to `InitAsync`, and call `InitAndRunAsync` instead of `RunAsync`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomaslevesque%2Fextensions.hosting.asyncinitialization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthomaslevesque%2Fextensions.hosting.asyncinitialization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthomaslevesque%2Fextensions.hosting.asyncinitialization/lists"}