Skip to main content

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

objectConsulClient

Implements

IConsulClient , IDisposable

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

IACLEndpoint

ACLReplication

ACLReplication returns a handle to the ACLReplication endpoints

public IACLReplicationEndpoint ACLReplication { get; }

Property Value

IACLReplicationEndpoint

Agent

Agent returns a handle to the agent endpoints

public IAgentEndpoint Agent { get; }

Property Value

IAgentEndpoint

AuthMethod

AuthMethod returns a handle to the ACL AuthMethod endpoints

public IAuthMethodEndpoint AuthMethod { get; }

Property Value

IAuthMethodEndpoint

Catalog

Catalog returns a handle to the catalog endpoints

public ICatalogEndpoint Catalog { get; }

Property Value

ICatalogEndpoint

Config

public ConsulClientConfiguration Config { get; }

Property Value

ConsulClientConfiguration

Configuration

ConsulClient returns a handle to the catalog endpoints

public IConfigurationEndpoint Configuration { get; }

Property Value

IConfigurationEndpoint

Connect

Connect returns a handle to the Connect endpoints

public IConnectEndpoint Connect { get; }

Property Value

IConnectEndpoint

Coordinate

Session returns a handle to the session endpoints

public ICoordinateEndpoint Coordinate { get; }

Property Value

ICoordinateEndpoint

DiscoveryChain

DiscoveryChain returns a handle to the discovery chain endpoints

public IDiscoveryChainEndpoint DiscoveryChain { get; }

Property Value

IDiscoveryChainEndpoint

Event

Event returns a handle to the event endpoints

public IEventEndpoint Event { get; }

Property Value

IEventEndpoint

Health

Health returns a handle to the health endpoint

public IHealthEndpoint Health { get; }

Property Value

IHealthEndpoint

KV

KV returns a handle to the KV endpoint

public IKVEndpoint KV { get; }

Property Value

IKVEndpoint

Namespaces

Namespaces returns a handle to the namespaces endpoint

public INamespacesEndpoint Namespaces { get; }

Property Value

INamespacesEndpoint

Operator

Operator returns a handle to the operator endpoints.

public IOperatorEndpoint Operator { get; }

Property Value

IOperatorEndpoint

Policy

Policy returns a handle to the ACL Policy endpoints

public IPolicyEndpoint Policy { get; }

Property Value

IPolicyEndpoint

PreparedQuery

Catalog returns a handle to the catalog endpoints

public IPreparedQueryEndpoint PreparedQuery { get; }

Property Value

IPreparedQueryEndpoint

Raw

Raw returns a handle to query endpoints

public IRawEndpoint Raw { get; }

Property Value

IRawEndpoint

Role

Role returns a handle to the ACL Role endpoints

public IRoleEndpoint Role { get; }

Property Value

IRoleEndpoint

Session

Session returns a handle to the session endpoint

public ISessionEndpoint Session { get; }

Property Value

ISessionEndpoint

Snapshot

Catalog returns a handle to the snapshot endpoints

public ISnapshotEndpoint Snapshot { get; }

Property Value

ISnapshotEndpoint

Status

Status returns a handle to the status endpoint

public IStatusEndpoint Status { get; }

Property Value

IStatusEndpoint

Token

Token returns a handle to the ACL Token endpoints

public ITokenEndpoint Token { get; }

Property Value

ITokenEndpoint

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

ct CancellationToken

The cancellation token

Returns

Task <IDistributedLock >

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

ct CancellationToken

The cancellation token

Returns

Task <IDistributedLock >

AcquireSemaphore(string, int, CancellationToken)

public Task<IDistributedSemaphore> AcquireSemaphore(string prefix, int limit, CancellationToken ct = default)

Parameters

prefix string

limit int

ct CancellationToken

Returns

Task <IDistributedSemaphore >

AcquireSemaphore(SemaphoreOptions, CancellationToken)

public Task<IDistributedSemaphore> AcquireSemaphore(SemaphoreOptions opts, CancellationToken ct = default)

Parameters

opts SemaphoreOptions

ct CancellationToken

Returns

Task <IDistributedSemaphore >

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

IDistributedLock

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

IDistributedLock

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

ct CancellationToken

Returns

Task

ExecuteInSemaphore(SemaphoreOptions, Action, CancellationToken)

public Task ExecuteInSemaphore(SemaphoreOptions opts, Action a, CancellationToken ct = default)

Parameters

opts SemaphoreOptions

a Action

ct CancellationToken

Returns

Task

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

ct CancellationToken

The cancellation token

Returns

Task

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

ct CancellationToken

The cancellation token

Returns

Task

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

ct CancellationToken

The cancellation token

action Action

Returns

Task

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

ct CancellationToken

The cancellation token

action Action

Returns

Task

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

IDistributedSemaphore

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

IDistributedSemaphore

An unlocked semaphore

  • Constructors
    • ConsulClient()
    • ConsulClient(Action<ConsulClientConfiguration>)
    • ConsulClient(Action<ConsulClientConfiguration>, Action<HttpClient>)
    • ConsulClient(Action<ConsulClientConfiguration>, Action<HttpClient>, Action<HttpClientHandler>)
    • ConsulClient(ConsulClientConfiguration)
    • ConsulClient(ConsulClientConfiguration, HttpClient)
  • Properties
    • ACL
    • ACLReplication
    • Agent
    • AuthMethod
    • Catalog
    • Config
    • Configuration
    • Connect
    • Coordinate
    • DiscoveryChain
    • Event
    • Health
    • KV
    • Namespaces
    • Operator
    • Policy
    • PreparedQuery
    • Raw
    • Role
    • Session
    • Snapshot
    • Status
    • Token
  • Methods
    • AcquireLock(string, CancellationToken)
    • AcquireLock(LockOptions, CancellationToken)
    • AcquireSemaphore(string, int, CancellationToken)
    • AcquireSemaphore(SemaphoreOptions, CancellationToken)
    • CheckDisposed()
    • CreateLock(string)
    • CreateLock(LockOptions)
    • Dispose(bool)
    • Dispose()
    • ExecuteInSemaphore(string, int, Action, CancellationToken)
    • ExecuteInSemaphore(SemaphoreOptions, Action, CancellationToken)
    • ExecuteLocked(string, Action, CancellationToken)
    • ExecuteLocked(LockOptions, Action, CancellationToken)
    • ExecuteLocked(string, CancellationToken, Action)
    • ExecuteLocked(LockOptions, CancellationToken, Action)
    • Semaphore(string, int)
    • Semaphore(SemaphoreOptions)