Note: .ConfigureAwait(false)
is omitted from all examples here, but due to poor design decisions in C# should be added to every await when writing library code.
Stephen Cleary, whose blog is an excellent resource for Async, describes The Asynchronous Initialization Pattern which is useful when your class has to do something async on construction. Contruction itself can't be async, but after construction (either by explicit new
or being passed an potentially newly constructed instance through DI), you await
the initialisation before using it. Note that the initialisation code starts executing on construction.
interface IAsyncInitialization
{
Task Initialization { get; }