Compartir a través de


IAsyncServiceProvider2.GetServiceAsync(Type, Boolean) Method

Definition

Retrieves a proffered service asynchronously, 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:
 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>
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.

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

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 equivalents GetServiceAsync<TService,TInterface>(Boolean, CancellationToken) and Microsoft.VisualStudio.Shell.ServiceExtensions.ServiceExtensions.GetServiceAsync, noting the difference in behavior around exceptions.

This method is safe to access from any thread.

Applies to