From 1296749d4e0518eff1276399485ada0242ff50e6 Mon Sep 17 00:00:00 2001 From: zhiyuanliang Date: Wed, 15 Nov 2023 11:45:32 +0800 Subject: [PATCH] if filter name can be found, exception will not be thrown --- .../FeatureManager.cs | 8 ++++++ .../FeatureManagement.cs | 26 ++++++++++++------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.FeatureManagement/FeatureManager.cs b/src/Microsoft.FeatureManagement/FeatureManager.cs index 99f34fa4..c3b587fe 100644 --- a/src/Microsoft.FeatureManagement/FeatureManager.cs +++ b/src/Microsoft.FeatureManagement/FeatureManager.cs @@ -202,6 +202,14 @@ private async Task IsEnabledAsync(string feature, TContext appCo if (filter == null) { + if (_featureFilters.Any(f => IsMatchingName(f.GetType(), featureFilterConfiguration.Name))) + { + // + // Cannot find the appropriate registered feature filter which matches the filter name and the provided context type. + // But there is a registered feature filter which matches the filter name. + continue; + } + string errorMessage = $"The feature filter '{featureFilterConfiguration.Name}' specified for feature '{feature}' was not found."; if (!_options.IgnoreMissingFeatureFilters) diff --git a/tests/Tests.FeatureManagement/FeatureManagement.cs b/tests/Tests.FeatureManagement/FeatureManagement.cs index 73816617..4899ebac 100644 --- a/tests/Tests.FeatureManagement/FeatureManagement.cs +++ b/tests/Tests.FeatureManagement/FeatureManagement.cs @@ -175,25 +175,33 @@ public async Task AllowsDuplicatedFilterAlias() }); Assert.Equal($"Multiple contextual feature filters match the configured filter named '{duplicatedFilterName}' and context type '{typeof(DummyContext)}'.", ex.Message); + } - services = new ServiceCollection(); + [Fact] + public async Task SkipsContextualFilterEvaluationForUnrecognizedContext() + { + string featureName = Features.FeatureUsesFiltersWithDuplicatedAlias; + + IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build(); + + var services = new ServiceCollection(); services .AddSingleton(config) .AddFeatureManagement() .AddFeatureFilter(); - serviceProvider = services.BuildServiceProvider(); + ServiceProvider serviceProvider = services.BuildServiceProvider(); - featureManager = serviceProvider.GetRequiredService(); + IFeatureManager featureManager = serviceProvider.GetRequiredService(); - ex = await Assert.ThrowsAsync( - async () => - { - await featureManager.IsEnabledAsync(featureName); - }); + var appContext = new AppContext(); + + var dummyContext = new DummyContext(); - Assert.Equal($"The feature filter '{duplicatedFilterName}' specified for feature '{featureName}' was not found.", ex.Message); + Assert.True(await featureManager.IsEnabledAsync(featureName)); + + Assert.True(await featureManager.IsEnabledAsync(featureName, dummyContext)); } [Fact]