# Using the ConfigCat SDK in DI‑Based .NET Applications

Copy page

<!-- -->

<!-- -->

<!-- -->

<!-- -->

[![Star on GitHub](https://img.shields.io/github/stars/configcat/.net-sdk.svg?style=social)](https://github.com/configcat/.net-sdk/stargazers) [![Build status](https://github.com/configcat/.net-sdk/actions/workflows/dotnet-sdk-ci.yml/badge.svg?branch=master)](https://github.com/configcat/.net-sdk/actions/workflows/dotnet-sdk-ci.yml) [![NuGet Version](https://img.shields.io/nuget/v/ConfigCat.Extensions.Hosting)](https://www.nuget.org/packages/ConfigCat.Extensions.Hosting/) [![Sonar Coverage](https://img.shields.io/sonar/coverage/net-sdk?logo=SonarCloud\&server=https%3A%2F%2Fsonarcloud.io)](https://sonarcloud.io/project/overview?id=net-sdk) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=net-sdk\&metric=alert_status)](https://sonarcloud.io/dashboard?id=net-sdk)

<!-- -->

info

As an alternative to using the [core ConfigCat SDK for .NET](https://configcat.com/docs/sdk-reference/dotnet.md), this approach offers a more streamlined integration for the following types of .NET applications:

* [ASP.NET Core](https://dotnet.microsoft.com/en-us/apps/aspnet)
* [Blazor](https://dotnet.microsoft.com/en-us/apps/aspnet/web-apps/blazor)
* [MAUI](https://dotnet.microsoft.com/en-us/apps/maui)
* [Worker Services](https://learn.microsoft.com/en-us/dotnet/core/extensions/workers)
* Any other application built on [.NET Generic Host](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host) (`Microsoft.Extensions.Hosting`) or [.NET's standard dependency injection](https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection/overview) (`Microsoft.Extensions.DependencyInjection`).

[ConfigCat .NET SDK on GitHub](https://github.com/configcat/.net-sdk)

## Getting started[​](#getting-started "Direct link to Getting started")

### 1. Install *ConfigCat SDK* NuGet package[​](#1-install-configcat-sdk-nuget-package "Direct link to 1-install-configcat-sdk-nuget-package")

<!-- -->

* .NET CLI
* Powershell / NuGet Package Manager Console

First, install the [integration NuGet package](https://www.nuget.org/packages/ConfigCat.Extensions.Hosting), which builds on top of the [core ConfigCat SDK package](https://www.nuget.org/packages/ConfigCat.Client):

```text
dotnet add package ConfigCat.Extensions.Hosting

```

First, install the [integration NuGet package](https://www.nuget.org/packages/ConfigCat.Extensions.Hosting), which builds on top of the [core ConfigCat SDK package](https://www.nuget.org/packages/ConfigCat.Client):

```powershell
Install-Package ConfigCat.Extensions.Hosting

```

### 2. Import package[​](#2-import-package "Direct link to 2. Import package")

<!-- -->

```csharp
using ConfigCat.Client;
using ConfigCat.Extensions.Hosting;

```

<!-- -->

### 3. Register the *ConfigCat* client with your *SDK Key*[​](#3-register-the-configcat-client-with-your-sdk-key "Direct link to 3-register-the-configcat-client-with-your-sdk-key")

* IHostApplicationBuilder
* IHostBuilder
* Plain DI

If your application uses the [modern, linear, property-based configuration style](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host?tabs=appbuilder#host-builder-options), i.e., if it performs setup via a host builder implementing [IHostApplicationBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostapplicationbuilder) (e.g., ASP.NET Core's [WebApplicationBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.webapplicationbuilder) or MAUI's [MauiAppBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.maui.hosting.mauiappbuilder)), add this to your application setup:

```csharp
builder.UseConfigCat();

```

Then register the default *ConfigCat client* (more specifically, the `IConfigCatClient` service) in the DI container by specifying your SDK Key in `appsettings.json` (or via other application configuration, such as environment variables):

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
    }
  }
}

```

Alternatively, it is possible to register the client by code:

```csharp
var configCatBuilder = builder.UseConfigCat();
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
});

```

If your application uses the [traditional, callback-based approach](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host?tabs=appbuilder#host-builder-options), i.e., if it performs setup via a host builder implementing [IHostBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.hosting.ihostbuilder), add this to your application setup:

```csharp
builder.ConfigureConfigCat();

```

Then register the default *ConfigCat client* (more specifically, the `IConfigCatClient` service) in the DI container by specifying your SDK Key in `appsettings.json` (or via other application configuration, such as environment variables):

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
    }
  }
}

```

Alternatively, it is possible to register the client by code:

```csharp
builder.ConfigureConfigCat(configCatBuilder =>
{
    configCatBuilder.AddDefaultClient(options =>
    {
      options.SdkKey = "#YOUR-SDK-KEY#";
    });
});

```

If your application performs setup via a host builder not compatible with [.NET Generic Host](https://learn.microsoft.com/en-us/dotnet/core/extensions/generic-host) (e.g., Blazor's [WebAssemblyHostBuilder](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.webassembly.hosting.webassemblyhostbuilder)), or manually builds a DI container using [ServiceCollection](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollection), add this to your application setup:

```csharp
services.AddConfigCat(configuration);

```

Then register the default *ConfigCat client* (more specifically, the `IConfigCatClient` service) in the DI container by specifying your SDK Key in `appsettings.json` (or via other application configuration, such as environment variables):

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
    }
  }
}

```

Alternatively, it is possible to register the client by code:

```csharp
services.AddConfigCat(configCatBuilder =>
{
  configCatBuilder.AddDefaultClient(options =>
  {
      options.SdkKey = "#YOUR-SDK-KEY#";
  });
});

```

Or, if you need both application configuration and setup by code:

```csharp
services.AddConfigCat(configuration, configCatBuilder => { /* ... */ });

```

If your application needs to use multiple ConfigCat configs, it is possible to register additional named clients as follows:

```json
{
  "ConfigCat": {
    "NamedClients": {
      "secondary": {
        "SdkKey": "#YOUR-SECONDARY-SDK-KEY#"
      }
    }
  }
}

```

Alternatively, by code:

```csharp
configCatBuilder.AddNamedClient("secondary", options =>
{
    options.SdkKey = "#YOUR-SECONDARY-SDK-KEY#";
});

```

Named clients are registered as [keyed services](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection#keyed-services), using the client name as the service key. See [this section](#5-obtain-the-configcat-client) on how to obtain such services.

### 4. Initialize the *ConfigCat* client[​](#4-initialize-the-configcat-client "Direct link to 4-initialize-the-configcat-client")

Although not necessarily needed, it is highly recommended to initialize the registered client(s) at application startup to surface errors caused by potential misconfiguration (e.g., invalid/mistyped SDK Keys) early. In addition, [synchronous feature flag evaluation](#snapshots-and-non-blocking-synchronous-feature-flag-evaluation) also requires the clients to reach the ready state at startup.

The SDK offers the following initialization modes:

* `ConfigCatInitMode.DoNotWaitForClientReady`: Ensures that the registered clients are created by resolving them from the DI container but does not wait for the clients to reach the ready state. This is the default mode, which is sufficient if you intend to use the [asynchronous API for feature flag evaluation](#anatomy-of-getvalueasync).
* `ConfigCatInitMode.WaitForClientReady`: Ensures that the registered clients are created by resolving them from the DI container and waits for all clients to reach the ready state. (Plus, the `throwOnFailure` parameter allows you to control how to proceed when any of the clients fail to initialize.) Choose this mode (along with [Auto Polling](#auto-polling-default)) if you want to use the [synchronous API for feature flag evaluation](#snapshots-and-non-blocking-synchronous-feature-flag-evaluation).

One way to specify the initialization mode is application configuration:

```json
{
  "ConfigCat": {
    "Init": {
      "Mode": "WaitForClientReady",
      "ThrowOnFailure": false
    }
  }
}

```

Alternatively, by code:

```csharp
configCatBuilder.UseInitMode(new ConfigCatInitMode.WaitForClientReady(throwOnFailure: false));

```

The `UseConfigCat` and `ConfigureConfigCat` methods automatically ensure that the registered client(s) are initialized at startup if your application host supports [hosted services](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services) (e.g., ASP.NET Core's host does, but MAUI's host [does not at the moment](https://github.com/dotnet/maui/issues/2244)).

In other cases, you need to perform initialization manually, in the startup phase of your application, after the DI container is built. E.g.:

```csharp
var app = builder.Build();

await app.Services.GetRequiredService<IConfigCatInitializer>().InitializeAsync();

```

### 5. Obtain the *ConfigCat* client[​](#5-obtain-the-configcat-client "Direct link to 5-obtain-the-configcat-client")

The singleton `IConfigCatClient` service can be resolved from the DI container

* using constructor injection, e.g.:

  ```csharp
  [ApiController, Route("[controller]")]
  public class SampleController(
      IConfigCatClient defaultClient,
      [FromKeyedServices("secondary")] IConfigCatClient secondaryClient)
      : ControllerBase
  {
    /* ... */
  }

  ```

* using method injection, e.g.:

  ```csharp
  [ApiController, Route("[controller]")]
  public class SampleController : ControllerBase
  {
      [HttpGet]
      public async Task<IActionResult> Get(
          [FromServices] IConfigCatClient defaultClient,
          [FromKeyedServices("secondary")] IConfigCatClient secondaryClient)
      {
        /* ... */
      }
  }

  ```

* using property injection, e.g.:

  ```razor
  @using ConfigCat.Client

  @inject IConfigCatClient DefaultClient

  @* Razor syntax does not support keyed services at the moment.
     See also: https://github.com/dotnet/razor/issues/9286 *@

  @code {
      // Alternatively, using the Inject attribute:

      [Inject]
      private IConfigCatClient DefaultClient { get; set; }

      [Inject(Key = "secondary")]
      private IConfigCatClient SecondaryClient { get; set; }
  }

  ```

* directly, e.g.:

  ```csharp
  var defaultClient = ServiceProvider.GetRequiredService<IConfigCatClient>();
  var secondaryClient = ServiceProvider.GetRequiredKeyedService<IConfigCatClient>("secondary");

  ```

### 6. Get your setting value[​](#6-get-your-setting-value "Direct link to 6. Get your setting value")

```csharp
var isMyAwesomeFeatureEnabled = await client.GetValueAsync("isMyAwesomeFeatureEnabled", false);
if (isMyAwesomeFeatureEnabled)
{
    doTheNewThing();
}
else
{
    doTheOldThing();
}

```

The *ConfigCat SDK* also offers a synchronous API for feature flag evaluation. Read more [here](#snapshots-and-non-blocking-synchronous-feature-flag-evaluation).

<!-- -->

## About the *ConfigCat Client*[​](#about-the-configcat-client "Direct link to about-the-configcat-client")

*ConfigCat Client* is responsible for:

* managing the communication between your application and ConfigCat servers.
* caching your setting values and feature flags.
* serving values quickly in a failsafe way.

<!-- -->

### Customizing the *ConfigCat Client*[​](#customizing-the-configcat-client "Direct link to customizing-the-configcat-client")

<!-- -->

To customize the client's behavior, you can specify additional settings in `appsettings.json` (or via other application configuration, such as environment variables):

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
      "Polling": {
        "Mode": "AutoPoll",
        "MaxInitWaitTime": "00:00:10"
      }
    }
  }
}

```

Alternatively, you can specify additional settings by code (using the `AddDefaultClient` and `AddNamedClient` methods):

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.PollingMode = PollingModes.AutoPoll(maxInitWaitTime: TimeSpan.FromSeconds(10));
});

```

info

If you add a client by code that is already defined in the application configuration (e.g. `appsettings.json`), the settings from the configuration and the settings made by code will be merged, with the latter taking precedence.

These are the available options on the `ConfigCatClientOptions` class:

| Properties       | Description                                                                                                                                                                                                                                                                                                                                | Default                                                                                                                                                   |
| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `PollingMode`    | Optional, sets the polling mode for the client. [More about polling modes](#polling-modes).                                                                                                                                                                                                                                                | `PollingModes.AutoPoll()`                                                                                                                                 |
| `ConfigFetcher`  | Optional, [`IConfigCatConfigFetcher`](https://github.com/configcat/.net-sdk/blob/master/src/ConfigCatClient/ConfigService/IConfigCatConfigFetcher.cs) instance for downloading a config.<!-- --> (Can only be set by code.)                                                                                                                | [`HttpClientConfigFetcher`](https://github.com/configcat/.net-sdk/blob/master/src/ConfigCatClient/ConfigService/HttpClientConfigFetcher.cs)               |
| `ConfigCache`    | Optional, [`IConfigCatCache`](https://github.com/configcat/.net-sdk/blob/master/src/ConfigCatClient/Cache/IConfigCatCache.cs) instance for caching the downloaded config.<!-- --> (Can only be set by code.)                                                                                                                               | [`InMemoryConfigCache`](https://github.com/configcat/.net-sdk/blob/master/src/ConfigCatClient/Cache/InMemoryConfigCache.cs)                               |
| `Logger`         | Optional, [`IConfigCatLogger`](https://github.com/configcat/.net-sdk/blob/master/src/ConfigCatClient/Logging/IConfigCatLogger.cs) instance for tracing.<!-- --> (Can only be set by code.)                                                                                                                                                 | [`ConfigCatToMSLoggerAdapter`](https://github.com/configcat/.net-sdk/blob/master/src/ConfigCat.Extensions.Hosting/Adapters/ConfigCatToMSLoggerAdapter.cs) |
| `LogFilter`      | Optional, sets a custom log filter. [More about log filtering](#log-filtering).<!-- --> (Can only be set by code.)                                                                                                                                                                                                                         | `null` (none)                                                                                                                                             |
| `BaseUrl`        | Optional, sets the CDN base url (forward proxy, dedicated subscription) from where the SDK will download the config JSON.                                                                                                                                                                                                                  |                                                                                                                                                           |
| `Proxy`          | Optional, [`IWebProxy`](https://learn.microsoft.com/en-us/dotnet/api/system.net.iwebproxy) instance that provides settings for routing HTTP requests made by the SDK through an HTTP, HTTPS, SOCKS, etc. proxy. [More about the proxy settings](#using-configcat-behind-a-proxy). (Applies only if the `ConfigFetcher` option is not set.) |                                                                                                                                                           |
| `HttpTimeout`    | Optional, sets the underlying HTTP client's timeout. [More about the HTTP timeout](#http-timeout). (Applies only if the `ConfigFetcher` option is not set.)                                                                                                                                                                                | `TimeSpan.FromSeconds(30)`                                                                                                                                |
| `FlagOverrides`  | Optional, sets the local feature flag & setting overrides. [More about feature flag overrides](#flag-overrides).<!-- --> (Can only be set by code.)                                                                                                                                                                                        |                                                                                                                                                           |
| `DataGovernance` | Optional, describes the location of your feature flag and setting data within the ConfigCat CDN. This parameter needs to be in sync with your Data Governance preferences. [More about Data Governance](https://configcat.com/docs/advanced/data-governance.md). Available options: `Global`, `EuOnly`                                     | `Global`                                                                                                                                                  |
| `DefaultUser`    | Optional, sets the default user. [More about default user](#default-user).<!-- --> (Can only be set by code.)                                                                                                                                                                                                                              | `null` (none)                                                                                                                                             |
| `Offline`        | Optional, determines whether the client should be initialized to offline mode. [More about offline mode](#online--offline-mode).                                                                                                                                                                                                           | `false`                                                                                                                                                   |

Via the events provided by `ConfigCatClientOptions` you can also subscribe to the hooks (events) at the time of initialization. [More about hooks](#hooks).

For example:

<!-- -->

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.ClientReady += (s, e) =>
    {
        var keys = ((IConfigCatClient)s).Snapshot().GetAllKeys();
        Console.WriteLine("Client is ready! Number of available feature flags: " + keys.Count);
    };
});

```

## Anatomy of `GetValueAsync()`[​](#anatomy-of-getvalueasync "Direct link to anatomy-of-getvalueasync")

| Parameters     | Description                                                                                                                                       |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `key`          | **REQUIRED.** The key of a specific setting or feature flag. Set on *ConfigCat Dashboard* for each setting.                                       |
| `defaultValue` | **REQUIRED.** This value will be returned in case of an error.                                                                                    |
| `user`         | Optional, *User Object*. Essential when using Targeting. [Read more about Targeting.](https://configcat.com/docs/targeting/targeting-overview.md) |

```csharp
var userObject = new User("#UNIQUE-USER-IDENTIFIER#"); // Optional User Object
var value = await client.GetValueAsync("keyOfMyFeatureFlag", false, userObject);

```

caution

It is important to provide an argument for the `defaultValue` parameter, specifically for the `T` generic type parameter, that matches the type of the feature flag or setting you are evaluating. Please refer to the following table for the corresponding types.

### Setting type mapping[​](#setting-type-mapping "Direct link to Setting type mapping")

| Setting Kind   | Type parameter `T`                |
| -------------- | --------------------------------- |
| On/Off Toggle  | `bool` / `bool?`                  |
| Text           | `string` / `string?`              |
| Whole Number   | `int` / `int?` / `long` / `long?` |
| Decimal Number | `double` / `double?`              |

In addition to the types mentioned above, you also have the option to provide `object` or `object?` for the type parameter regardless of the setting kind. However, this approach is not recommended as it may involve [boxing](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing).

It's important to note that providing any other type for the type parameter will result in an `ArgumentException`.

If you specify an allowed type but it mismatches the setting kind, an error message will be logged and `defaultValue` will be returned.

When relying on type inference and not explicitly specifying the type parameter, be mindful of potential type mismatch issues, especially with number types. For example, `client.GetValueAsync("keyOfMyDecimalSetting", 0)` will return `defaultValue` (`0`) instead of the actual value of the decimal setting because the compiler infers the type as `int` instead of `double`, that is, the call is equivalent to `client.GetValueAsync<int>("keyOfMyDecimalSetting", 0)`, which is a type mismatch.

To correctly evaluate a decimal setting, you should use:

```csharp
var value = await client.GetValueAsync("keyOfMyDecimalSetting", 0.0);
// -or-
var value = await client.GetValueAsync("keyOfMyDecimalSetting", 0d);
// -or-
var value = await client.GetValueAsync<double>("keyOfMyDecimalSetting", 0);

```

## Anatomy of `GetValueDetailsAsync()`[​](#anatomy-of-getvaluedetailsasync "Direct link to anatomy-of-getvaluedetailsasync")

`GetValueDetailsAsync()` is similar to `GetValueAsync()` but instead of returning the evaluated value only, it provides more detailed information about the evaluation result.

| Parameters     | Description                                                                                                                                       |
| -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------- |
| `key`          | **REQUIRED.** The key of a specific setting or feature flag. Set on *ConfigCat Dashboard* for each setting.                                       |
| `defaultValue` | **REQUIRED.** This value will be returned in case of an error.                                                                                    |
| `user`         | Optional, *User Object*. Essential when using Targeting. [Read more about Targeting.](https://configcat.com/docs/targeting/targeting-overview.md) |

```csharp
var userObject = new User("#UNIQUE-USER-IDENTIFIER#"); // Optional User Object
var details = await client.GetValueDetailsAsync("keyOfMyFeatureFlag", false, userObject);

```

caution

It is important to provide an argument for the `defaultValue` parameter, specifically for the `T` generic type parameter, that matches the type of the feature flag or setting you are evaluating. Please refer to [this table](#setting-type-mapping) for the corresponding types.

The `details` result contains the following information:

| Field                     | Type                                 | Description                                                                                                |
| ------------------------- | ------------------------------------ | ---------------------------------------------------------------------------------------------------------- |
| `Key`                     | `string`                             | The key of the evaluated feature flag or setting.                                                          |
| `Value`                   | `bool` / `string` / `int` / `double` | The evaluated value of the feature flag or setting.                                                        |
| `User`                    | `User`                               | The User Object used for the evaluation.                                                                   |
| `IsDefaultValue`          | `bool`                               | True when the default value passed to `GetValueDetailsAsync()` is returned due to an error.                |
| `ErrorCode`               | `EvaluationErrorCode`                | In case of an error, this property contains a code that identifies the reason for the error.               |
| `ErrorMessage`            | `string`                             | In case of an error, this property contains the error message.                                             |
| `ErrorException`          | `Exception`                          | In case of an error, this property contains the related exception object (if any).                         |
| `MatchedTargetingRule`    | `ITargetingRule`                     | The Targeting Rule (if any) that matched during the evaluation and was used to return the evaluated value. |
| `MatchedPercentageOption` | `IPercentageOption`                  | The Percentage Option (if any) that was used to select the evaluated value.                                |
| `FetchTime`               | `DateTime`                           | The last download time (UTC) of the current config.                                                        |

## User Object[​](#user-object "Direct link to User Object")

The [User Object](https://configcat.com/docs/targeting/user-object.md) is essential if you'd like to use ConfigCat's [Targeting](https://configcat.com/docs/targeting/targeting-overview.md) feature.

```csharp
var userObject = new User("#UNIQUE-USER-IDENTIFIER#");

```

```csharp
var userObject = new User("john@example.com");

```

| Parameters | Description                                                                                                                     |
| ---------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `Id`       | **REQUIRED.** Unique identifier of a user in your application. Can be any `string` value, even an email address.                |
| `Email`    | Optional parameter for easier Targeting Rule definitions.                                                                       |
| `Country`  | Optional parameter for easier Targeting Rule definitions.                                                                       |
| `Custom`   | Optional dictionary for custom attributes of a user for advanced Targeting Rule definitions. E.g. User role, Subscription type. |

```csharp
var userObject = new User("#UNIQUE-USER-IDENTIFIER#")
{
    Email = "john@example.com",
    Country = "United Kingdom",
    Custom =
    {
        ["SubscriptionType"] = "Pro",
        ["UserRole"] = "Admin"
    }
};

```

The `Custom` dictionary also allows attribute values other than `string` values:

```csharp
var userObject = new User("#UNIQUE-USER-IDENTIFIER#")
{
    Custom =
    {
        ["Rating"] = 4.5,
        ["RegisteredAt"] = DateTimeOffset.Parse("2023-11-22 12:34:56 +00:00", CultureInfo.InvariantCulture),
        ["Roles"] = new[] { "Role1", "Role2" }
    }
};

```

### User Object Attribute Types[​](#user-object-attribute-types "Direct link to User Object Attribute Types")

All comparators support `string` values as User Object attribute (in some cases they need to be provided in a specific format though, see below), but some of them also support other types of values. It depends on the comparator how the values will be handled. The following rules apply:

**Text-based comparators** (EQUALS, IS ONE OF, etc.)

* accept `string` values,
* all other values are automatically converted to `string` (a warning will be logged but evaluation will continue as normal).

**SemVer-based comparators** (IS ONE OF, <, >=, etc.)

* accept `SemVersion` values containing a preparsed semver value,
* accept `string` values containing a properly formatted, valid semver value,
* all other values are considered invalid (a warning will be logged and the currently evaluated Targeting Rule will be skipped).

**Number-based comparators** (=, <, >=, etc.)

* accept `double` values and all other numeric values which can safely be converted to `double`,
* accept `string` values containing a properly formatted, valid `double` value,
* all other values are considered invalid (a warning will be logged and the currently evaluated Targeting Rule will be skipped).

**Date time-based comparators** (BEFORE / AFTER)

* accept `DateTime` or `DateTimeOffset` values, which are automatically converted to a second-based Unix timestamp,
* accept `double` values representing a second-based Unix timestamp and all other numeric values which can safely be converted to `double`,
* accept `string` values containing a properly formatted, valid `double` value,
* all other values are considered invalid (a warning will be logged and the currently evaluated Targeting Rule will be skipped).

**String array-based comparators** (ARRAY CONTAINS ANY OF / ARRAY NOT CONTAINS ANY OF)

* accept arrays of `string`, `IReadOnlyList<string>` or `IList<string>`,
* accept `string` values containing a valid JSON string which can be deserialized to an array of `string`,
* all other values are considered invalid (a warning will be logged and the currently evaluated Targeting Rule will be skipped).

### Default user[​](#default-user "Direct link to Default user")

It's possible to set a default User Object that will be used on feature flag and setting evaluation. It can be useful when your application has a single user only or rarely switches users.

You can set the default User Object either on SDK initialization:

<!-- -->

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.DefaultUser = new User(identifier: "john@example.com"));
});

```

...or using the `SetDefaultUser()` method of the `ConfigCatClient` object:

```csharp
client.SetDefaultUser(new User(identifier: "john@example.com"));

```

Whenever the evaluation methods like `GetValueAsync()`, `GetValueDetailsAsync()`, etc. are called without an explicit `user` parameter, the SDK will automatically use the default user as a User Object.

```csharp
var user = new User(identifier: "john@example.com");
client.SetDefaultUser(user);

// The default user will be used in the evaluation process.
var value = await client.GetValueAsync(key: "keyOfMyFeatureFlag", defaultValue: false);

```

When a `user` parameter is passed to the evaluation methods, it takes precedence over the default user.

```csharp
var user = new User(identifier: "john@example.com");
client.SetDefaultUser(user);

var otherUser = new User(identifier: "brian@example.com");

// otherUser will be used in the evaluation process.
var value = await client.GetValueAsync(key: "keyOfMyFeatureFlag", defaultValue: false, user: otherUser);

```

You can also remove the default user by doing the following:

```csharp
client.ClearDefaultUser();

```

## Polling Modes[​](#polling-modes "Direct link to Polling Modes")

The *ConfigCat SDK* supports 3 different polling strategies to fetch feature flags and settings from the ConfigCat CDN. Once the latest data is downloaded, it is stored in the cache, then calls to `GetValueAsync()` use the cached data to evaluate feature flags and settings. With the following polling modes, you can customize the SDK to best fit to your application's lifecycle. [More about polling modes.](https://configcat.com/docs/advanced/caching.md)

### Auto polling (default)[​](#auto-polling-default "Direct link to Auto polling (default)")

The *ConfigCat SDK* downloads the latest config data from the ConfigCat CDN automatically every 60 seconds and stores it in the cache.

Use the `PollInterval` option or `pollInterval` parameter to change the polling interval.

<!-- -->

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
      "Polling": {
        "Mode": "AutoPoll",
        "PollInterval": "00:01:35"
      }
    }
  }
}

```

Alternatively, by code:

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.PollingMode = PollingModes.AutoPoll(pollInterval: TimeSpan.FromSeconds(95));
});

```

Available options:

| Option Parameter  | Description                                                                              | Default |
| ----------------- | ---------------------------------------------------------------------------------------- | ------- |
| `pollInterval`    | Polling interval.                                                                        | 60s     |
| `maxInitWaitTime` | Maximum waiting time between the client initialization and the first config acquisition. | 5s      |

### Lazy loading[​](#lazy-loading "Direct link to Lazy loading")

When calling `GetValueAsync()`, the *ConfigCat SDK* downloads the latest config data from the ConfigCat CDN only if it is not already present in the cache, or if the cache has expired. In this case `GetValueAsync()` will return the setting value after the cache is updated.

Use the `CacheTimeToLive` option or `cacheTimeToLive` parameter to set cache lifetime.

<!-- -->

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
      "Polling": {
        "Mode": "LazyLoad",
        "CacheTimeToLive": "00:10:00"
      }
    }
  }
}

```

Alternatively, by code:

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.PollingMode = PollingModes.LazyLoad(cacheTimeToLive: TimeSpan.FromSeconds(600));
});

