Skip to content

Latest commit

 

History

History
109 lines (84 loc) · 2.81 KB

File metadata and controls

109 lines (84 loc) · 2.81 KB

FeatureManagement .NET Provider

![NOTE] This requires a new feature of the FeatureManagement system, Variants. This feature is still in preview and has not been fully released.

The FeatureManagement Provider allows you to use the FeatureManagement system as an OpenFeature Provider.

Requirements

  • open-feature/dotnet-sdk v1.5.0 > v2.0.0

.NET SDK Usage

Install dependencies

.NET Cli

dotnet add package OpenFeature.Contrib.Provider.FeatureManagement --version 0.0.4

Package Manager

NuGet\Install-Package OpenFeature.Contrib.Provider.FeatureManagement -Version 0.0.4

Package Reference

<PackageReference Include="OpenFeature.Contrib.Provider.FeatureManagement" Version="0.0.4" />

Paket CLI

paket add OpenFeature.Contrib.Provider.FeatureManagement --version 0.0.4

Cake

// Install OpenFeature.Contrib.Provider.FeatureManagement as a Cake Addin
#addin nuget:?package=OpenFeature.Contrib.Provider.FeatureManagement&version=0.0.4&prerelease

// Install OpenFeature.Contrib.Provider.FeatureManagement as a Cake Tool
#tool nuget:?package=OpenFeature.Contrib.Provider.FeatureManagement&version=0.0.4&prerelease

Using the FeatureManagement Provider with the OpenFeature SDK

FeatureManagement is built on top of .NETs Configuration system, so you must provide the loaded Configuration.
Since Configuration is passed in any valid Configuration source can be used.
For simplicity, we'll stick with a json file for all examples.

namespace OpenFeatureTestApp
{
    class Program
    {
        static void Main(string[] args)
        {
            var configurationBuilder = new ConfigurationBuilder();
            configurationBuilder.AddJsonFile("featureFlags.json");

            IConfiguration configuration = configurationBuilder.Build();

            var featureManagementProvider = new FeatureManagementProvider(configuration);
            OpenFeature.Api.Instance.SetProvider(featureManagementProvider);

            var client = OpenFeature.Api.Instance.GetClient();

            var val = await client.GetBooleanValueAsync("myBoolFlag", false, null);

            System.Console.WriteLine(val.ToString());
        }
    }
}

A simple example configuration would look like this.

{
  "FeatureManagement": {
    "myBoolFlag": {
      "Allocation": {
        "DefaultWhenEnabled": "FlagEnabled",
        "DefaultWhenDisabled": "FlagDisabled"
      },
      "Variants": [
        {
          "Name": "FlagEnabled",
          "ConfigurationValue": true
        },
        {
          "Name": "FlagDisabled",
          "ConfigurationValue": false
        }
      ],
      "EnabledFor": [
        {
          "Name": "AlwaysOn"
        }
      ]
    }
  }
}