Class Service
Provides service management and resolution functionality for the system.
public static class Service
- Inheritance
-
objectService
Methods
Configure<TOptions>(Action<TOptions>)
Configures TOptions
.
public static void Configure<TOptions>(Action<TOptions> configurationAction) where TOptions : class
Parameters
configurationAction
Action<TOptions>
Type Parameters
TOptions
InitializeContainer()
Initialize container for services resolution. No action is performed when container was already initialized
public static void InitializeContainer()
MergeDescriptors(IServiceCollection)
Merges registered services (descriptors) that were registered through RegisterImplementationAttribute or one of 'CMS.Core.Service.Use' methods to the
serviceDescriptors
collection.
After the merge the services are ordered in the ascending order by their priority.
Firstly, services registered with the Fallback priority, then services from serviceDescriptors
, then services with the
SystemDefault priority and then with the Default priority.
public static void MergeDescriptors(IServiceCollection serviceDescriptors)
Parameters
serviceDescriptors
IServiceCollectionCollection of service descriptors to be merged with service descriptors collected via Kentico API.
Remarks
After the service provider is built from the Microsoft.Extensions.DependencyInjection.IServiceCollection it must be set as a provider for the system using SetProvider(IServiceProvider).
- See Also
Resolve(Type)
Returns an instance of serviceType
as registered within the system.
Only services with Singleton or Transient are resolved.
public static object Resolve(Type serviceType)
Parameters
serviceType
TypeService type to be resolved.
Returns
- object
Returns an instance of
serviceType
.
Exceptions
- ArgumentNullException
Thrown when
serviceType
is null.- ServiceResolutionException
Thrown when resolution of
serviceType
fails. This typically occurs when no implementing type forserviceType
is registered or the implementing type has dependencies which cannot be satisfied.
ResolveOptional(Type)
Returns an instance of serviceType
as registered within the system
or null when no resolution is possible.
Only services with Singleton or Transient are resolved.
public static object ResolveOptional(Type serviceType)
Parameters
serviceType
TypeService type to be resolved.
Returns
- object
Returns an instance of
serviceType
or null.
Exceptions
- ArgumentNullException
Thrown when
serviceType
is null.- ServiceResolutionException
Thrown when resolution of
serviceType
fails. This typically occurs when the implementing type has dependencies which cannot be satisfied.
ResolveOptional<TService>()
Returns an instance of TService
as registered within the system
or null when no resolution is possible.
Only services with Singleton or Transient are resolved.
public static TService ResolveOptional<TService>()
Returns
- TService
Returns an instance of
TService
or null.
Type Parameters
TService
Service type to be resolved.
Exceptions
- ServiceResolutionException
Thrown when resolution of
TService
fails. This typically occurs when the implementing type has dependencies which cannot be satisfied.
Resolve<TService>()
Returns an instance of TService
as registered within the system.
Only services with Singleton or Transient are resolved.
public static TService Resolve<TService>()
Returns
- TService
Returns an instance of
TService
.
Type Parameters
TService
Service type to be resolved.
Exceptions
- ServiceResolutionException
Thrown when resolution of
TService
fails. This typically occurs when no implementing type forTService
is registered or the implementing type has dependencies which cannot be satisfied.
SetProvider(IServiceProvider)
Sets the service provider to be used for service resolution in the system.
public static void SetProvider(IServiceProvider serviceProvider)
Parameters
serviceProvider
IServiceProviderService provider to be used for service resolution in the system.
Remarks
The service provider must be built from the Microsoft.Extensions.DependencyInjection.IServiceCollection that was merged with the registered services through MergeDescriptors(IServiceCollection) in order for the system service resolution to work.
Exceptions
- InvalidOperationException
Thrown when System.IServiceProvider was already initialized.
- See Also
Use(Type, Func<object>, string, bool)
Sets factoryMethod
to be used as a provider of implementation of service
.
public static void Use(Type service, Func<object> factoryMethod, string name = null, bool transient = false)
Parameters
service
TypeType of service being implemented.
factoryMethod
Func<object>Method providing an instance of
service
.name
stringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transient
boolIndicates whether service implementation lifestyle should be transient, or default to singleton.
Remarks
The name
does not need to be explicitly set unless registering one implementation multiple times (for various interfaces).
Exceptions
- ArgumentNullException
Thrown when
service
orfactoryMethod
is null.- ServiceRegistrationException
Thrown when registration fails (e.g.
name
is not unique).
Use(Type, object, string)
Sets instance
to be used as implementation of service
.
public static void Use(Type service, object instance, string name = null)
Parameters
service
TypeType of service being implemented.
instance
objectInstance of
service
implementation to be used.name
stringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
Remarks
The name
does not need to be explicitly set unless registering one implementation multiple times (for various interfaces).
Exceptions
- ArgumentNullException
Thrown when
service
orinstance
is null.- ServiceRegistrationException
Thrown when registration fails (e.g.
name
is not unique).
Use(Type, Type, string, bool)
Sets implementation
to be used as implementation of service
.
public static void Use(Type service, Type implementation, string name = null, bool transient = false)
Parameters
service
TypeType of service being implemented.
implementation
TypeImplementing type for
service
.name
stringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transient
boolIndicates whether service implementation lifestyle should be transient, or default to singleton.
Remarks
The name
does not need to be explicitly set unless registering one implementation multiple times (for various interfaces).
Exceptions
- ArgumentNullException
Thrown when
service
orimplementation
is null.- ServiceRegistrationException
Thrown when registration fails (e.g.
name
is not unique).
Use<TService>(Func<object>, string, bool, RegistrationPriority)
Sets factoryMethod
to be used as a provider of implementation of TService
.
public static void Use<TService>(Func<object> factoryMethod, string name = null, bool transient = false, RegistrationPriority registrationPriority = RegistrationPriority.Default)
Parameters
factoryMethod
Func<object>Method providing an instance of
TService
.name
stringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transient
boolIndicates whether service implementation lifestyle should be transient, or default to singleton.
registrationPriority
RegistrationPriorityDefines the registration priority.
Type Parameters
TService
Service being implemented.
Remarks
The name
does not need to be explicitly set unless registering one implementation multiple times (for various interfaces).
Exceptions
- ArgumentNullException
Thrown when
factoryMethod
is null.- ServiceRegistrationException
Thrown when registration fails (e.g.
name
is not unique).
Use<TService>(object, string)
Sets instance
to be used as implementation of TService
.
public static void Use<TService>(object instance, string name = null)
Parameters
instance
objectInstance of
TService
implementation to be used.name
stringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
Type Parameters
TService
Service being implemented.
Remarks
The name
does not need to be explicitly set unless registering one implementation multiple times (for various interfaces).
Exceptions
- ArgumentNullException
Thrown when
instance
is null.- ServiceRegistrationException
Thrown when registration fails (e.g.
name
is not unique).
Use<TService, TImplementation>(string, bool)
Sets TImplementation
to be used as implementation of TService
.
public static void Use<TService, TImplementation>(string name = null, bool transient = false) where TService : class where TImplementation : TService
Parameters
name
stringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transient
boolIndicates whether service implementation lifestyle should be transient, or default to singleton.
Type Parameters
TService
Service being implemented.
TImplementation
Implementing type for
TService
.
Remarks
The name
does not need to be explicitly set unless registering one implementation multiple times (for various interfaces).
Exceptions
- ServiceRegistrationException
Thrown when registration fails (e.g.
name
is not unique).