```

Available options:

| Option Parameter  | Description | Default |
| ----------------- | ----------- | ------- |
| `cacheTimeToLive` | Cache TTL.  | 60s     |

### Manual polling[​](#manual-polling "Direct link to Manual polling")

Manual polling gives you full control over when the config data is downloaded from the ConfigCat CDN. The *ConfigCat SDK* will not download it automatically. Calling `ForceRefreshAsync()` is your application's responsibility.

<!-- -->

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
      "Polling": {
        "Mode": "ManualPoll"
      }
    }
  }
}

```

Alternatively, by code:

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.PollingMode = PollingModes.ManualPoll;
});

```

caution

`GetValueAsync()` returns `defaultValue` if the cache is empty, so <!-- -->once the DI container is built and the `IConfigCatClient` service is resolved<!-- -->, be sure to call `ForceRefreshAsync()` to update the cache.

```csharp
Console.WriteLine(await client.GetValueAsync("keyOfMyTextSetting", "my default value")); // console: "my default value"
await client.ForceRefreshAsync();
Console.WriteLine(await client.GetValueAsync("keyOfMyTextSetting", "my default value")); // console: "value from server"

```

## Hooks[​](#hooks "Direct link to Hooks")

The SDK provides several hooks (events), by means of which you can get notified of its actions. Via the following events you can subscribe to particular events raised by the *ConfigCat* client:

* `event EventHandler<ClientReadyEventArgs> ClientReady`: This event is raised when the client reaches the ready state, i.e. completes initialization.

  * If Lazy Loading or Manual Polling is used, it's considered ready right after the initial sync with the external cache (if any) completes.

  * If Auto Polling is used, the ready state is reached as soon as

    <!-- -->

    * the initial sync with the external cache yields up-to-date config data,
    * otherwise, if the client is online (i.e. HTTP requests are allowed), the first config fetch operation completes (regardless of success or failure),
    * or the time specified via Auto Polling's `maxInitWaitTime` option has passed.

  Reaching the ready state usually means the client is ready to evaluate feature flags and settings. However, please note that this is not guaranteed. In case of initialization failure or timeout, the internal cache may be empty or expired even after the ready state is reported. You can verify this by checking the `CacheState` property of the event arguments.

* `event EventHandler<ConfigFetchedEventArgs> ConfigFetched`: This event is raised each time the client attempts to refresh the cached config by fetching the latest version from the ConfigCat CDN. It is raised not only when `ForceRefreshAsync` is called but also when the refresh is initiated by the client automatically. Thus, this event allows you to observe potential network issues that occur under the hood.

* `event EventHandler<ConfigChangedEventArgs> ConfigChanged`: This event is raised first when the client's internal cache gets populated. Afterwards, it is raised again each time the internally cached config is updated to a newer version, either as a result of synchronization with the external cache, or as a result of fetching a newer version from the ConfigCat CDN.

* `event EventHandler<FlagEvaluatedEventArgs> FlagEvaluated`: This event is raised each time the client evaluates a feature flag or setting. The event provides the same evaluation details that you would get from [`GetValueDetailsAsync()`](#anatomy-of-getvaluedetailsasync).

* `event EventHandler<ConfigCatClientErrorEventArgs> Error`: This event is raised when an error occurs within the client.

You can subscribe to these events either on initialization:

<!-- -->

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.PollingMode = PollingModes.ManualPoll;
    options.FlagEvaluated += (s, e) => { /* handle the event */ };
});

```

