.NET provider
Installation
We will first install the OpenFeature SDK and the flagd provider.
.NET Cli
Package Manager
Package Reference
Packet cli
Cake
// Install OpenFeature.Contrib.Providers.Flagd as a Cake Addin
#addin nuget:?package=OpenFeature.Contrib.Providers.Flagd
// Install OpenFeature.Contrib.Providers.Flagd as a Cake Tool
#tool nuget:?package=OpenFeature.Contrib.Providers.Flagd
Using the flagd Provider with the OpenFeature SDK
This example assumes that the flagd server is running locally For example, you can start flagd with the following example configuration:
flagd start --uri https://raw.githubusercontent.com/open-feature/flagd/main/config/samples/example_flags.json
When the flagd service is running, you can use the SDK with the flagd Provider as in the following example console application:
using OpenFeature.Contrib.Providers.Flagd;
namespace OpenFeatureTestApp
{
class Hello {
static void Main(string[] args) {
var flagdProvider = new FlagdProvider(new Uri("http://localhost:8013"));
// Set the flagdProvider as the provider for the OpenFeature SDK
OpenFeature.Api.Instance.SetProvider(flagdProvider);
var client = OpenFeature.Api.Instance.GetClient("my-app");
var val = client.GetBooleanValueAsync("myBoolFlag", false, null);
// Print the value of the 'myBoolFlag' feature flag
System.Console.WriteLine(val.Result.ToString());
}
}
}
Configuring the FlagdProvider
The URI of the flagd server to which the flagd Provider
connects to can either be passed directly to the constructor, or be configured using the following environment variables:
Option name | Environment variable name | Type | Default | Values |
---|---|---|---|---|
host | FLAGD_HOST | string | localhost | |
port | FLAGD_PORT | number | 8013 | |
tls | FLAGD_TLS | boolean | false | |
tls certPath | FLAGD_SERVER_CERT_PATH | string | ||
unix socket path | FLAGD_SOCKET_PATH | string | ||
Caching | FLAGD_CACHE | string | lru | |
Maximum cache size | FLAGD_MAX_CACHE_SIZE | number | 10 | |
Maximum event stream retries | FLAGD_MAX_EVENT_STREAM_RETRIES | number | 3 | |
Resolver type | FLAGD_RESOLVER | string | rpc | rpc, in-process |
Source selector | FLAGD_SOURCE_SELECTOR | string |
Note that if FLAGD_SOCKET_PATH
is set, this value takes precedence, and the other variables (FLAGD_HOST
, FLAGD_PORT
, FLAGD_TLS
, FLAGD_SERVER_CERT_PATH
) are disregarded.
Note that if you are on NET462
through NET48
as the target framework for your project, you are required to enable TLS and supply a certificate path as part of your configuration. This is a limitation Microsoft has documented.
If you rely on the environment variables listed above, you can use the empty constructor which then configures the provider accordingly:
Alternatively, if you would like to pass the URI directly, you can initialise it as follows:
// either communicate with Flagd over HTTP ...
var flagdProvider = new FlagdProvider(new Uri("http://localhost:8013"));
// ... or use the unix:// prefix if the provider should communicate via a unix socket
var unixFlagdProvider = new FlagdProvider(new Uri("unix://socket.tmp"));
In-process resolver type
The flagd provider also supports the in-process provider mode,
which is activated by setting the FLAGD_RESOLVER
env var to IN_PROCESS
.
In this mode, the provider will connect to a service implementing the flagd.sync.v1 interface
and subscribe to a feature flag configuration determined by the FLAGD_SOURCE_SELECTOR
.
After an initial retrieval of the desired flag configuration, the in-process provider will keep the latest known state in memory,
meaning that no requests need to be sent over the network for resolving flags that are part of the flag configuration.
Updates to the flag configuration will be sent via the grpc event stream established between the in-process provider and
the service implementing the flagd.sync.v1
interface (e.g. flagd-proxy).
Example of using the in-process provider mode:
using OpenFeature.Contrib.Providers.Flagd;
namespace OpenFeatureTestApp
{
class Hello {
static void Main(string[] args) {
var flagdConfig = new FlagdConfigBuilder()
// set the host and port for flagd-proxy
.WithHost("localhost")
.WithPort("8015")
// set the resolver type to 'IN_PROCESS'
.WithResolverType(ResolverType.IN_PROCESS)
// provide the flag source selector, e.g. the name of a Flags custom resource which is watched by the flagd-proxy
.WithSourceSelector("core.openfeature.dev/flags/sample-flags")
.Build();
var flagdProvider = new FlagdProvider(flagdConfig);
// Set the flagdProvider as the provider for the OpenFeature SDK
OpenFeature.Api.Instance.SetProvider(flagdProvider);
var client = OpenFeature.Api.Instance.GetClient("my-app");
var val = client.GetBooleanValueAsync("myBoolFlag", false, null);
// Print the value of the 'myBoolFlag' feature flag
System.Console.WriteLine(val.Result.ToString());
}
}
}