microsoft / FeatureManagement-Dotnet

Microsoft.FeatureManagement provides standardized APIs for enabling feature flags within applications. Utilize this library to secure a consistent experience when developing applications that use patterns such as beta access, rollout, dark deployments, and more.
MIT License
1.02k stars 111 forks source link

Cannot use the TargetingFilter in .NET class library project #446

Closed bdscarboro1 closed 3 months ago

bdscarboro1 commented 3 months ago

FeatureManagementBuilder is internal, so cannot instantiate from a class library project, which in turn means cannot do the equivalent of this setup from a web api project: builder.Services.AddFeatureManagement() .WithTargeting<HttpContextTargetingContextAccessor>();

Are there code samples anywhere on how to use Microsoft.FeatureManagement from a class library?

zhiyuanliang-ms commented 3 months ago

Hi, @bdscarboro1

Start from Microsoft.FeatureManagement v3.1.0, FeatureManager and ConfigurationFeatureDefniitonProvider are exposed to public. Also, all built-in feature filters (targeting, timewindow, percentage) are public class.

We just released v3.3.0, where we made parameters of the constructors of the built-in feature filters mentioned above optional. This change is mainly for improving the experience of non-DI usage of feature management library.

So, I would suggest you update to the latest Microsoft.FeatureManagement v3.3.0.

There are two targeting filters in this lib, ContextualTargetingFilter and TargetingFilter.

They are respectively designed for console app(explicit TargetingContext) and web app(ambient TargetingContext through ITargetingContextAccessor).

For more details, please read Targeting.

Here is an example of how to use feature management without dependency injection.

using Microsoft.Extensions.Configuration;
using Microsoft.FeatureManagement;
using Microsoft.FeatureManagement.FeatureFilters;

IConfiguration configuration = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

IFeatureDefinitionProvider featureDefinitionProvider = new ConfigurationFeatureDefinitionProvider(configuration);

IFeatureManager featureManager = new FeatureManager(featureDefinitionProvider)
{
    FeatureFilters = new List<IFeatureFilterMetadata> { new ContextualTargetingFilter() }
};

var targetingContext = new TargetingContext()
{
    UserId = "Jeff"
};

bool res = await featureManager.IsEnabledAsync("MyFeature", targetingContext);

The feature flag in Microsoft.FeatureManagement schema:

{
  "feature_management": {
    "feature_flags": [
      {
        "id": "MyFeature",
        "enabled": true,
        "conditions": {
          "client_filters": [
            {
              "name": "Microsoft.Targeting",
              "parameters": {
                "Audience": {
                  "Users": [ "Jeff" ]
                }
              }
            }
          ]
        }
      }
    ]
  }
}

Or in .NET FeatureManagement schema:

{
  "FeatureManagement": {
    "MyFeature": {
      "EnabledFor": [
        {
          "Name": "Microsoft.Targeting",
          "Parameters": {
            "Audience": {
              "Users": ["Jeff"]
            }
          }
        }
      ]
    }
  }
}
zhiyuanliang-ms commented 3 months ago

@bdscarboro1 Does this example help you?

bdscarboro1 commented 3 months ago

Yes, it did help me, thank you!

On Wed, May 15, 2024, 2:02 AM Zhiyuan Liang @.***> wrote:

@bdscarboro1 https://github.com/bdscarboro1 Does this example help you?

— Reply to this email directly, view it on GitHub https://github.com/microsoft/FeatureManagement-Dotnet/issues/446#issuecomment-2111647480, or unsubscribe https://github.com/notifications/unsubscribe-auth/BH6IXGF4IJJU4ZLODXVVPM3ZCL253AVCNFSM6AAAAABHNP4UUCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJRGY2DONBYGA . You are receiving this because you were mentioned.Message ID: @.***>