...or directly on the `ConfigCatClient` instance:

```csharp
client.FlagEvaluated += (s, e) => { /* handle the event */ };

```

<!-- -->

## Snapshots and non-blocking synchronous feature flag evaluation[​](#snapshots-and-non-blocking-synchronous-feature-flag-evaluation "Direct link to Snapshots and non-blocking synchronous feature flag evaluation")

The *ConfigCat* client doesn't directly provide synchronous methods for evaluating feature flags and settings because such synchronous methods could block the executing thread for longer periods of time (e.g. when downloading config data from the ConfigCat CDN servers), which could lead to an unresponsive application.

However, there can be circumstances where synchronous evaluation is preferable, thus, since v9.2.0, the .NET SDK provides a way to synchronously evaluate feature flags and settings as a non-blocking operation, via *snapshots*.

<!-- -->

The typical setup for non-blocking synchronous feature flag evaluation looks like this:

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
      "Polling": {
        "Mode": "AutoPoll"
      }
    },
    "Init": {
      "Mode": "WaitForClientReady",
      "ThrowOnFailure": false
    }
  }
}

```

Alternatively, by code:

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.PollingMode = PollingModes.AutoPoll();
});
configCatBuilder.UseInitMode(new ConfigCatInitMode.WaitForClientReady(throwOnFailure: false));

```

