Extension library designed to simplify the dependency registration process within the Microsoft.Extensions.DependencyInjection
base APIs for small applications.
There are 2 extension methods as an entry point for this library, the first on which we'll talk about in this section is AddAllServicesFrom
:
public void ConfigureServices(IServiceCollection services)
{
services.AddAllServicesFrom(triggersAsyncSetup: true, Assembly.GetEntryAssembly());
}
This method will register every class that implements one of the 6 services interfaces into the IServiceCollection. These interfaces can be implementend by a concrete type directly (they'll be registered with their concrete type into the DI container) or by an interface that is then implemented by the concrete type (which will register them by their own interface type into the DI container).
Also, an IHostedService responsible of setting up the IAsyncSetupWithProvider, ILifetimeHostedService and IAsyncSetup services is automatically registered, therefore if you need thoses registered services setting up before that your own IHostedService starts you'll have to call this method before your own service registration. You can disable this feature by setting the 'triggersAsyncSetup' arg to false. Plus if any IAsyncSetupWithProvider, IAsyncSetup or ISingleton also implement either the IAsyncDisposable or IDisposable interface, the registered hosted service will also handle their disposal.
The second entry point of this library is the Configure extension method, here is an example :
public void ConfigureServices(IServiceCollection services)
{
services.Configure(opt =>
{
opt.ConfigureAsyncSetupWithProvider<IDataRepository, DataRepository>();
opt.ConfigureAsyncSetup<DataFromJsonRepository>();
opt.ExecuteAsyncSetupServicesFirst();
if(_environment.IsDevelopment()) opt
.FireAndForgetAsyncSetupServices()
.ConfigureRegistrationOptions(
x => { x.RegisterSingletonServices = true;
x.RegisterScopedServices = true; });
else opt.RegisterAllServices();
opt.UseServiceAssemblies(
Assembly.GetEntryAssembly(),
typeof(DataFromJsonRepository).Assembly);
});
}
If you need anymore infos about the configure logic, you can check the relative sources on this repository.