Class ServiceCollectionExtensions
- Namespace
- Cloudflare.NET.Core
- Assembly
- Cloudflare.NET.dll
Provides extension methods for setting up the Cloudflare API client in an IServiceCollection.
public static class ServiceCollectionExtensions
- Inheritance
-
ServiceCollectionExtensions
- Inherited Members
Methods
AddCloudflareApiClient(IServiceCollection, IConfiguration)
Registers the ICloudflareApiClient and its dependencies using a configuration section.
This is a convenience method that binds to the "Cloudflare" section of the application's IConfiguration.
public static IServiceCollection AddCloudflareApiClient(this IServiceCollection services, IConfiguration configuration)
Parameters
servicesIServiceCollectionThe IServiceCollection to add the services to.
configurationIConfigurationThe application configuration.
Returns
- IServiceCollection
The IServiceCollection so that additional calls can be chained.
AddCloudflareApiClient(IServiceCollection, Action<CloudflareApiOptions>)
Registers the ICloudflareApiClient and its dependencies, allowing for fine-grained programmatic configuration.
This method sets up the necessary HttpClient, authentication handler, and resilience policies for rate limiting and transient error handling.
Configuration is validated at application startup. If required settings (ApiToken) are missing, an OptionsValidationException is thrown with a clear error message indicating what configuration is missing and how to fix it.
public static IServiceCollection AddCloudflareApiClient(this IServiceCollection services, Action<CloudflareApiOptions> configureOptions)
Parameters
servicesIServiceCollectionThe IServiceCollection to add the services to.
configureOptionsAction<CloudflareApiOptions>An action to configure the CloudflareApiOptions.
Returns
- IServiceCollection
The IServiceCollection so that additional calls can be chained.
Exceptions
- OptionsValidationException
Thrown at application startup if required configuration is missing or invalid.
AddCloudflareApiClient(IServiceCollection, string, IConfiguration)
Registers a named ICloudflareApiClient configuration using a configuration section.
Named clients can be retrieved using ICloudflareApiClientFactory or via keyed services using
[FromKeyedServices("name")].
public static IServiceCollection AddCloudflareApiClient(this IServiceCollection services, string name, IConfiguration configuration)
Parameters
servicesIServiceCollectionThe IServiceCollection to add the services to.
namestringThe unique name for this client configuration. Used to retrieve the client from the factory or via keyed services.
configurationIConfigurationThe application configuration. Will bind to the "Cloudflare:" section.
Returns
- IServiceCollection
The IServiceCollection so that additional calls can be chained.
Exceptions
- ArgumentException
Thrown when
nameis null or whitespace.
AddCloudflareApiClient(IServiceCollection, string, Action<CloudflareApiOptions>)
Registers a named ICloudflareApiClient configuration with programmatic options.
Named clients can be retrieved using ICloudflareApiClientFactory or via keyed services using
[FromKeyedServices("name")].
public static IServiceCollection AddCloudflareApiClient(this IServiceCollection services, string name, Action<CloudflareApiOptions> configureOptions)
Parameters
servicesIServiceCollectionThe IServiceCollection to add the services to.
namestringThe unique name for this client configuration. Used to retrieve the client from the factory or via keyed services.
configureOptionsAction<CloudflareApiOptions>An action to configure the CloudflareApiOptions.
Returns
- IServiceCollection
The IServiceCollection so that additional calls can be chained.
Examples
// Register multiple named clients
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";
});
// Use via factory
public class MyService(ICloudflareApiClientFactory factory)
{
public async Task DoSomething()
{
var prodClient = factory.CreateClient("production");
// ...
}
}
// Or use via keyed services
public class MyService([FromKeyedServices("production")] ICloudflareApiClient client)
{
// ...
}
Remarks
Unlike the default client registration, named clients are validated when first created via the factory or keyed services, not at application startup. This is because named configurations may be dynamically added or configured after startup.
Exceptions
- ArgumentException
Thrown when
nameis null or whitespace.- InvalidOperationException
Thrown when the named client is created if required configuration is missing or invalid.