Then, in the startup phase of your application, ensure that the registered client(s) are initialized:

* If your application host supports [hosted services](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/hosted-services), this will happen automatically.

* In other cases, you need to perform initialization manually, in the startup phase of your application, after the DI container is built. E.g.:

  ```csharp
  var app = builder.Build();

  await app.Services.GetRequiredService<IConfigCatInitializer>().InitializeAsync();

  ```

caution

Reaching the ready state usually means the client is ready to evaluate feature flags and settings. However, please note that this is not guaranteed. In case of initialization failure or timeout, the internal cache may be empty or expired even after the ready state is reported. You can use the `ThrowOnFailure` option or `throwOnFailure` parameter to control how to proceed in such cases.

From this point on, you can call the `Snapshot()` method of the `IConfigCatClient` service to capture the current state of the *ConfigCat* client (including the latest downloaded config data) and use the resulting snapshot object to synchronously evaluate feature flags and settings based on the captured state:

```csharp
var snapshot = client.Snapshot();

var user = new User("#UNIQUE-USER-IDENTIFIER#");
foreach (var key in snapshot.GetAllKeys())
{
    var value = snapshot.GetValue(key, default(object), user);
    Console.WriteLine($"{key}: {value}");
}

```

tip

In ASP.NET Core applications, creating one snapshot per request is usually sufficient. To support this, the SDK offers a shortcut: you can obtain a snapshot for the current request by injecting the scoped `IConfigCatClientSnapshot` service directly.

