Async initialization in ASP.NET Core
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src/AspNetCore.AsyncInitialization
tests/AspNetCore.AsyncInitialization.Tests
tools/build
.gitignore
AspNetCore.AsyncInitialization.sln
LICENSE.txt
README.md
appveyor.yml
build.cmd

README.md

AspNetCore.AsyncInitialization

A simple helper to perform async application initialization in ASP.NET Core 2.0 or higher.

Usage

  1. Install the AspNetCore.AsyncInitialization NuGet package:

    Command line:

    dotnet add package AspNetCore.AsyncInitialization

    Package manager console:

    Install-Package AspNetCore.AsyncInitialization
  2. Create a class (or several) that implements IAsyncInitializer. This class can depend on any registered service.

    public class MyAppInitializer : IAsyncInitializer
    {
        public MyAppInitializer(IFoo foo, IBar bar)
        {
            ...
        }
    
        public async Task InitializeAsync()
        {
            // Initialization code here
        }
    }
  3. Register your initializer(s) in the Startup.ConfigureServices method:

        services.AddAsyncInitializer<MyAppInitializer>();
  4. In the Program class, make the Main method async and change its code to initialize the host before running it:

    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 to enable the "async Main" feature.)

This will run each initializer, in the order in which they were registered.