Class ConsulClient
Namespace: Consul
Assembly: Consul.dll
Represents a persistant connection to a Consul agent. Instances of this class should be created rarely and reused often.
public class ConsulClient : IConsulClient, IDisposable
Inheritance
Implements
Inherited Members
object.Equals(object) , object.Equals(object, object) , object.GetHashCode() , object.GetType() , object.MemberwiseClone() , object.ReferenceEquals(object, object) , object.ToString()
Constructors
ConsulClient()
Initializes a new Consul client with a default configuration that connects to 127.0.0.1:8500.
public ConsulClient()
ConsulClient(Action<ConsulClientConfiguration>)
Initializes a new Consul client with the ability to set portions of the configuration.
public ConsulClient(Action<ConsulClientConfiguration> configOverride)
Parameters
configOverride
Action <ConsulClientConfiguration >
The Action to modify the default configuration with
ConsulClient(Action<ConsulClientConfiguration>, Action<HttpClient>)
Initializes a new Consul client with the ability to set portions of the configuration and access the underlying HttpClient for modification. The HttpClient is modified to set options like the request timeout and headers. The Timeout property also applies to all long-poll requests and should be set to a value that will encompass all successful requests.
public ConsulClient(Action<ConsulClientConfiguration> configOverride, Action<HttpClient> clientOverride)
Parameters
configOverride
Action <ConsulClientConfiguration >
The Action to modify the default configuration with
clientOverride
Action <HttpClient >
The Action to modify the HttpClient with
ConsulClient(Action<ConsulClientConfiguration>, Action<HttpClient>, Action<HttpClientHandler>)
Initializes a new Consul client with the ability to set portions of the configuration and access the underlying HttpClient and WebRequestHandler for modification. The HttpClient is modified to set options like the request timeout and headers. The WebRequestHandler is modified to set options like Proxy and Credentials. The Timeout property also applies to all long-poll requests and should be set to a value that will encompass all successful requests.
public ConsulClient(Action<ConsulClientConfiguration> configOverride, Action<HttpClient> clientOverride, Action<HttpClientHandler> handlerOverride)
Parameters
configOverride
Action <ConsulClientConfiguration >
The Action to modify the default configuration with
clientOverride
Action <HttpClient >
The Action to modify the HttpClient with
handlerOverride
Action <HttpClientHandler >
The Action to modify the WebRequestHandler with
ConsulClient(ConsulClientConfiguration)
Initializes a new Consul client with the configuration specified.
public ConsulClient(ConsulClientConfiguration config)
Parameters
config
ConsulClientConfiguration
A Consul client configuration
ConsulClient(ConsulClientConfiguration, HttpClient)
Initializes a new Consul client with the configuration specified and a custom HttpClient, which is useful for setting proxies/custom timeouts. The HttpClient must accept the "application/json" content type and the Timeout property should be set to at least 15 minutes to allow for blocking queries.
public ConsulClient(ConsulClientConfiguration config, HttpClient client)
Parameters
config
ConsulClientConfiguration
A Consul client configuration
client
HttpClient
A custom HttpClient. The lifetime, including disposal, of this HttpClient is not handled by ConsulClient
Properties
ACL
[Deprecated] ACL returns a handle to the ACL endpoints
[Obsolete("The Legacy ACL system has been deprecated, please use Token, Role and Policy instead.")]
public IACLEndpoint ACL { get; }
Property Value
ACLReplication
ACLReplication returns a handle to the ACLReplication endpoints
public IACLReplicationEndpoint ACLReplication { get; }
Property Value
Agent
Agent returns a handle to the agent endpoints
public IAgentEndpoint Agent { get; }
Property Value
AuthMethod
AuthMethod returns a handle to the ACL AuthMethod endpoints
public IAuthMethodEndpoint AuthMethod { get; }
Property Value
Catalog
Catalog returns a handle to the catalog endpoints
public ICatalogEndpoint Catalog { get; }
Property Value
Config
public ConsulClientConfiguration Config { get; }
Property Value
Configuration
ConsulClient returns a handle to the catalog endpoints
public IConfigurationEndpoint Configuration { get; }
Property Value
Connect
Connect returns a handle to the Connect endpoints
public IConnectEndpoint Connect { get; }
Property Value
Coordinate
Session returns a handle to the session endpoints
public ICoordinateEndpoint Coordinate { get; }
Property Value
DiscoveryChain
DiscoveryChain returns a handle to the discovery chain endpoints
public IDiscoveryChainEndpoint DiscoveryChain { get; }
Property Value
Event
Event returns a handle to the event endpoints
public IEventEndpoint Event { get; }
Property Value
Health
Health returns a handle to the health endpoint
public IHealthEndpoint Health { get; }
Property Value
KV
KV returns a handle to the KV endpoint
public IKVEndpoint KV { get; }
Property Value
Namespaces
Namespaces returns a handle to the namespaces endpoint
public INamespacesEndpoint Namespaces { get; }
Property Value
Operator
Operator returns a handle to the operator endpoints.
public IOperatorEndpoint Operator { get; }
Property Value
Policy
Policy returns a handle to the ACL Policy endpoints
public IPolicyEndpoint Policy { get; }
Property Value
PreparedQuery
Catalog returns a handle to the catalog endpoints
public IPreparedQueryEndpoint PreparedQuery { get; }
Property Value
Raw
Raw returns a handle to query endpoints
public IRawEndpoint Raw { get; }
Property Value
Role
Role returns a handle to the ACL Role endpoints
public IRoleEndpoint Role { get; }
Property Value
Session
Session returns a handle to the session endpoint
public ISessionEndpoint Session { get; }
Property Value
Snapshot
Catalog returns a handle to the snapshot endpoints
public ISnapshotEndpoint Snapshot { get; }
Property Value
Status
Status returns a handle to the status endpoint
public IStatusEndpoint Status { get; }
Property Value
Token
Token returns a handle to the ACL Token endpoints
public ITokenEndpoint Token { get; }
Property Value
Methods
AcquireLock(string, CancellationToken)
AcquireLock creates a lock that is already acquired when this call returns.
public Task<IDistributedLock> AcquireLock(string key, CancellationToken ct = default)
Parameters
key
string
The cancellation token
Returns
AcquireLock(LockOptions, CancellationToken)
AcquireLock creates a lock that is already acquired when this call returns.
public Task<IDistributedLock> AcquireLock(LockOptions opts, CancellationToken ct = default)
Parameters
opts
LockOptions
The cancellation token
Returns
AcquireSemaphore(string, int, CancellationToken)
public Task<IDistributedSemaphore> AcquireSemaphore(string prefix, int limit, CancellationToken ct = default)
Parameters
prefix
string
limit
int
Returns
AcquireSemaphore(SemaphoreOptions, CancellationToken)
public Task<IDistributedSemaphore> AcquireSemaphore(SemaphoreOptions opts, CancellationToken ct = default)
Parameters
opts
SemaphoreOptions
Returns
CheckDisposed()
public void CheckDisposed()
CreateLock(string)
CreateLock returns an unlocked lock which can be used to acquire and release the mutex. The key used must have write permissions.
public IDistributedLock CreateLock(string key)
Parameters
key
string
Returns
CreateLock(LockOptions)
CreateLock returns an unlocked lock which can be used to acquire and release the mutex. The key used must have write permissions.
public IDistributedLock CreateLock(LockOptions opts)
Parameters
opts
LockOptions
Returns
Dispose(bool)
protected virtual void Dispose(bool disposing)
Parameters
disposing
bool
Dispose()
public void Dispose()
ExecuteInSemaphore(string, int, Action, CancellationToken)
public Task ExecuteInSemaphore(string prefix, int limit, Action a, CancellationToken ct = default)
Parameters
prefix
string
limit
int
a
Action
Returns
ExecuteInSemaphore(SemaphoreOptions, Action, CancellationToken)
public Task ExecuteInSemaphore(SemaphoreOptions opts, Action a, CancellationToken ct = default)
Parameters
opts
SemaphoreOptions
a
Action
Returns
ExecuteLocked(string, Action, CancellationToken)
ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
public Task ExecuteLocked(string key, Action action, CancellationToken ct = default)
Parameters
key
string
action
Action
The cancellation token
Returns
ExecuteLocked(LockOptions, Action, CancellationToken)
ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
public Task ExecuteLocked(LockOptions opts, Action action, CancellationToken ct = default)
Parameters
opts
LockOptions
action
Action
The cancellation token
Returns
ExecuteLocked(string, CancellationToken, Action)
ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
[Obsolete("This method will be removed in a future release. Replace calls with the method signature ExecuteLocked(string, Action, CancellationToken)")]
public Task ExecuteLocked(string key, CancellationToken ct, Action action)
Parameters
key
string
The cancellation token
action
Action
Returns
ExecuteLocked(LockOptions, CancellationToken, Action)
ExecuteLock accepts a delegate to execute in the context of a lock, releasing the lock when completed.
[Obsolete("This method will be removed in a future release. Replace calls with the method signature ExecuteLocked(LockOptions, Action, CancellationToken)")]
public Task ExecuteLocked(LockOptions opts, CancellationToken ct, Action action)
Parameters
opts
LockOptions
The cancellation token
action
Action
Returns
Semaphore(string, int)
Used to created a Semaphore which will operate at the given KV prefix and uses the given limit for the semaphore. The prefix must have write privileges, and the limit must be agreed upon by all contenders.
public IDistributedSemaphore Semaphore(string prefix, int limit)
Parameters
prefix
string
The keyspace prefix (e.g. "locks/semaphore")
limit
int
The number of available semaphore slots
Returns
An unlocked semaphore
Semaphore(SemaphoreOptions)
SemaphoreOpts is used to create a Semaphore with the given options. The prefix must have write privileges, and the limit must be agreed upon by all contenders. If a Session is not provided, one will be created.
public IDistributedSemaphore Semaphore(SemaphoreOptions opts)
Parameters
opts
SemaphoreOptions
The semaphore options
Returns
An unlocked semaphore