Creating a snapshot is a cheap operation. This is possible because snapshots capture the client's internal (in-memory) cache. No attempt is made to refresh the internal cache, even if it's empty or expired.

caution

Please note that creating and using a snapshot

* won't trigger synchronization with the external cache when working with [shared caching](https://configcat.com/docs/advanced/caching.md#shared-cache),
* won't fetch the latest config data from the ConfigCat CDN when the internally cached config data is empty or expired.

For the above reasons, it's recommended to use snapshots in conjunction with the Auto Polling mode, where the SDK automatically updates the internal cache in the background. (For other polling modes, you'll need to manually initiate a cache refresh by calling `ForceRefreshAsync`.)

<!-- -->

## Online / Offline mode[​](#online--offline-mode "Direct link to Online / Offline mode")

In cases where you want to prevent the SDK from making HTTP calls, you can switch it to offline mode:

```csharp
client.SetOffline();

```

In offline mode, the SDK won't initiate HTTP requests and will work only from its cache.

To switch the SDK back to online mode, do the following:

```csharp
client.SetOnline();

```

Using the `client.IsOffline` property you can check whether the SDK is in offline mode.

## Flag Overrides[​](#flag-overrides "Direct link to Flag Overrides")

With flag overrides you can overwrite the feature flags & settings downloaded from the ConfigCat CDN with local values. Moreover, you can specify how the overrides should apply over the downloaded values. The following 3 behaviours are supported:

* **Local only** (`OverrideBehaviour.LocalOnly`): When evaluating values, the SDK will not use feature flags & settings from the ConfigCat CDN, but it will use all feature flags & settings that are loaded from local-override sources.

* **Local over remote** (`OverrideBehaviour.LocalOverRemote`): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the local-override version will take precedence.

* **Remote over local** (`OverrideBehaviour.RemoteOverLocal`): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the downloaded version will take precedence.

You can load your feature flag & setting overrides from a file or from a simple `Dictionary<string, object>` structure.

### JSON File[​](#json-file "Direct link to JSON File")

The SDK can load your feature flag & setting overrides from a file. You can also specify whether the file should be reloaded when it gets modified.

#### File[​](#file "Direct link to File")

<!-- -->

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "localhost";
    options.FlagOverrides = FlagOverrides.LocalFile(
        "path/to/local_flags.json", // path to the file
        true, // reload the file when it gets modified
        OverrideBehaviour.LocalOnly
    );
});

```

#### JSON File Structure[​](#json-file-structure "Direct link to JSON File Structure")

The SDK supports 2 types of JSON structures to describe feature flags & settings.

##### 1. Simple (key-value) structure[​](#1-simple-key-value-structure "Direct link to 1. Simple (key-value) structure")

```json
{
  "flags": {
    "enabledFeature": true,
    "disabledFeature": false,
    "intSetting": 5,
    "doubleSetting": 3.14,
    "stringSetting": "test"
  }
}

```

##### 2. Complex (full-featured) structure[​](#2-complex-full-featured-structure "Direct link to 2. Complex (full-featured) structure")

This is the same format that the SDK downloads from the ConfigCat CDN. It allows the usage of all features that are available on the ConfigCat Dashboard.

You can download your current config JSON from ConfigCat's CDN and use it as a baseline.

A convenient way to get the config JSON for a specific SDK Key is to install the [ConfigCat CLI](https://github.com/configcat/cli) tool and execute the following command:

```bash
configcat config-json get -f v6 -p {YOUR-SDK-KEY} > config.json

```

(Depending on your [Data Governance](https://configcat.com/docs/advanced/data-governance.md) settings, you may need to add the `--eu` switch.)

Alternatively, you can download the config JSON manually, based on your [Data Governance](https://configcat.com/docs/advanced/data-governance.md) settings:

* GLOBAL: `https://cdn-global.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v6.json`
* EU: `https://cdn-eu.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v6.json`

```json
{
  "p": {
    // hash salt, required only when confidential text comparator(s) are used
    "s": "80xCU/SlDz1lCiWFaxIBjyJeJecWjq46T4eu6GtozkM="
  },
  "s": [ // array of segments
    {
      "n": "Beta Users", // segment name
      "r": [ // array of User Conditions (there is a logical AND relation between the elements)
        {
          "a": "Email", // comparison attribute
          "c": 0, // comparator (see below)
          "l": [ // comparison value (see below)
            "john@example.com", "jane@example.com"
          ]
        }
      ]
    }
  ],
  "f": { // key-value map of feature flags & settings
    "isFeatureEnabled": { // key of a particular flag / setting
      "t": 0, // setting type, possible values:
              // 0 -> on/off setting (feature flag)
              // 1 -> text setting
              // 2 -> whole number setting
              // 3 -> decimal number setting
      "r": [ // array of Targeting Rules (there is a logical OR relation between the elements)
        {
          "c": [ // array of conditions (there is a logical AND relation between the elements)
            {
              "u": { // User Condition
                "a": "Email", // comparison attribute
                "c": 2, // comparator, possible values and required comparison value types:
                        // 0  -> IS ONE OF (cleartext) + string array comparison value ("l")
                        // 1  -> IS NOT ONE OF (cleartext) + string array comparison value ("l")
                        // 2  -> CONTAINS ANY OF (cleartext) + string array comparison value ("l")
                        // 3  -> NOT CONTAINS ANY OF (cleartext) + string array comparison value ("l")
                        // 4  -> IS ONE OF (semver) + semver string array comparison value ("l")
                        // 5  -> IS NOT ONE OF (semver) + semver string array comparison value ("l")
                        // 6  -> < (semver) + semver string comparison value ("s")
                        // 7  -> <= (semver + semver string comparison value ("s")
                        // 8  -> > (semver) + semver string comparison value ("s")
                        // 9  -> >= (semver + semver string comparison value ("s")
                        // 10 -> = (number) + number comparison value ("d")
                        // 11 -> <> (number + number comparison value ("d")
                        // 12 -> < (number) + number comparison value ("d")
                        // 13 -> <= (number + number comparison value ("d")
                        // 14 -> > (number) + number comparison value ("d")
                        // 15 -> >= (number) + number comparison value ("d")
                        // 16 -> IS ONE OF (hashed) + string array comparison value ("l")
                        // 17 -> IS NOT ONE OF (hashed) + string array comparison value ("l")
                        // 18 -> BEFORE (UTC datetime) + second-based Unix timestamp number comparison value ("d")
                        // 19 -> AFTER (UTC datetime) + second-based Unix timestamp number comparison value ("d")
                        // 20 -> EQUALS (hashed) + string comparison value ("s")
                        // 21 -> NOT EQUALS (hashed) + string comparison value ("s")
                        // 22 -> STARTS WITH ANY OF (hashed) + string array comparison value ("l")
                        // 23 -> NOT STARTS WITH ANY OF (hashed) + string array comparison value ("l")
                        // 24 -> ENDS WITH ANY OF (hashed) + string array comparison value ("l")
                        // 25 -> NOT ENDS WITH ANY OF (hashed) + string array comparison value ("l")
                        // 26 -> ARRAY CONTAINS ANY OF (hashed) + string array comparison value ("l")
                        // 27 -> ARRAY NOT CONTAINS ANY OF (hashed) + string array comparison value ("l")
                        // 28 -> EQUALS (cleartext) + string comparison value ("s")
                        // 29 -> NOT EQUALS (cleartext) + string comparison value ("s")
                        // 30 -> STARTS WITH ANY OF (cleartext) + string array comparison value ("l")
                        // 31 -> NOT STARTS WITH ANY OF (cleartext) + string array comparison value ("l")
                        // 32 -> ENDS WITH ANY OF (cleartext) + string array comparison value ("l")
                        // 33 -> NOT ENDS WITH ANY OF (cleartext + string array comparison value ("l")
                        // 34 -> ARRAY CONTAINS ANY OF (cleartext) + string array comparison value ("l")
                        // 35 -> ARRAY NOT CONTAINS ANY OF (cleartext) + string array comparison value ("l")
                "l": [ // comparison value - depending on the comparator, another type of value may need
                       // to be specified (see above):
                       // "s": string
                       // "d": number
                  "@example.com"
                ]
              }
            },
            {
              "p": { // Flag Condition (Prerequisite)
                "f": "mainIntFlag", // key of prerequisite flag
                "c": 0, // comparator, possible values: 0 -> EQUALS, 1 -> NOT EQUALS
                "v": { // comparison value (value's type must match the prerequisite flag's type)
                  "i": 42
                }
              }
            },
            {
              "s": { // Segment Condition
                "s": 0, // segment index, a valid index into the top-level segment array ("s")
                "c": 1 // comparator, possible values: 0 -> IS IN SEGMENT, 1 -> IS NOT IN SEGMENT
              }
            }
          ],
          "s": { // alternatively, an array of Percentage Options ("p", see below) can also be specified
            "v": { // the value served when the rule is selected during evaluation
              "b": true
            },
            "i": "bcfb84a7"
          }
        }
      ],
      "p": [ // array of Percentage Options
        {
          "p": 10, // % value
          "v": { // the value served when the Percentage Option is selected during evaluation
            "b": true
          },
          "i": "bcfb84a7"
        },
        {
          "p": 90,
          "v": {
            "b": false
          },
          "i": "bddac6ae"
        }
      ],
      "v": { // fallback value, served when none of the Targeting Rules match,
             // no Percentage Options are defined or evaluation of these is not possible
        "b": false // depending on the setting type, another type of value may need to be specified:
                   // text setting -> "s": string
                   // whole number setting -> "i": number
                   // decimal number setting -> "d": number
      },
      "i": "430bded3" // variation id (for analytical purposes)
    }
  }
}

```

