Table of Contents

Class CloudflareApiClientFactory

Namespace
Cloudflare.NET.Core
Assembly
Cloudflare.NET.dll

Factory that creates named ICloudflareApiClient instances. Each named client is configured with its own CloudflareApiOptions and uses a dedicated HttpClient with appropriate authentication and resilience.

public sealed class CloudflareApiClientFactory : ICloudflareApiClientFactory
Inheritance
CloudflareApiClientFactory
Implements
Inherited Members

Remarks

This factory is used to support multiple Cloudflare configurations within a single application, such as connecting to different accounts or using different API tokens.

Named clients are registered using AddCloudflareApiClient(IServiceCollection, string, Action<CloudflareApiOptions>) .

Constructors

CloudflareApiClientFactory(IHttpClientFactory, IOptionsMonitor<CloudflareApiOptions>, ILoggerFactory)

Initializes a new instance of the CloudflareApiClientFactory class.

public CloudflareApiClientFactory(IHttpClientFactory httpClientFactory, IOptionsMonitor<CloudflareApiOptions> optionsMonitor, ILoggerFactory loggerFactory)

Parameters

httpClientFactory IHttpClientFactory

The HTTP client factory.

optionsMonitor IOptionsMonitor<CloudflareApiOptions>

The options monitor for named options.

loggerFactory ILoggerFactory

The logger factory.

Methods

CreateClient(CloudflareApiOptions)

Creates an ICloudflareApiClient instance dynamically from the provided options, without requiring pre-registration in the DI container.

public ICloudflareApiClient CreateClient(CloudflareApiOptions options)

Parameters

options CloudflareApiOptions

The configuration options for the client.

Returns

ICloudflareApiClient

A fully configured ICloudflareApiClient with authentication and resilience (rate limiting, retries, circuit breaker, timeouts).

Examples

// Create a dynamic client for a user-provided account
var options = new CloudflareApiOptions
{
    ApiToken = userProvidedToken,
    AccountId = userProvidedAccountId,
    RateLimiting = new RateLimitingOptions
    {
        IsEnabled = true,
        PermitLimit = 10  // Conservative limit for user accounts
    }
};

var client = factory.CreateClient(options);

Remarks

Use this method when client configurations are not known at application startup, such as when users can add Cloudflare accounts at runtime through a UI.

The returned client manages its own HttpClient instance and should be disposed when no longer needed to release resources. Use a using statement or call Dispose():

using var client = factory.CreateClient(options);
var zones = await client.Zones.ListZonesAsync();
// Client is disposed when the using scope ends

Each dynamic client has its own isolated resilience pipeline (rate limiter, circuit breaker, etc.). Dynamic clients do not share state with pre-registered named clients or other dynamic clients.

Exceptions

ArgumentNullException

Thrown when options is null.

InvalidOperationException

Thrown when the options fail validation (e.g., missing ApiToken).

CreateClient(string)

Creates an ICloudflareApiClient instance configured with the specified named options.

public ICloudflareApiClient CreateClient(string name)

Parameters

name string

The name of the client configuration to use. This must match the name used during registration.

Returns

ICloudflareApiClient

A new ICloudflareApiClient instance configured with the named options.

Exceptions

InvalidOperationException

Thrown when no client with the specified name has been registered.