Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
bdscarboro1 opened this issue May 8, 2024 · 3 comments
Closed

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

bdscarboro1 opened this issue May 8, 2024 · 3 comments

Comments

@bdscarboro1
Copy link

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
Copy link
Contributor

zhiyuanliang-ms commented May 9, 2024

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
Copy link
Contributor

@bdscarboro1 Does this example help you?

@bdscarboro1
Copy link
Author

bdscarboro1 commented May 15, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants