Skip to main content

Introducing Client Initialization from Configuration for .NET SDKs

· 3 min read

We have added support for initializing the .NET SDK from configuration to simplify setup and enhance developer experience.

Details

The .NET SDK now supports client initialization directly from IConfigurationSection, leveraging .NET's built-in configuration system. This allows developers to define SDK settings through multiple configuration sources such as configuration files, environment variables, user secrets, and command-line arguments.

The SDK can now seamlessly integrate with your existing .NET configuration setup (for example, appsettings.json or any other provider), enabling environment-specific deployments, secure credential management, and runtime configuration updates without requiring code changes. If you don't have configuration set up yet, you can use ConfigurationBuilder as shown in the code samples below to easily configure and utilize the configuration system.

What's New

A new method FromConfiguration has been added to both Client and Client.Builder classes. This method accepts an IConfigurationSection from Microsoft.Extensions.Configuration as input to initialize the client configuration. Developers simply need to provide the IConfigurationSection containing the client configuration, and FromConfiguration will initialize and return either a configured Client or Client.Builder instance, depending on the calling context.

Code Sample for Client FromConfiguration

using Sdk.Standard;
using Microsoft.Extensions.Configuration;

namespace ConsoleApp;

// Build the IConfiguration using .NET conventions (JSON, environment, etc.)
var configuration = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddEnvironmentVariables() // [optional] read environment variables
.Build();

// Instantiate your SDK and configure it from IConfiguration
var client = SdkClient
.FromConfiguration(configuration.GetSection("SdkConfig"));

Code Sample for Client.Builder FromConfiguration

using Sdk.Standard;
using Microsoft.Extensions.Configuration;
using Environment = Tester.Standard.Environment;

namespace ConsoleApp;

// Build the IConfiguration using .NET conventions (JSON, environment, etc.)
var configuration = new ConfigurationBuilder()
.AddJsonFile("config.json")
.AddEnvironmentVariables() // [optional] read environment variables
.Build();

// Instantiate your SDK builder and configure it from IConfiguration with overrides
var client = Sdk.Builder
.FromConfiguration(configuration.GetSection("SdkConfig"))
.Environment(Environment.Testing)
.HttpClientConfig(c => c.Timeout(TimeSpan.FromSeconds(60)))
.Build();

Note: You can also override configurations loaded from the IConfigurationSection if needed by using the Builder methods on Client.Builder.

Sample Configuration File

Here's an example of how your config.json file should be structured to work with the above code samples:

{
"SdkConfig": {
"Environment": "testing",
"Port": "port",
"HttpClientConfig": {
"Timeout": "00:01:00",
"NumberOfRetries": 3,
"BackoffFactor": 2,
"RetryInterval": 1,
"MaximumRetryWaitTime": "00:02:00",
"StatusCodesToRetry": [408, 413],
"RequestMethodsToRetry": ["GET", "PUT", "DELETE"],
"ProxyConfiguration": {
"Address": "http://localhost:3000",
"Port": 8080,
"Tunnel": false,
"User": "username",
"Pass": "password"
}
}
}
}

Environment Variable Configuration

You can also configure individual settings using environment variables. .NET's configuration system uses double underscores (__) to represent nested configuration sections. Here are some examples:

# Set the environment
SdkConfig__Environment=production

# Set number of retries
SdkConfig__HttpClientConfig__NumberOfRetries=5

# Configure proxy settings
SdkConfig__HttpClientConfig__ProxyConfiguration__Port=8080

When using environment variables, their precedence depends on the order you add configuration providers to the ConfigurationBuilder.

Portal Enhancements

A new Configuration-Based Initialization section has been added to SDK Infrastructure. This section contains information on how to initialize the SDK client from configuration and includes configuration file examples for the SDK with all configurable fields.

Learn more about Client Initialization from Environment feature