Class ServiceProviderExtensions
- Namespace
- Arc4u.Dependency
- Assembly
- Arc4u.Dependency.dll
Extension methods on IServiceProvider to resolve services without throwing when they cannot be created.
public static class ServiceProviderExtensions
- Inheritance
-
ServiceProviderExtensions
- Inherited Members
Methods
TryGetService(IServiceProvider, Type, string, out object?)
Tries to resolve a keyed service of the given type. Any exception raised while resolving (including a missing registration) is swallowed.
public static bool TryGetService(this IServiceProvider provider, Type type, string name, out object? value)
Parameters
providerIServiceProviderThe service provider.
typeTypeThe type of the service.
namestringThe key under which the service is registered.
valueobjectThe resolved service, or null when it cannot be resolved.
Returns
TryGetService<T>(IServiceProvider, string, out T?)
Tries to resolve a keyed service of type T. Any exception raised while resolving is swallowed.
public static bool TryGetService<T>(this IServiceProvider provider, string name, out T? service)
Parameters
providerIServiceProviderThe service provider.
namestringThe key under which the service is registered.
serviceTThe resolved service, or null when it is not registered or cannot be created.
Returns
Type Parameters
TThe type of the service.
TryGetService<T>(IServiceProvider, out T?)
Tries to resolve a service of type T. Any exception raised while resolving is swallowed.
public static bool TryGetService<T>(this IServiceProvider provider, out T? service)
Parameters
providerIServiceProviderThe service provider.
serviceTThe resolved service, or null when it is not registered or cannot be created.
Returns
Type Parameters
TThe type of the service.
Examples
if (serviceProvider.TryGetService<IMyOptionalService>(out var service))
{
service!.DoWork();
}