Table of Contents

Class ServiceBrokerClient

Namespace
Microsoft.ServiceHub.Framework
Assembly
Microsoft.ServiceHub.Framework.dll

A wrapper around IServiceBroker that caches and shares client proxies.

public class ServiceBrokerClient : IDisposableObservable, IDisposable
Inheritance
ServiceBrokerClient
Implements
Inherited Members

Constructors

ServiceBrokerClient(IServiceBroker, JoinableTaskFactory?)

Initializes a new instance of the ServiceBrokerClient class.

public ServiceBrokerClient(IServiceBroker serviceBroker, JoinableTaskFactory? joinableTaskFactory = null)

Parameters

serviceBroker IServiceBroker

The underlying service broker. This will be disposed of if it implements IDisposable when this ServiceBrokerClient is disposed of.

joinableTaskFactory JoinableTaskFactory

A means to avoid deadlocks if the authorization service requires the main thread. May be null.

Properties

InvalidationSemaphore

Gets the semaphore that is entered to raise the Invalidated event.

public ReentrantSemaphore InvalidationSemaphore { get; }

Property Value

ReentrantSemaphore

Remarks

This can be used to enter the same semaphore during initialization in order to ensure that an Invalidated event does not disrupt initialization.

IsDisposed

Gets a value indicating whether this instance has been disposed.

public bool IsDisposed { get; }

Property Value

bool

true if this instance has been disposed.

Methods

Dispose()

Invalidates all previously produced client proxies and disposes this object. Any client proxies currently rented will be disposed of when they are all returned.

public void Dispose()

Dispose(bool)

Disposes managed and unmanaged resources held by this instance.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

true to dispose managed and native resources; false to dispose of only native resources.

GetProxyAsync<T>(ServiceRpcDescriptor, ServiceActivationOptions, CancellationToken)

Requests access to some service through a client proxy. The same client proxy is returned for a given service and proxy type until it is invalidated.

public ValueTask<ServiceBrokerClient.Rental<T>> GetProxyAsync<T>(ServiceRpcDescriptor serviceRpcDescriptor, ServiceActivationOptions options = default, CancellationToken cancellationToken = default) where T : class

Parameters

serviceRpcDescriptor ServiceRpcDescriptor

An descriptor of the service.

options ServiceActivationOptions

Additional options that alter how the service may be activated or provide additional data to the service constructor. Only used if the service has not already been cached.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<ServiceBrokerClient.Rental<T>>

A rental around the client proxy that may be used to communicate with the service; or null if no matching service could be found. Proxies are kept alive while "rented", and may be kept alive beyond a rental until they are invalidated. The rental struct should be disposed as soon as the caller is done using it (such that the next use will call GetProxyAsync<T>(ServiceRpcDescriptor, CancellationToken) again and could tolerate getting a client proxy to a different service instance.) The client proxy itself within the rental struct should NOT be disposed directly since it can be shared across invocations of this method.

Type Parameters

T

The type of client proxy to create.

Exceptions

ServiceCompositionException

Thrown when a service discovery or activation error occurs.

GetProxyAsync<T>(ServiceRpcDescriptor, CancellationToken)

Requests access to some service through a client proxy. The same client proxy is returned for a given service and proxy type until it is invalidated.

public ValueTask<ServiceBrokerClient.Rental<T>> GetProxyAsync<T>(ServiceRpcDescriptor serviceRpcDescriptor, CancellationToken cancellationToken) where T : class

Parameters

serviceRpcDescriptor ServiceRpcDescriptor

An descriptor of the service.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<ServiceBrokerClient.Rental<T>>

A rental around the client proxy that may be used to communicate with the service; or null if no matching service could be found. Proxies are kept alive while "rented", and may be kept alive beyond a rental until they are invalidated. The rental struct should be disposed as soon as the caller is done using it (such that the next use will call GetProxyAsync<T>(ServiceRpcDescriptor, CancellationToken) again and could tolerate getting a client proxy to a different service instance.) The client proxy itself within the rental struct should NOT be disposed directly since it can be shared across invocations of this method.

Type Parameters

T

The type of client proxy to create.

Exceptions

ServiceCompositionException

Thrown when a service discovery or activation error occurs.

Events

Invalidated

Occurs when previously acquired proxies have gone stale.

public event ServiceBrokerClient.ClientProxiesInvalidatedEventHandler? Invalidated

Event Type

ServiceBrokerClient.ClientProxiesInvalidatedEventHandler

Remarks

Handlers should release any outstanding rentals at their earliest convenience and use GetProxyAsync<T>(ServiceRpcDescriptor, CancellationToken) to get new proxies. Exceptions thrown or faulted tasks returned by the handler are ignored.

Handlers return a Task to they can carry out asynchronous operations such as acquiring and initializing new services without fear that another invocation of their handler will happen concurrently. Any further invalidation event will await for handlers of the prior event to complete before raising the next one. The CancellationToken provided to the earlier invocation signals that a follow-up event is waiting to be raised to reset the services again. Note however that even if the event handler has not yet completed, all calls to GetProxyAsync<T>(ServiceRpcDescriptor, CancellationToken) will always return a proxy to the most current service available.