For a more comprehensive specification of the config JSON v6 format, you may refer to [this JSON schema document](https://github.com/configcat/config-json/blob/main/V6/config.schema.json).

### Dictionary[​](#dictionary "Direct link to Dictionary")

You can set up the SDK to load your feature flag & setting overrides from a `Dictionary<string, object>`.

<!-- -->

```csharp
var dictionary = new Dictionary<string, object>
{
    {"enabledFeature", true},
    {"disabledFeature", false},
    {"intSetting", 5},
    {"doubleSetting", 3.14},
    {"stringSetting", "test"},
};

configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "localhost";
    options.FlagOverrides = FlagOverrides.LocalDictionary(dictionary, OverrideBehaviour.LocalOnly);
});

```

### Custom data source implementation[​](#custom-data-source-implementation "Direct link to Custom data source implementation")

You can create a custom flag override data source by implementing `IOverrideDataSource`.

The SDK provides the `Setting.FromValue()` method to create `Setting` objects from simple `bool`, `string`, `int` and `double` values. In case you need complex (full-featured) flag overrides, you can use the `Config.Deserialize()` method to obtain `Setting` objects from a config JSON conforming to the [config JSON v6 format](https://github.com/configcat/config-json/blob/main/V6/config.schema.json).

```csharp
class MyCustomOverrideDataSource : IOverrideDataSource
{
    private IReadOnlyDictionary<string, Setting> settings;

    public MyCustomOverrideDataSource(string configJson)
    {
        this.settings = Config.Deserialize(configJson).Settings;
    }

    public IReadOnlyDictionary<string, Setting> GetOverrides()
    {
        return this.settings;
    }
}

```

Then configure the client to use the `MyCustomOverrideDataSource` implementation:

<!-- -->

```csharp
var flagOverrideDataSource = new MyCustomOverrideDataSource("{ \"f\": { ... } }");

configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "localhost";
    options.FlagOverrides = new FlagOverrides(flagOverrideDataSource, OverrideBehaviour.LocalOnly);
});

```

## Logging[​](#logging "Direct link to Logging")

### Setting log level[​](#setting-log-level "Direct link to Setting log level")

<!-- -->

The SDK automatically configures the registered clients to integrate with `Microsoft.Extensions.Logging`, the [built-in logging framework](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging) of .NET Core/.NET 5+. Accordingly, you can set log levels using the standard configuration approach described [here](https://learn.microsoft.com/en-us/dotnet/core/extensions/logging/overview#configure-logging). E.g., via `appsettings.json`:

```json
{
  "Logging": {
    "LogLevel": {
      "ConfigCat.Client": "Information",
      "Default": "Information"
    }
  }
}

```

If you have multiple clients, you can define more specific log level filters using

* the `ConfigCat.Client.ConfigCatClient^` category name for the default client and
* the `ConfigCat.Client.ConfigCatClient[#CLIENT‑NAME#]` category name for named clients.

### Custom logger implementation[​](#custom-logger-implementation "Direct link to Custom logger implementation")

By default, the SDK <!-- -->forwards logs to the [the built-in logging framework](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging) (and even supports structured logging)<!-- --> but it also allows you to inject any custom logger implementation via the `ConfigCatClientOptions.Logger` property.

See [this sample code](https://github.com/configcat/.net-sdk/blob/master/samples/FileLoggerSample.cs) on how to create a basic file logger implementation for ConfigCat client.

### Log Filtering[​](#log-filtering "Direct link to Log Filtering")

You can define an <!-- -->additional<!-- --> log filter by providing a callback function via the `ConfigCatClientOptions.LogFilter` property. The callback will be called by the *ConfigCat SDK* each time a log event occurs <!-- -->. That is, the callback allows you to filter log events by `level`, `eventId`, `message` or `exception`. The formatted message string can be obtained via `message.InvariantFormattedMessage`. If the callback function returns `true`, the event will be logged, otherwise it will be skipped.

<!-- -->

```csharp
// Filter out events with id 1001 from the log.
LogFilterCallback logFilter = (LogLevel level, LogEventId eventId, ref FormattableLogMessage message, Exception? exception) => eventId != 1001;

configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.LogFilter = logFilter;
});

```

caution

Please make sure that your log filter logic doesn't perform heavy computation and doesn't block the executing thread. A complex or incorrectly implemented log filter can degrade the performance of the SDK.

## `GetAllKeysAsync()`[​](#getallkeysasync "Direct link to getallkeysasync")

You can get the keys for all available feature flags and settings by calling the `GetAllKeysAsync()` method.

```csharp
var keys = await client.GetAllKeysAsync();

```

## `GetAllValuesAsync()`[​](#getallvaluesasync "Direct link to getallvaluesasync")

Evaluates and returns the values of all feature flags and settings. Passing a [User Object](#user-object) is optional.

```csharp
var settingValues = await client.GetAllValuesAsync();

// invoke with User Object
var userObject = new User("#UNIQUE-USER-IDENTIFIER#");
var settingValuesTargeting = await client.GetAllValuesAsync(userObject);

```

## `GetAllValueDetailsAsync()`[​](#getallvaluedetailsasync "Direct link to getallvaluedetailsasync")

Evaluates and returns the values along with evaluation details of all feature flags and settings. Passing a [User Object](#user-object) is optional.

```csharp
var settingValues = await client.GetAllValueDetailsAsync();

// invoke with User Object
var userObject = new User("435170f4-8a8b-4b67-a723-505ac7cdea92");
var settingValuesTargeting = await client.GetAllValueDetailsAsync(userObject);

```

## Using custom cache implementation[​](#using-custom-cache-implementation "Direct link to Using custom cache implementation")

The *ConfigCat SDK* stores the downloaded config data in a local cache to minimize network traffic and enhance client performance. If you prefer to use your own cache solution, such as an external or distributed cache in your system, you can implement the [`IConfigCatCache`](https://github.com/configcat/.net-sdk/blob/master/src/ConfigCatClient/Cache/IConfigCatCache.cs) interface and set the `ConfigCache` parameter in the setup callback of `ConfigCatClient.Get`. This allows you to seamlessly integrate ConfigCat with your existing caching infrastructure.

```csharp
public class MyCustomCache : IConfigCatCache
{
    public ValueTask<string?> GetAsync(string key, CancellationToken cancellationToken = default)
    {
        /* insert your cache read logic here */
    }

    public ValueTask SetAsync(string key, string value, CancellationToken cancellationToken = default)
    {
        /* insert your cache write logic here */
    }
}

```

then

<!-- -->

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.ConfigCache = new MyCustomCache();
});

```

info

The .NET SDK supports *shared caching*. You can read more about this feature and the required minimum SDK versions [here](https://configcat.com/docs/advanced/caching.md#shared-cache).

## Using ConfigCat behind a proxy[​](#using-configcat-behind-a-proxy "Direct link to Using ConfigCat behind a proxy")

Provide your own network credentials (username/password) and proxy server settings (proxy server/port) by setting the `Proxy` property in the setup callback of `ConfigCatClient.Get`.

caution

This setting does not apply if you configure the client to use a custom config fetcher via the `ConfigFetcher` option.

<!-- -->

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
      "Proxy": "#PROXY-URL#"
    }
  }
}

```

Alternatively, by code:

```csharp
var myProxySettings = new WebProxy(proxyHost, proxyPort)
{
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(proxyUserName, proxyPassword)
};

configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.Proxy = myProxySettings;
});

```

## HTTP Timeout[​](#http-timeout "Direct link to HTTP Timeout")

You can set the maximum wait time for a ConfigCat HTTP response.

caution

This setting does not apply if you configure the client to use a custom config fetcher via the `ConfigFetcher` option.

<!-- -->

```json
{
  "ConfigCat": {
    "DefaultClient": {
      "SdkKey": "#YOUR-SDK-KEY#",
      "HttpTimeout": "00:00:10"
    }
  }
}

```

Alternatively, by code:

```csharp
configCatBuilder.AddDefaultClient(options =>
{
    options.SdkKey = "#YOUR-SDK-KEY#";
    options.HttpTimeout = TimeSpan.FromSeconds(10);
});

```

The default timeout is 30 seconds.

## Platform compatibility[​](#platform-compatibility "Direct link to Platform compatibility")

The *ConfigCat SDK* supports all the widespread .NET JIT runtimes, everything that implements [.NET Standard 2.0](https://learn.microsoft.com/en-us/dotnet/standard/net-standard?tabs=net-standard-2-0)+ and supports TLS 1.2 should work. Starting with v9.3.0, it can also be used in applications that employ [trimmed self-contained](https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trim-self-contained) or various [ahead-of-time (AOT) compilation](https://en.wikipedia.org/wiki/Ahead-of-time_compilation) deployment models.

Based on our tests, the SDK is compatible with the following runtimes/deployment models:

* .NET Framework 4.6.2+ (including Ngen)
* .NET Core 3.1, .NET 5+ (including Crossgen2/ReadyToRun and Native AOT)
* Mono 5.10+
* .NET for Android (formerly known as Xamarin.Android)
* .NET for iOS (formerly known as Xamarin.iOS)
* Unity 2021.3+ (Mono JIT)
* Unity 2021.3+ (IL2CPP)\*
* Universal Windows Platform 10.0.16299.0+ (.NET Native)\*\*
* WebAssembly (Mono AOT/Emscripten, also known as wasm-tools)

\*Unity WebGL also works but needs a bit of extra effort: you will need to enable WebGL compatibility by calling the `ConfigCatClient.PlatformCompatibilityOptions.EnableUnityWebGLCompatibility` method. For more details, see [Sample Scripts](https://github.com/configcat/.net-sdk/tree/master/samples/UnityWebGL).<br />\*\*To make the SDK work in Release builds on UWP, you will need to add `<Namespace Name="System.Text.Json.Serialization.Converters" Browse="Required All"/>` to your application's [.rd.xml](https://learn.microsoft.com/en-us/windows/uwp/dotnet-native/runtime-directives-rd-xml-configuration-file-reference) file. See also [this discussion](https://github.com/dotnet/runtime/issues/29912#issuecomment-638471351).

info

We strive to provide an extensive support for the various .NET runtimes and versions. If you still encounter an issue with the SDK on some platform, please open a [GitHub issue](https://github.com/configcat/.net-sdk/issues/new/choose) or [contact support](https://configcat.com/support).

## Sample Applications[​](#sample-applications "Direct link to Sample Applications")

Check out our Sample Applications how they use the *ConfigCat SDK*:

* [Sample Console App](https://github.com/configcat/.net-sdk/tree/master/samples/ConsoleApp)
* [Sample Multi-Page Web App (ASP.NET Core MVC)](https://github.com/configcat/.net-sdk/tree/master/samples/ASP.NETCore)
* [Sample Single-Page Web App (ASP.NET Core Blazor WebAssembly)](https://github.com/configcat/.net-sdk/tree/master/samples/BlazorWasm)
* [Sample Mobile/Windows Store App (.NET MAUI)](https://github.com/configcat/.net-sdk/tree/master/samples/MAUI)
* [Sample Windows Service (Worker Service built on .NET Generic Host)](https://github.com/configcat/.net-sdk/tree/master/samples/WindowsService)

## Guides[​](#guides "Direct link to Guides")

See the following guides on how to use ConfigCat's .NET SDK:

* [Feature Flags in a .NET 6 Application](https://configcat.com/blog/2022/11/25/feature-flags-in-net6/)
* [Using ConfigCat's Feature Flags in an ASP.NET Core Application](https://configcat.com/blog/2021/10/10/aspnetcore-options-pattern/)
* [Using ConfigCat Feature Flags in ASP.NET Core Web API](https://configcat.com/blog/2023/01/31/feature-flags-in-asp-net-core-web-api/)
* [How to Implement A/B Testing in .NET?](https://configcat.com/blog/2023/03/09/ab-testing-in-dotnet/)
* [Feature Flags in Microservices and Serverless Architecture (AWS Lambda and .NET)](https://configcat.com/blog/feature-flags-aws-lambda-dotnet/)

## Look under the hood[​](#look-under-the-hood "Direct link to Look under the hood")

<!-- -->

* [ConfigCat .NET SDK on GitHub](https://github.com/configcat/.net-sdk)
* [ConfigCat .NET SDK on nuget.org](https://www.nuget.org/packages/ConfigCat.Extensions.Hosting)
