Fill service provider with services marked by attributes
Install-Package ServiceLocator
where T is a Assembly to scan
public void ConfigureServices(IServiceCollection services)
{
// ****
services.AddServiceLocator<Program>();
// ****
}
Decorate class with attributes, so that locator can find them and register them in service provider.
- [Service(ServiceLifetime.Scoped)]
- [Service(ServiceLifetime.Singleton)]
- [Service(ServiceLifetime.Transient)]
Singleton means only a single instance will ever be created. That instance is shared between all components that require it. The same instance is thus used always.
Scoped means an instance is created once per scope. A scope is created on every request to the application, thus any components registered as Scoped will be created once per request.
Transient components are created every time they are requested and are never shared.
[Service(ServiceLifetime.Scoped)]
internal class ScopedScopedValueService : IScopedValueService
{
}