Skip to content

Async Construct

Kengwang edited this page Jan 29, 2024 · 1 revision

If you have to do some async work during construct, you can implement IAsyncConstruct interface to let Depository know.

public class AsyncConstructorService : IAsyncConstructService
{
    // You can do some async work here
    public async Task InitializeService()
    {
        await Task.Delay(1000);
    }
}

Depository will invoke InitializeService after construct.

You can resolve Task<T> so that you can await the async construct.

var service = await depository.Resolve<Task<IAsyncConstructService>>();

You can also resolve IAsyncConstructService directly, Depository will invoke InitializeService as a fire-and-forget.

var service = depository.Resolve<IAsyncConstructService>();

Warning

If you use fire-and-forget way, the async constructor will be invoked after the resolve method returns.

Clone this wiki locally