Condividi tramite


AsyncPackage.GetServiceAsync Method

Definition

Overloads

GetServiceAsync(Type)

Retrieves an async service registered with this package asynchronously, falling back to the asynchronous GlobalProvider and synchronous GlobalProvider if there is no service with this identity registered with the package.

GetServiceAsync(Type, Boolean)

Retrieves an async service registered with this package asynchronously, falling back to the asynchronous GlobalProvider and synchronous GlobalProvider if there is no service with this identity registered with the package. It also allows specifying a hint whether to throw an exception if it could not be retrieved, it does not, however, guarantee exceptions will be thrown for all failure cases.

GetServiceAsync<TService,TInterface>(Boolean, CancellationToken)

Retrieves an async service registered with this package asynchronously, falling back to the asynchronous GlobalProvider and synchronous GlobalProvider if there is no service with this identity registered with the package. It also allows specifying whether to throw an exception if it could not be retrieved.

GetServiceAsync(Type)

Retrieves an async service registered with this package asynchronously, falling back to the asynchronous GlobalProvider and synchronous GlobalProvider if there is no service with this identity registered with the package.

public:
 virtual System::Threading::Tasks::Task<System::Object ^> ^ GetServiceAsync(Type ^ serviceType);
public System.Threading.Tasks.Task<object> GetServiceAsync (Type serviceType);
public System.Threading.Tasks.Task<object?> GetServiceAsync (Type serviceType);
abstract member GetServiceAsync : Type -> System.Threading.Tasks.Task<obj>
override this.GetServiceAsync : Type -> System.Threading.Tasks.Task<obj>
Public Function GetServiceAsync (serviceType As Type) As Task(Of Object)

Parameters

serviceType
Type

The service identity of the service to retrieve.

Returns

A task whose result is one of the following:

  • The service if the request was successful.
  • null if the service threw an exception during activation.
  • null if the associated package failed to load.
  • null if the associated package proffered null.
  • null if the associated package could not be found, or the package did not correctly proffer the service.
  • null if the environment has starting shutting down and the retrieval would have resulted in a package load.
  • Otherwise; null if there was any other failure retrieving the service.

Implements

Exceptions

serviceType is null.

Remarks

This method retrieves only asynchronous and not synchronous services registered with the package, however, it can retrieve both synchronous and asynchronous proffered services if it falls back to GlobalProvider or GlobalProvider.

Performing an explicit cast of the resulting service to a specific interface could perform 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). Instead, prefer the generic equivalent GetServiceAsync<TService,TInterface>(Boolean, CancellationToken), or the extension methods available from ServiceExtensions, noting the difference in behavior around exceptions.

This method is safe to access from any thread.

Applies to

GetServiceAsync(Type, Boolean)

Retrieves an async service registered with this package asynchronously, falling back to the asynchronous GlobalProvider and synchronous GlobalProvider if there is no service with this identity registered with the package. It also allows specifying a hint whether to throw an exception if it could not be retrieved, it does not, however, guarantee exceptions will be thrown for all failure cases.

public:
 virtual System::Threading::Tasks::Task<System::Object ^> ^ GetServiceAsync(Type ^ serviceType, bool swallowExceptions);
public System.Threading.Tasks.Task<object> GetServiceAsync (Type serviceType, bool swallowExceptions);
public System.Threading.Tasks.Task<object?> GetServiceAsync (Type serviceType, bool swallowExceptions);
abstract member GetServiceAsync : Type * bool -> System.Threading.Tasks.Task<obj>
override this.GetServiceAsync : Type * bool -> System.Threading.Tasks.Task<obj>
Public Function GetServiceAsync (serviceType As Type, swallowExceptions As Boolean) As Task(Of Object)

Parameters

serviceType
Type

The service identity of the proffered service to retrieve.

swallowExceptions
Boolean

true to return null on failure; otherwise, false to throw an exception if the service could not be retrieved. NOTE, specifying false does not does not guarantee exceptions will be thrown for all failures.

Returns

A task whose result is one of the following:

  • The service if the request was successful.
  • null if the associated package proffered null.
  • null if the associated package could not be found, or the package did not correctly proffer the service.
  • null if the environment has starting shutting down and the retrieval would have resulted in a package load.
  • Otherwise; null if there was any other failure retrieving the service and swallowExceptions is true.

Implements

Exceptions

serviceType is null.

swallowExceptions is false and there was 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.

Remarks

This method retrieves only asynchronous and not synchronous services registered with the package, however, it can retrieve both synchronous and asynchronous proffered services if it falls back to GlobalProvider or GlobalProvider.

Performing an explicit cast of the resulting service to a specific interface could perform 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). Instead, prefer the generic equivalent GetServiceAsync<TService,TInterface>(Boolean, CancellationToken), or the extension methods available from ServiceExtensions, noting the difference in behavior around exceptions.

This method is safe to access from any thread.

Applies to

GetServiceAsync<TService,TInterface>(Boolean, CancellationToken)

Retrieves an async service registered with this package asynchronously, falling back to the asynchronous GlobalProvider and synchronous GlobalProvider if there is no service with this identity registered with the package. It also allows specifying whether to throw an exception if it could not be retrieved.

public System.Threading.Tasks.Task<TInterface?> GetServiceAsync<TService,TInterface> (bool throwOnFailure, System.Threading.CancellationToken cancellationToken) where TInterface : class;
abstract member GetServiceAsync : bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Interface (requires 'Interface : null)> (requires 'Interface : null)
override this.GetServiceAsync : bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Interface (requires 'Interface : null)> (requires 'Interface : null)
Public Function GetServiceAsync(Of TService, TInterface) (throwOnFailure As Boolean, cancellationToken As CancellationToken) As Task(Of 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.

cancellationToken
CancellationToken

A token whose cancellation indicates that the caller no longer is interested in the result. This will not cancel the in-progress loading of packages and/or creation of services as a result of the service retrieval, but this token will result in an expedient cancellation of the returned Task.

Returns

Task<TInterface>

A task representing the service retrieval whose result is the service or null if there was a failure and throwOnFailure is false.

Implements

Exceptions

cancellationToken has been canceled.

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

This method retrieves only asynchronous and not synchronous services registered with the package, however, it can retrieve both synchronous and asynchronous proffered services if it falls back to GlobalProvider or GlobalProvider.

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 equivalents GetServiceAsync(Type) and GetServiceAsync(Type, Boolean).

This method is safe to access from any thread.

Applies to