A class decorated with this attribute becomes a service provider. That class must be partial.
To add services to that class, add additional attributes to that class:
SingletonAttribute, ScopedAttribute, TransientAttribute, DelegateAttribute.
The source generator will generate a complete service provider, but you can add additional custom implementation to it.
Name | Type | Description |
---|---|---|
InterfaceType | Type | Type of the generated Interface. If omitted, the name will be "I{ClassName}". Not compatible with InterfaceName. At most one property must be set. |
InterfaceName | string | Name/Identifier of the generated Interface. If omitted, the name will be "I{ClassName}". Not compatible with InterfaceType. At most one property must be set. |
CreationTime | CreationTiming | Decides whether services defaulting to lazy construction or instantiation inside the constructor. This option applies to all services where the "CreationTime"-attribute is not set. Default is CreationTiming.Constructor. |
GetAccessor | GetAccess | Decides whether the members to access the services in the ServiceProvider defaulting to properties or methods. This option applies to all services where the "GetAccessor"-attribute is not set. Default is GetAccess.Property. |
GenerateDisposeMethods | DisposeGeneration | Toggles the generation of the Dispose methods: public void Dispose(); public ValueTask DisposeAsync(); It can be toggled that both are generated, only one of them or the generation is skipped entirely. Default is DisposeGeneration.GenerateBoth |
ThreadSafe | bool | Indicates if the generated code will be thread safe or a little bit more efficient. Affects performance for lazy constructed singleton services and disposables transient services: Singletons acquire a lock for construction; Disposable transient services acquire a lock when constructed and disposed to synchronize access on a dispose-list. This option should be set to false, if the provider is used in a single threaded scenario or only accessed by one thread at a time. Default is true. |
A class decorated with this attribute becomes a service provider. That class must be partial.
To add services to that class, add additional attributes to that class:
SingletonAttribute, ScopedAttribute, TransientAttribute, DelegateAttribute.
The source generator will generate a complete service provider, but you can add additional custom implementation to it.
Name | TypeConstraints | Description |
---|---|---|
TInterface | interface | An explicit declared interface the generated interface will be based on: The name, access modifier, namespace and containing types will be inferred. That interface must be partial. If the generated interface is used without declaring the interface yourself, it will have no effect. |
Name | Type | Description |
---|---|---|
CreationTime | CreationTiming | Decides whether services defaulting to lazy construction or instantiation inside the constructor. This option applies to all services where the "CreationTime"-attribute is not set. Default is CreationTiming.Constructor. |
GetAccessor | GetAccess | Decides whether the members to access the services in the ServiceProvider defaulting to properties or methods. This option applies to all services where the "GetAccessor"-attribute is not set. Default is GetAccess.Property. |
GenerateDisposeMethods | DisposeGeneration | Toggles the generation of the Dispose methods: public void Dispose(); public ValueTask DisposeAsync(); It can be toggled that both are generated, only one of them or the generation is skipped entirely. Default is DisposeGeneration.GenerateBoth |
ThreadSafe | bool | Indicates if the generated code will be thread safe or a little bit more efficient. Affects performance for lazy constructed singleton services and disposables transient services: Singletons acquire a lock for construction; Disposable transient services acquire a lock when constructed and disposed to synchronize access on a dispose-list. This option should be set to false, if the provider is used in a single threaded scenario or only accessed by one thread at a time. Default is true. |
This attribute can be applied to the ServiceProvider itself, so right next to the ServiceProviderAttribute or to a class named "Scope" inside the ServiceProvider, but not both.
This attribute itself does nothing, but it provides additional configurations for the ScopedProvider.
Name | Type | Description |
---|---|---|
Generate | bool | If turned off, no Scope Class is generated and therefore ScopedAttribute has no effect. |
CreationTime | CreationTiming | Decides whether services in the ScopedProvider defaulting to lazy construction or instantiation inside the constructor. This option applies to all services where the "CreationTime"-attribute is not set. Default is ServiceProviderAttribute.CreationTime. |
GetAccessor | GetAccess | Decides whether the members to access the services in the ScopedProvider defaulting to properties or methods. This option applies to all services where the "GetAccessor"-attribute is not set. Default is ServiceProviderAttribute.GetAccessor. |
GenerateDisposeMethods | DisposeGeneration | Toggles the generation of the Dispose methods: public void Dispose(); public ValueTask DisposeAsync(); It can be toggled that both are generated, only one of them or the generation is skipped entirely. Default is ServiceProviderAttribute.GenerateDisposeMethods |
ThreadSafe | bool | Indicates if the generated code will be thread safe or a little bit more efficient. Affects performance for lazy constructed singleton services and disposables transient services: Singletons acquire a lock for construction; Disposable transient services acquire a lock when constructed and disposed to synchronize access on a dispose-list. This option should be set to false, if the provider is used in a single threaded scenario or only accessed by one thread at a time. Default is ServiceProviderAttribute.ThreadSafe. |
Specifies a singleton service. That means there will be a single instance of that service in every ServiceProvider instance.
If ServiceProviderAttribute is used at the same class, this service will be added to the provider.
Name | Description |
---|---|
TService | Type of the service. |
TImplementation (Optional) | Type of the implementation. If omitted, implementation will be the same type as TService. |
Name | Type | Description |
---|---|---|
Implementation | string | Fieldname, propertyname or methodname that will be the implementation supplier for the given service. The parameters of the method will be dependency injected. |
Name | string | The name of this service. If omitted, it will be the name of TImplementation. If the getter is a method, for the identifier a "Get" prefix will be used, but this does not affect the name. |
CreationTime | CreationTiming | Decides whether this service will be lazy constructed or instantiated inside the constructor. Defaults to ServiceProviderAttribute.CreationTime or ScopedProviderAttribute.CreationTime. |
GetAccessor | GetAccess | Decides whether the type of the member to access this service will be a property or method. Defaults to ServiceProviderAttribute.GetAccessor or ScopedProviderAttribute.GetAccessor. |
NoDispose | bool | When true, the ServiceProvider does not dispose this service on Dispose() or DisposeAsync(), regardless the service implements IDisposable or IAsyncDisposable. If the service does not implement IDisposable/IAsyncDisposable, this will have no effect. Default is false. |
Specifies a scoped service. That means this service will only be available in the ScopedProvider and there will be a single instance of that service in every ScopedProvider instance.
If ServiceProviderAttribute is used at the same class, this service will be added to the provider.
Name | Description |
---|---|
TService | Type of the service. |
TImplementation (Optional) | Type of the implementation. If omitted, implementation will be the same type as TService. |
Name | Type | Description |
---|---|---|
Implementation | string | Fieldname, propertyname or methodname that will be the implementation supplier for the given service. The parameters of the method will be dependency injected. |
Name | string | The name of this service. If omitted, it will be the name of TImplementation. If the getter is a method, for the identifier a "Get" prefix will be used, but this does not affect the name. |
CreationTime | CreationTiming | Decides whether this service will be lazy constructed or instantiated inside the constructor. Defaults to ServiceProviderAttribute.CreationTime or ScopedProviderAttribute.CreationTime. |
GetAccessor | GetAccess | Decides whether the type of the member to access this service will be a property or method. Defaults to ServiceProviderAttribute.GetAccessor or ScopedProviderAttribute.GetAccessor. |
NoDispose | bool | When true, the ServiceProvider does not dispose this service on Dispose() or DisposeAsync(), regardless the service implements IDisposable or IAsyncDisposable. If the service does not implement IDisposable/IAsyncDisposable, this will have no effect. Default is false. |
Specifies a transient service. That means this service will be instantiated each time requested.
If ServiceProviderAttribute is used at the same class, this service will be added to the provider.
Name | Description |
---|---|
TService | Type of the service. |
TImplementation (Optional) | Type of the implementation. If omitted, implementation will be the same type as TService. |
Name | Type | Description |
---|---|---|
Implementation | string | The name of a method or property that construct a implementation for the given service. The parameters of the method will be dependency injected. |
Name | string | The name of this service. If omitted, it will be the name of TImplementation. If the getter is a method, for the identifier a "Get" prefix will be used, but this does not affect the name. |
GetAccessor | GetAccess | Decides whether the type of the member to access this service will be a property or method. Defaults to ServiceProviderAttribute.GetAccessor or ScopedProviderAttribute.GetAccessor. |
NoDispose | bool | When true, the ServiceProvider does not dispose this service on Dispose() or DisposeAsync(), regardless the service implements IDisposable or IAsyncDisposable. If the service does not implement IDisposable/IAsyncDisposable, this will have no effect. Default is false. |
Specifies a delegate service. That means requesting this service will give you a method.
If ServiceProviderAttribute is used at the same class, this service will be added to the provider.
Name | Description |
---|---|
TService | Type of the service and implementation. |
Name | Type | Description |
---|---|---|
methodName | string | Methodname that will be the implementation for the given service. |
Name | Type | Description |
---|---|---|
Name | string | The name of this service. If omitted, it will be the name of TImplementation. If the getter is a method, for the identifier a "Get" prefix will be used, but this does not affect the name. |
GetAccessor | GetAccess | Decides whether the type of the member to access this service will be a property or method. Defaults to ServiceProviderAttribute.GetAccessor or ScopedProviderAttribute.GetAccessor. |
Registers all attributes of the specified class/struct/interface in TModule.
There are several options to handle the instantiation of the module:
- static (no instantiation)
- injected as service
- given as parameter
Name | Description |
---|---|
TModule | The class, struct or interface containing the attributes to include. |
Name | Type | Description |
---|---|---|
mode | ImportMode = ImportMode.Auto | Option for handling the instantiation of the module. It can be static (no instantiation), injected as service or given as parameter. Default is ImportMode.Auto. |
This attribute is used to set a non-required property as dependency (required properties are always dependencies).
It is also used to specify specific/named services, see Name property.
Name | Type | Description |
---|---|---|
Name | string | The name this dependency gets the service injected from. If omitted, it will match based on the type. If multiple services for this type exists, this property must be set, otherwise compile error. When the Name property of a service not set, the name defaults to the name of TImplementation. |
Explicitly specifies the constructor that is used to create the service.
If multiple constructors are available, you must use this attribute on exactly one constructor, otherwise a compile error occurs.
Only constructors with accessibility public or internal are considered, others are ignored.
A struct has always the parameterless constructor, so by specifying one non-parameterless constructor you have actually two and therefore have to use this attribute.
Configuration when the instantiation of the service happens.
At ServiceProvider instantiation (inside the constructor) or lazy (first time used).
Name | Description |
---|---|
Constructor | The instantiation of the service happens inside the constructor of the ServiceProvider. |
Lazy | The instantiation of the service happens the first time it is requested. |
The type of the member to access the service.
It can be either property or method.
Name | Description |
---|---|
Property | The member will be a property with a get-accessor. The name of the property will be the same as the service name. |
Method | The member will be a method with no parameters. The name will be prefixed with "Get" e.g. name "MyService" will generate member "GetMyService()". |
Configuration for generating the Dispose methods:
public void Dispose();
public ValueTask DisposeAsync();
It can be toggled that both are generated, only one of them or the generation is skipped entirely.
Name | Description |
---|---|
NoDisposing | The generation of both dispose methods will be skipped. |
DisposeOnly | Only the sync version of Dispose will be generated: public void Dispose(); |
DisposeAsyncOnly | Only the async version DisposeAsync will be generated: public ValueTask DisposeAsync(); |
GenerateBoth | Both versions Dispose and DisposeAsync will be generated. public void Dispose(); public ValueTask DisposeAsync(); |
Option for handling the instantiation of the module.
Name | Description |
---|---|
Auto | Chooses Static when type is interface, Service if constructed with parameterless constructor, Parameter otherwise. |
Static | No instantiation needed, all members are static. |
Service | The module is registered as service. |
Parameter | An instance of the module is given as parameter. |