ServiceExtensions.GetService Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
GetService<TService,TInterface>(IServiceProvider) |
Retrieves a proffered service synchronously, throwing an exception if it could not be retrieved. |
GetService<TService,TInterface>(IServiceProvider, Boolean) |
Retrieves a proffered service synchronously, specifying whether to throw an exception if it could not be retrieved. |
GetService<TService,TInterface>(IServiceProvider)
Retrieves a proffered service synchronously, throwing an exception if it could not be retrieved.
public:
generic <typename TService, typename TInterface>
where TInterface : class[System::Runtime::CompilerServices::Extension]
static TInterface GetService(IServiceProvider ^ sp);
public static TInterface GetService<TService,TInterface> (this IServiceProvider sp) where TInterface : class;
static member GetService : IServiceProvider -> 'Interface (requires 'Interface : null)
<Extension()>
Public Function GetService(Of TService, TInterface) (sp As IServiceProvider) As TInterface
Type Parameters
- TService
The service identity of the proffered service to retrieve.
- TInterface
The interface used to interact with the proffered service. If TService
is not registered as async or free-threaded, then this cast will be performed on the main thread.
Parameters
The IServiceProvider.
Returns
The requested service.
Exceptions
sp
is null
There was a failure retrieving the service due to one of the following conditions:
- The service threw an exception during activation. InnerException may include details about the underlying failure.
- The associated package failed to load. InnerException may include details about the underlying failure.
- The associated package could not be found, or the package did not correctly proffer the service.
- The associated package proffered
null
. - The service does not support the requested interface specified by
TInterface
. - The environment has starting shutting down and the retrieval would have resulted in a package load.
Remarks
Prefer this method over an explicit cast to TInterface
to avoid a blocking RPC call if the underlying service is STA-bound, which can hang if the UI thread is blocked in a Run(Func<Task>) or Join(CancellationToken)
Note the difference in behavior this method has around exceptions to its non-generic equivalent GetService(Type).
This method is safe to access from any thread.
Applies to
GetService<TService,TInterface>(IServiceProvider, Boolean)
Retrieves a proffered service synchronously, specifying whether to throw an exception if it could not be retrieved.
public static TInterface? GetService<TService,TInterface> (this IServiceProvider sp, bool throwOnFailure = true) where TInterface : class;
public static TInterface? GetService<TService,TInterface> (this IServiceProvider sp, bool throwOnFailure) where TInterface : class;
static member GetService : IServiceProvider * bool -> 'Interface (requires 'Interface : null)
<Extension()>
Public Function GetService(Of TService, TInterface) (sp As IServiceProvider, Optional throwOnFailure As Boolean = true) As TInterface
<Extension()>
Public Function GetService(Of TService, TInterface) (sp As IServiceProvider, throwOnFailure As Boolean) As TInterface
Type Parameters
- TService
The service identity of the proffered service to retrieve.
- TInterface
The interface used to interact with the proffered service. If TService
is not registered as async or free-threaded, then this cast will be performed on the main thread.
Parameters
- throwOnFailure
- Boolean
true
to throw an exception if the service could not be retrieved; otherwise, false
to return null
on failure.
Returns
The requested service or null
if there was a failure and throwOnFailure
is false
.
Exceptions
sp
is null
throwOnFailure
is true
and there was a failure retrieving the service due to one of the following conditions:
- The service threw an exception during activation. InnerException may include details about the underlying failure.
- The associated package failed to load. InnerException may include details about the underlying failure.
- The associated package could not be found, or the package did not correctly proffer the service.
- The associated package proffered
null
. - The service does not support the requested interface specified by
TInterface
. - The environment has starting shutting down and the retrieval would have resulted in a package load.
Remarks
Prefer this method over an explicit cast to TInterface
to avoid a blocking RPC call if the underlying service is STA-bound, which can hang if the UI thread is blocked in a Run(Func<Task>) or Join(CancellationToken).
Note the difference in behavior this method has around exceptions to its non-generic equivalent GetService(Type).
This method is safe to access from any thread.