Skip to content

Commit a8d12ef

Browse files
fix: Address issue with FeatureClient not being resolved when no Provider added (#607)
* Fix issue with FeatureClient not being resolved * Address issue when adding context to the feature builder without providing any provider or default feature client. Signed-off-by: Kyle Julian <[email protected]> * Add unit test for bug Signed-off-by: Kyle Julian <[email protected]> * Run dotnet format Signed-off-by: Kyle Julian <[email protected]> * Address Gemini comment Signed-off-by: Kyle Julian <[email protected]> --------- Signed-off-by: Kyle Julian <[email protected]>
1 parent 1b40391 commit a8d12ef

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/OpenFeature.Hosting/OpenFeatureServiceCollectionExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public static IServiceCollection AddOpenFeature(this IServiceCollection services
3030
var builder = new OpenFeatureBuilder(services);
3131
configure(builder);
3232

33+
builder.Services.Configure<OpenFeatureOptions>(c => { }); // Ensures IOptions<OpenFeatureOptions> is available even when no providers are configured.
3334
builder.Services.AddHostedService<HostedFeatureLifecycleService>();
3435

3536
// If a default provider is specified without additional providers,
@@ -50,7 +51,7 @@ public static IServiceCollection AddOpenFeature(this IServiceCollection services
5051
options.DefaultNameSelector = provider =>
5152
{
5253
var options = provider.GetRequiredService<IOptions<OpenFeatureOptions>>().Value;
53-
return options.ProviderNames.First();
54+
return options.ProviderNames.FirstOrDefault();
5455
};
5556
});
5657
}

test/OpenFeature.Hosting.Tests/OpenFeatureServiceCollectionExtensionsTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,16 @@ public void AddOpenFeature_WithNamedDefaultProvider_InvokesAddPolicyName()
9292
var otherClient = serviceProvider.GetService<IFeatureClient>();
9393
Assert.NotNull(otherClient);
9494
}
95+
96+
[Fact]
97+
public void AddOpenFeature_WithNoProvider_CanResolveFeatureClient()
98+
{
99+
// Act
100+
_systemUnderTest.AddOpenFeature(builder => { });
101+
102+
// Assert
103+
using var serviceProvider = _systemUnderTest.BuildServiceProvider();
104+
var client = serviceProvider.GetService<IFeatureClient>();
105+
Assert.NotNull(client);
106+
}
95107
}

0 commit comments

Comments
 (0)