Skip to content

Split-Community/dotnet-example

Repository files navigation

Split.io Integration with ASP.NET Core

This sample project demonstrates how to integrate Split.io feature flags into an ASP.NET Core web application using a production-ready approach with proper dependency injection and logging.

Features

  • Split.io SDK integration with ASP.NET Core
  • Custom logging implementation using Serilog
  • Dependency injection of the Split.io client
  • Example endpoint demonstrating feature flag usage
  • Structured logging configuration

Project Structure

  • Program.cs - Main application entry point with Split.io configuration
  • CustomLoggerImplementation.cs - Custom logger implementation bridging Split.io logs to Serilog
  • appsettings.json - Configuration file with Split.io API key and Serilog settings

Prerequisites

  • .NET 9.0 SDK or later
  • Split.io account and API key

Setup

  1. Clone the repository
  2. Replace the placeholder value in appsettings.json with your actual Split.io API key:
"SplitIO": {
  "ApiKey": "YOUR_SPLIT_IO_API_KEY"
}
  1. Run the application:
dotnet run

Usage

The application exposes an example endpoint that demonstrates how to use the Split.io client to get treatments for feature flags:

GET /split-example/{userId}/{featureFlag}

Example:

curl http://localhost:5238/split-example/testuser/test-feature

Response:

User testuser receives treatment 'control' for feature 'test-feature'

Key Implementation Points

1. Split.io Configuration

The Split.io client is configured in Program.cs using a configuration value from appsettings.json:

var config = new ConfigurationOptions();
config.Logger = new CustomLoggerImplementation();
var apiKey = builder.Configuration["SplitIO:ApiKey"] ?? throw new InvalidOperationException("SplitIO API key not found in configuration");
var factory = new SplitFactory(apiKey, config);
var splitClient = factory.Client();

2. Custom Logging

A custom implementation of the ISplitLogger interface bridges Split.io logs to Serilog:

public class CustomLoggerImplementation : ISplitLogger
{
    public bool IsDebugEnabled => true;

    public void Debug(string message)
    {
        Log.Debug(message);
    }
    
    // Additional methods...
}

3. Dependency Injection

The Split.io client is registered as a singleton service and available throughout the application:

builder.Services.AddSingleton<ISplitClient>(splitClient);

4. Using the Split.io Client in Endpoints

The Split.io client is injected into endpoints and used to get treatments:

app.MapGet("/split-example/{userId}/{featureFlag}", (string userId, string featureFlag, ISplitClient client) =>
{
    var treatment = client.GetTreatment(userId, featureFlag);
    return $"User {userId} receives treatment '{treatment}' for feature '{featureFlag}'";
});

Additional Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages