https://github.com/thomaslevesque/aspnetcore.asyncinitialization
Async initialization in ASP.NET Core 2.x
https://github.com/thomaslevesque/aspnetcore.asyncinitialization
Last synced: about 1 year ago
JSON representation
Async initialization in ASP.NET Core 2.x
- Host: GitHub
- URL: https://github.com/thomaslevesque/aspnetcore.asyncinitialization
- Owner: thomaslevesque
- License: apache-2.0
- Created: 2018-09-23T17:27:22.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-09-01T17:29:39.000Z (almost 4 years ago)
- Last Synced: 2025-04-11T01:19:19.744Z (about 1 year ago)
- Language: C#
- Homepage:
- Size: 41 KB
- Stars: 87
- Watchers: 8
- Forks: 9
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# AspNetCore.AsyncInitialization
[](https://www.nuget.org/packages/AspNetCore.AsyncInitialization)
[](https://ci.appveyor.com/project/thomaslevesque/aspnetcore-asyncinitialization)
[](https://ci.appveyor.com/project/thomaslevesque/aspnetcore-asyncinitialization/build/tests)
A simple helper to perform async application initialization in ASP.NET Core 2.x.
***Note:** This project supports **only** ASP.NET Core 2.x. If you need to perform async initialization for the generic host (.NET Core 2.1+ and ASP.NET Core 3), use [Extensions.Hosting.AsyncInitialization](https://github.com/thomaslevesque/Extensions.Hosting.AsyncInitialization) instead.*
## Usage
1. Install the [AspNetCore.AsyncInitialization](https://www.nuget.org/packages/AspNetCore.AsyncInitialization/) NuGet package:
Command line:
```PowerShell
dotnet add package AspNetCore.AsyncInitialization
```
Package manager console:
```PowerShell
Install-Package AspNetCore.AsyncInitialization
```
1. Create a class (or several) that implements `IAsyncInitializer`. This class can depend on any registered service.
```csharp
public class MyAppInitializer : IAsyncInitializer
{
public MyAppInitializer(IFoo foo, IBar bar)
{
...
}
public async Task InitializeAsync()
{
// Initialization code here
}
}
```
1. Register your initializer(s) in the `Startup.ConfigureServices` method:
```csharp
services.AddAsyncInitializer();
```
1. In the `Program` class, make the `Main` method async and change its code to initialize the host before running it:
```csharp
public static async Task Main(string[] args)
{
var host = CreateWebHostBuilder(args).Build();
await host.InitAsync();
host.Run();
}
```
(Note that you need to [set the C# language version to 7.1 or higher in your project](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version#edit-the-csproj-file) to enable the "async Main" feature.)
This will run each initializer, in the order in which they were registered.