Wrapper around AWS AppConfig for Simple Feature Flags.
NuGet: https://www.nuget.org/packages/AwsFeatureFlags/
To use simply add:
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
.AddEnvironmentVariables()
.AddCommandLine(args)
.Build();
services.AddSingleton(configuration);
services.AddDefaultAWSOptions(p => p.GetService<IConfiguration>().GetAWSOptions());
services.AddFeatureFlags();
var provider = services.BuildServiceProvider();By default AwsFeatureFlags pulls its configuration from IConfiguration which can be configured in many ways and come from any number of sources.
Without specifying a configuration source, AwsFeatureFlags will look for a FeatureFlags section in the appsettings.json file, or other configuration source.
AwsFeatureFlags can also be configured using one of the overrides of AddFeatureFlags(), as follows:
services.AddFeatureFlags(o => myPreconfiguredOptions);Or:
services.AddFeatureFlags(o => {
o.ApplicationIdentifier = "myApp";
o.EnvironmentIdentifier = "dev";
o.ConfigurationProfileIdentifier = "default";
});Note that you don't have to set
RequiredMinimumPollIntervalInSecondsas it will default to 120 seconds (2 minutes).
Or:
services.AddFeatureFlags(p => {
var myService = p.GetService<MyService>();
return myService.GetOptions();
});In all of the above cases the AddFeatureFlags() method takes care of everything that AwsFeatureFlags needs to work.
AwsFeatureFlags does use the AWSSDK.Extensions.NETCore.Setup package, and as such you will need the following somewhere in your dependency tree:
services.AddDefaultAWSOptions(p => p.GetService<IConfiguration>().GetAWSOptions());AwsFeatureFlags provides a simple interface for checking feature flags:
- Simply configure as above and the
IFeatureFlagServicewill be added to your DI tree - Inject into a class you are using:
namespace: mynamespace; public class MyClass { private readonly IFeatureFlagService _featureFlags; public MyClass(IFeatureFlagService featureFlags) => _featureFlags = featureFlags; public async Task Get() { var enabled = await _featureFlags.IsEnabledAsync("my_flag"); if(!enabled) return; // do some things here } }
AwsFeatureFlags provides a single service IFeatureFlagService which encapsulates all the functionality that this package provides.
This provides the following methods:
| Signature | Return Type | Description |
|---|---|---|
Get(string key) |
FeatureFlag |
Gets a single Feature Flag. |
GetAsync(string key, CancellationToken cancellationToken = default) |
Task<FeatureFlag> |
Gets a single feature flag asynchronously. |
IsEnabled(string key) |
bool |
Returns whether or not a given flag is enabled. If the flag is not found, it returns true. |
IsEnabledAsync(string key, CancellationToken cancellationToken = default) |
Task<bool> |
Returns whether or not a given flag is enabled asynchronously. If the flag is not found, it returns true. |
All() |
IEnumerable<FeatureFlag> |
Gets all known feature flags. |
AllAsync(CancellationToken cancellationToken = default) |
Task<IEnumerable<FeatureFlag>> |
Gets all know feature flags asynchronously. |
- The feature flags need to be defined, and setup as per the AWS documentation (here).
- The
keyused needs to exactly correspond to what has been created in AppConfig. - AwsFeatureFlags will not throw an exception if a flag is not found, the default behaviour is to return
true, ornullif not found.