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
configurationActionAction<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
serviceDescriptorsIServiceCollectionCollection 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
serviceTypeTypeService type to be resolved.
Returns
- object
Returns an instance of
serviceType.
Exceptions
- ArgumentNullException
Thrown when
serviceTypeis null.- ServiceResolutionException
Thrown when resolution of
serviceTypefails. This typically occurs when no implementing type forserviceTypeis 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
serviceTypeTypeService type to be resolved.
Returns
- object
Returns an instance of
serviceTypeor null.
Exceptions
- ArgumentNullException
Thrown when
serviceTypeis null.- ServiceResolutionException
Thrown when resolution of
serviceTypefails. 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
TServiceor null.
Type Parameters
TServiceService type to be resolved.
Exceptions
- ServiceResolutionException
Thrown when resolution of
TServicefails. 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
TServiceService type to be resolved.
Exceptions
- ServiceResolutionException
Thrown when resolution of
TServicefails. This typically occurs when no implementing type forTServiceis 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
serviceProviderIServiceProviderService 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
serviceTypeType of service being implemented.
factoryMethodFunc<object>Method providing an instance of
service.namestringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transientboolIndicates 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
serviceorfactoryMethodis null.- ServiceRegistrationException
Thrown when registration fails (e.g.
nameis 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
serviceTypeType of service being implemented.
instanceobjectInstance of
serviceimplementation to be used.namestringSets 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
serviceorinstanceis null.- ServiceRegistrationException
Thrown when registration fails (e.g.
nameis 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
serviceTypeType of service being implemented.
implementationTypeImplementing type for
service.namestringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transientboolIndicates 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
serviceorimplementationis null.- ServiceRegistrationException
Thrown when registration fails (e.g.
nameis 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
factoryMethodFunc<object>Method providing an instance of
TService.namestringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transientboolIndicates whether service implementation lifestyle should be transient, or default to singleton.
registrationPriorityRegistrationPriorityDefines the registration priority.
Type Parameters
TServiceService 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
factoryMethodis null.- ServiceRegistrationException
Thrown when registration fails (e.g.
nameis 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
instanceobjectInstance of
TServiceimplementation to be used.namestringSets 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
TServiceService 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
instanceis null.- ServiceRegistrationException
Thrown when registration fails (e.g.
nameis 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
namestringSets an explicit name of the registration. If not set, the name is automatically inferred from the implementation being registered. Name must be unique.
transientboolIndicates whether service implementation lifestyle should be transient, or default to singleton.
Type Parameters
TServiceService being implemented.
TImplementationImplementing 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.
nameis not unique).