forked from open-feature/dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add OpenFeature.Extensions.Hosting package
See: open-feature/dotnet-sdk-contrib#127 Signed-off-by: Austin Drenski <[email protected]>
- Loading branch information
1 parent
45e2c86
commit ad140a2
Showing
16 changed files
with
464 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ jobs: | |
7.0.x | ||
- name: Run Tests | ||
run: dotnet test test/OpenFeature.Tests/ --configuration Release --logger GitHubActions | ||
run: dotnet test --logger GitHubActions | ||
|
||
unit-tests-windows: | ||
runs-on: windows-latest | ||
|
@@ -43,47 +43,4 @@ jobs: | |
7.0.x | ||
- name: Run Tests | ||
run: dotnet test test\OpenFeature.Tests\ --configuration Release --logger GitHubActions | ||
|
||
packaging: | ||
needs: | ||
- unit-tests-linux | ||
- unit-tests-windows | ||
|
||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Setup .NET SDK | ||
uses: actions/setup-dotnet@v4 | ||
with: | ||
dotnet-version: | | ||
6.0.x | ||
7.0.x | ||
- name: Restore | ||
run: dotnet restore | ||
|
||
- name: Pack NuGet packages (CI versions) | ||
if: startsWith(github.ref, 'refs/heads/') | ||
run: dotnet pack --no-restore --version-suffix "ci.$(date -u +%Y%m%dT%H%M%S)+sha.${GITHUB_SHA:0:9}" | ||
|
||
- name: Pack NuGet packages (PR versions) | ||
if: startsWith(github.ref, 'refs/pull/') | ||
run: dotnet pack --no-restore --version-suffix "pr.$(date -u +%Y%m%dT%H%M%S)+sha.${GITHUB_SHA:0:9}" | ||
|
||
- name: Publish NuGet packages (base) | ||
if: github.event.pull_request.head.repo.fork == false | ||
run: dotnet nuget push "src/**/*.nupkg" --api-key "${{ secrets.GITHUB_TOKEN }}" --source https://nuget.pkg.github.com/open-feature/index.json | ||
|
||
- name: Publish NuGet packages (fork) | ||
if: github.event.pull_request.head.repo.fork == true | ||
uses: actions/[email protected] | ||
with: | ||
name: nupkgs | ||
path: src/**/*.nupkg | ||
run: dotnet test --logger GitHubActions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
src/OpenFeature.Extensions.Hosting/Internal/OpenFeatureHostedService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Hosting; | ||
|
||
namespace OpenFeature.Internal; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public sealed class OpenFeatureHostedService(Api api, IEnumerable<FeatureProvider> providers) : IHostedLifecycleService | ||
{ | ||
readonly Api _api = Check.NotNull(api); | ||
readonly IEnumerable<FeatureProvider> _providers = Check.NotNull(providers); | ||
|
||
async Task IHostedLifecycleService.StartingAsync(CancellationToken cancellationToken) | ||
{ | ||
foreach (var provider in this._providers) | ||
{ | ||
await this._api.SetProviderAsync(provider.GetMetadata().Name, provider).ConfigureAwait(false); | ||
|
||
if (this._api.GetProviderMetadata() is { Name: "No-op Provider" }) | ||
await this._api.SetProviderAsync(provider).ConfigureAwait(false); | ||
} | ||
} | ||
|
||
Task IHostedService.StartAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
Task IHostedLifecycleService.StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
Task IHostedLifecycleService.StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
Task IHostedService.StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; | ||
|
||
Task IHostedLifecycleService.StoppedAsync(CancellationToken cancellationToken) => this._api.Shutdown(); | ||
} |
25 changes: 25 additions & 0 deletions
25
src/OpenFeature.Extensions.Hosting/OpenFeature.Extensions.Hosting.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<Nullable>enable</Nullable> | ||
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net462</TargetFrameworks> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<RootNamespace>OpenFeature</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="../../README.md" Pack="true" PackagePath="/" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="$(MicrosoftExtensionsHostingAbstractionsVer)" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="../OpenFeature/OpenFeature.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace OpenFeature; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="Services"></param> | ||
public sealed record OpenFeatureBuilder(IServiceCollection Services); |
145 changes: 145 additions & 0 deletions
145
src/OpenFeature.Extensions.Hosting/OpenFeatureBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
using System; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.DependencyInjection.Extensions; | ||
using Microsoft.Extensions.Logging; | ||
using OpenFeature.Internal; | ||
using OpenFeature.Model; | ||
|
||
namespace OpenFeature; | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public static class OpenFeatureBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="configure"></param> | ||
/// <returns> | ||
/// | ||
/// </returns> | ||
public static OpenFeatureBuilder AddContext( | ||
this OpenFeatureBuilder builder, | ||
Action<EvaluationContextBuilder> configure) | ||
{ | ||
Check.NotNull(builder); | ||
Check.NotNull(configure); | ||
|
||
AddContext(builder, null, (b, _, _) => configure(b)); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="configure"></param> | ||
/// <returns> | ||
/// | ||
/// </returns> | ||
public static OpenFeatureBuilder AddContext( | ||
this OpenFeatureBuilder builder, | ||
Action<EvaluationContextBuilder, IServiceProvider> configure) | ||
{ | ||
Check.NotNull(builder); | ||
Check.NotNull(configure); | ||
|
||
AddContext(builder, null, (b, _, s) => configure(b, s)); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="providerName"></param> | ||
/// <param name="configure"></param> | ||
/// <returns> | ||
/// | ||
/// </returns> | ||
public static OpenFeatureBuilder AddContext( | ||
this OpenFeatureBuilder builder, | ||
string? providerName, | ||
Action<EvaluationContextBuilder, string?, IServiceProvider> configure) | ||
{ | ||
Check.NotNull(builder); | ||
Check.NotNull(configure); | ||
|
||
builder.Services.AddKeyedSingleton(providerName, (services, key) => | ||
{ | ||
var b = EvaluationContext.Builder(); | ||
configure(b, key as string, services); | ||
return b.Build(); | ||
}); | ||
|
||
return builder; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="builder"></param> | ||
/// <param name="providerName"></param> | ||
public static void TryAddOpenFeatureClient(this OpenFeatureBuilder builder, string? providerName = null) | ||
{ | ||
Check.NotNull(builder); | ||
|
||
builder.Services.AddHostedService<OpenFeatureHostedService>(); | ||
|
||
builder.Services.TryAddKeyedSingleton(providerName, static (services, providerName) => | ||
{ | ||
var api = providerName switch | ||
{ | ||
null => Api.Instance, | ||
not null => services.GetRequiredKeyedService<Api>(null) | ||
}; | ||
api.AddHooks(services.GetKeyedServices<Hook>(providerName)); | ||
api.SetContext(services.GetRequiredKeyedService<EvaluationContextBuilder>(providerName).Build()); | ||
return api; | ||
}); | ||
|
||
builder.Services.TryAddKeyedSingleton(providerName, static (services, providerName) => providerName switch | ||
{ | ||
null => services.GetRequiredService<ILogger<FeatureClient>>(), | ||
not null => services.GetRequiredService<ILoggerFactory>().CreateLogger($"OpenFeature.FeatureClient.{providerName}") | ||
}); | ||
|
||
builder.Services.TryAddKeyedTransient(providerName, static (services, providerName) => | ||
{ | ||
var builder = providerName switch | ||
{ | ||
null => EvaluationContext.Builder(), | ||
not null => services.GetRequiredKeyedService<EvaluationContextBuilder>(null) | ||
}; | ||
foreach (var c in services.GetKeyedServices<EvaluationContext>(providerName)) | ||
{ | ||
builder.Merge(c); | ||
} | ||
return builder; | ||
}); | ||
|
||
builder.Services.TryAddKeyedTransient<IFeatureClient>(providerName, static (services, providerName) => | ||
{ | ||
var api = services.GetRequiredService<Api>(); | ||
return api.GetClient( | ||
api.GetProviderMetadata(providerName as string).Name, | ||
null, | ||
services.GetRequiredKeyedService<ILogger>(providerName), | ||
services.GetRequiredKeyedService<EvaluationContextBuilder>(providerName).Build()); | ||
}); | ||
|
||
if (providerName is not null) | ||
builder.Services.Replace(ServiceDescriptor.Transient(services => services.GetRequiredKeyedService<IFeatureClient>(providerName))); | ||
} | ||
} |
Oops, something went wrong.