Skip to content

Commit e3e4b1b

Browse files
if filter name can be found, exception will not be thrown (#314)
1 parent 17a9af5 commit e3e4b1b

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/Microsoft.FeatureManagement/FeatureManager.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,14 @@ private async Task<bool> IsEnabledAsync<TContext>(string feature, TContext appCo
202202

203203
if (filter == null)
204204
{
205+
if (_featureFilters.Any(f => IsMatchingName(f.GetType(), featureFilterConfiguration.Name)))
206+
{
207+
//
208+
// Cannot find the appropriate registered feature filter which matches the filter name and the provided context type.
209+
// But there is a registered feature filter which matches the filter name.
210+
continue;
211+
}
212+
205213
string errorMessage = $"The feature filter '{featureFilterConfiguration.Name}' specified for feature '{feature}' was not found.";
206214

207215
if (!_options.IgnoreMissingFeatureFilters)

tests/Tests.FeatureManagement/FeatureManagement.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,33 @@ public async Task AllowsDuplicatedFilterAlias()
237237
});
238238

239239
Assert.Equal($"Multiple contextual feature filters match the configured filter named '{duplicatedFilterName}' and context type '{typeof(DummyContext)}'.", ex.Message);
240+
}
240241

241-
services = new ServiceCollection();
242+
[Fact]
243+
public async Task SkipsContextualFilterEvaluationForUnrecognizedContext()
244+
{
245+
string featureName = Features.FeatureUsesFiltersWithDuplicatedAlias;
246+
247+
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
248+
249+
var services = new ServiceCollection();
242250

243251
services
244252
.AddSingleton(config)
245253
.AddFeatureManagement()
246254
.AddFeatureFilter<ContextualDuplicatedAliasFeatureFilterWithAccountContext>();
247255

248-
serviceProvider = services.BuildServiceProvider();
256+
ServiceProvider serviceProvider = services.BuildServiceProvider();
249257

250-
featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
258+
IFeatureManager featureManager = serviceProvider.GetRequiredService<IFeatureManager>();
251259

252-
ex = await Assert.ThrowsAsync<FeatureManagementException>(
253-
async () =>
254-
{
255-
await featureManager.IsEnabledAsync(featureName);
256-
});
260+
var appContext = new AppContext();
261+
262+
var dummyContext = new DummyContext();
257263

258-
Assert.Equal($"The feature filter '{duplicatedFilterName}' specified for feature '{featureName}' was not found.", ex.Message);
264+
Assert.True(await featureManager.IsEnabledAsync(featureName));
265+
266+
Assert.True(await featureManager.IsEnabledAsync(featureName, dummyContext));
259267
}
260268

261269
[Fact]

0 commit comments

Comments
 (0)