Interface IDiscoveryClient
Provides access to remote service instances using a discovery server.
public interface IDiscoveryClient
Properties
Description
Gets a human-readable description of this discovery client.
string Description { get; }
Property Value
Methods
GetInstancesAsync(string, CancellationToken)
Gets all service instances associated with the specified service ID from the discovery server.
Task<IList<IServiceInstance>> GetInstancesAsync(string serviceId, CancellationToken cancellationToken)
Parameters
serviceId
stringThe ID of the service to lookup.
cancellationToken
CancellationTokenThe token to monitor for cancellation requests.
Returns
- Task<IList<IServiceInstance>>
The list of remote service instances.
GetLocalServiceInstance()
Gets information used to register the local service instance (this app) to the discovery server.
IServiceInstance? GetLocalServiceInstance()
Returns
- IServiceInstance
The service instance that represents this app, or
null
when unavailable.
GetServiceIdsAsync(CancellationToken)
Gets all registered service IDs from the discovery server.
Task<ISet<string>> GetServiceIdsAsync(CancellationToken cancellationToken)
Parameters
cancellationToken
CancellationTokenThe token to monitor for cancellation requests.
Returns
ShutdownAsync(CancellationToken)
Deregisters the local service (this app) from the discovery server.
Task ShutdownAsync(CancellationToken cancellationToken)
Parameters
cancellationToken
CancellationTokenThe token to monitor for cancellation requests.
Returns
Remarks
This method exists for two reasons, instead of just reusing IAsyncDisposable. The first reason is that this method enables
cancellation. The second reason is more complicated. Deregistration typically requires to send an HTTP request to the discovery server. When using
IHttpClientFactory
, it requires the IoC container to obtain an HttpClient. But that fails when the container is being disposed.
Deregistration must be performed earlier. That's why implementations should register DiscoveryClientHostedService
in the IoC container, which is a
hosted service that performs deregistration (by calling this method) when the app is terminating. At that time, the IoC container is still
accessible.