Table of Contents

Class ServiceCollectionExtensions

Namespace
Cloudflare.NET.Analytics
Assembly
Cloudflare.NET.Analytics.dll

Provides extension methods for setting up the Cloudflare Analytics API client in an IServiceCollection.

public static class ServiceCollectionExtensions
Inheritance
ServiceCollectionExtensions
Inherited Members

Methods

AddCloudflareAnalytics(IServiceCollection)

Registers the IAnalyticsApi and its dependencies with the service collection.

This method should be called after AddCloudflareApiClient(IServiceCollection, IConfiguration) , as it relies on the core CloudflareApiOptions for authentication and endpoint configuration.

Configuration is validated at application startup. If required settings (ApiToken, GraphQlApiUrl) are missing, an OptionsValidationException is thrown with a clear error message indicating what configuration is missing and how to fix it.

public static IServiceCollection AddCloudflareAnalytics(this IServiceCollection services)

Parameters

services IServiceCollection

The IServiceCollection to add the services to.

Returns

IServiceCollection

The IServiceCollection so that additional calls can be chained.

Exceptions

OptionsValidationException

Thrown at application startup if required configuration is missing or invalid.

AddCloudflareAnalytics(IServiceCollection, string)

Registers a named IAnalyticsApi configuration.

Named clients can be retrieved using IAnalyticsApiFactory or via keyed services using [FromKeyedServices("name")].

public static IServiceCollection AddCloudflareAnalytics(this IServiceCollection services, string name)

Parameters

services IServiceCollection

The IServiceCollection to add the services to.

name string

The unique name for this client configuration. This must match the name used when registering the corresponding CloudflareApiOptions via AddCloudflareApiClient(IServiceCollection, string, Action<CloudflareApiOptions>) .

Returns

IServiceCollection

The IServiceCollection so that additional calls can be chained.

Examples

// Register multiple named clients
services.AddCloudflareApiClient("account1", options => {
    options.ApiToken = "token1";
    options.AccountId = "account1-id";
});
services.AddCloudflareAnalytics("account1");

services.AddCloudflareApiClient("account2", options => {
    options.ApiToken = "token2";
    options.AccountId = "account2-id";
});
services.AddCloudflareAnalytics("account2");

// Use via factory
public class MyService(IAnalyticsApiFactory factory)
{
    public async Task DoSomething()
    {
        var account1Api = factory.CreateClient("account1");
        // ...
    }
}

// Or use via keyed services
public class MyService([FromKeyedServices("account1")] IAnalyticsApi api)
{
    // ...
}

Remarks

This method requires that the Cloudflare API options for the same name are registered using AddCloudflareApiClient(IServiceCollection, string, Action<CloudflareApiOptions>) . The API token and GraphQL URL from those options are used for authentication and endpoint configuration.

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 name is null or whitespace.

InvalidOperationException

Thrown when the named client is created if required configuration is missing or invalid.