Interface ICloudflareApiClientFactory
- Namespace
- Cloudflare.NET.Core
- Assembly
- Cloudflare.NET.dll
Defines the contract for a factory that creates named ICloudflareApiClient instances. This allows applications to work with multiple Cloudflare accounts or configurations simultaneously.
public interface ICloudflareApiClientFactory
Remarks
Named clients are registered using the AddCloudflareApiClient(IServiceCollection, string, Action<CloudflareApiOptions>) overload with a name parameter.
// Registration
services.AddCloudflareApiClient("production", options => {
options.ApiToken = "prod-token";
options.AccountId = "prod-account-id";
});
services.AddCloudflareApiClient("staging", options => {
options.ApiToken = "staging-token";
options.AccountId = "staging-account-id";
});
// Usage via factory
public class MyService(ICloudflareApiClientFactory factory)
{
public async Task DoSomething()
{
var prodClient = factory.CreateClient("production");
var stagingClient = factory.CreateClient("staging");
// ...
}
}
Methods
CreateClient(CloudflareApiOptions)
Creates an ICloudflareApiClient instance dynamically from the provided options, without requiring pre-registration in the DI container.
ICloudflareApiClient CreateClient(CloudflareApiOptions options)
Parameters
optionsCloudflareApiOptionsThe 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
optionsis null.- InvalidOperationException
Thrown when the options fail validation (e.g., missing ApiToken).
CreateClient(string)
Creates an ICloudflareApiClient instance configured with the specified named options.
ICloudflareApiClient CreateClient(string name)
Parameters
namestringThe 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.