prepare AoT support

This commit is contained in:
Mathias Beaulieu-Duncan 2024-12-22 13:03:54 -05:00
parent 3478750bc9
commit 892101a84d
Signed by: mathias
GPG Key ID: 8C3667DADE3B6303
6 changed files with 26 additions and 13 deletions

View File

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Authors>Mathias Beaulieu-Duncan</Authors> <Authors>Mathias Beaulieu-Duncan</Authors>
@ -14,7 +15,6 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols> <IncludeSymbols>true</IncludeSymbols>
<IncludeSource>true</IncludeSource> <IncludeSource>true</IncludeSource>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -0,0 +1,12 @@
using System.Text.Json.Serialization;
namespace OpenHarbor.JwtTokenManager;
[JsonSourceGenerationOptions(
PropertyNamingPolicy = JsonKnownNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true
)]
[JsonSerializable(typeof(JwtTokenResponse))]
public partial class JwtTokenManagerJsonContext : JsonSerializerContext
{
}

View File

@ -1,5 +1,8 @@
using System.Diagnostics.CodeAnalysis;
namespace OpenHarbor.JwtTokenManager; namespace OpenHarbor.JwtTokenManager;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public class JwtTokenManagerOptions public class JwtTokenManagerOptions
{ {
public required string TokenEndpoint { get; set; } public required string TokenEndpoint { get; set; }

View File

@ -14,12 +14,6 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
private readonly TimeSpan _cacheExpirationOffset = TimeSpan.FromSeconds(cacheOptions.ExpirationOffset); private readonly TimeSpan _cacheExpirationOffset = TimeSpan.FromSeconds(cacheOptions.ExpirationOffset);
private readonly string _scopes = string.Join(" ", options.Scopes); private readonly string _scopes = string.Join(" ", options.Scopes);
private static readonly JsonSerializerOptions SnakeCaseOptions = new()
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true
};
public async Task<JwtTokenManagerResult> GetTokenAsync(CancellationToken cancellationToken = default) public async Task<JwtTokenManagerResult> GetTokenAsync(CancellationToken cancellationToken = default)
{ {
if (memoryCache != null) if (memoryCache != null)
@ -49,7 +43,7 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
response.EnsureSuccessStatusCode(); response.EnsureSuccessStatusCode();
var tokenResponse = await response.Content.ReadFromJsonAsync<JwtTokenResponse>(SnakeCaseOptions, cancellationToken); var tokenResponse = await response.Content.ReadFromJsonAsync(JwtTokenManagerJsonContext.Default.JwtTokenResponse, cancellationToken);
if (tokenResponse == null) if (tokenResponse == null)
{ {

View File

@ -2,6 +2,7 @@
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net8.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<Authors>Mathias Beaulieu-Duncan</Authors> <Authors>Mathias Beaulieu-Duncan</Authors>
@ -14,7 +15,8 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression> <PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols> <IncludeSymbols>true</IncludeSymbols>
<IncludeSource>true</IncludeSource> <IncludeSource>true</IncludeSource>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<EnableJsonSourceGeneration>true</EnableJsonSourceGeneration>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@ -1,3 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
@ -9,6 +10,7 @@ namespace OpenHarbor.JwtTokenManager;
public static class ServiceCollectionExtensions public static class ServiceCollectionExtensions
{ {
[RequiresDynamicCode("Not AoT safe signature. Will add one in the future.")]
public static IServiceCollection AddJwtTokenManager( public static IServiceCollection AddJwtTokenManager(
this IServiceCollection services, this IServiceCollection services,
IConfiguration configuration, IConfiguration configuration,
@ -19,9 +21,9 @@ public static class ServiceCollectionExtensions
throw new ArgumentNullException(nameof(configuration)); throw new ArgumentNullException(nameof(configuration));
if (string.IsNullOrWhiteSpace(sectionName)) if (string.IsNullOrWhiteSpace(sectionName))
throw new ArgumentException("Section name must be provided.", nameof(sectionName)); throw new ArgumentException("Section name must be provided.", nameof(sectionName));
// Configure JwtTokenManagerOptions from the section var section = configuration.GetSection(sectionName);
services.Configure<JwtTokenManagerOptions>(configuration.GetSection(sectionName)); services.Configure<JwtTokenManagerBuilderOptions>(section);
// Apply the builder options // Apply the builder options
var builderOptions = new JwtTokenManagerBuilderOptions(); var builderOptions = new JwtTokenManagerBuilderOptions();
@ -30,7 +32,7 @@ public static class ServiceCollectionExtensions
// Register the service // Register the service
services.AddSingleton<IJwtTokenManagerService>(provider => services.AddSingleton<IJwtTokenManagerService>(provider =>
{ {
var optionsMonitor = provider.GetRequiredService<Microsoft.Extensions.Options.IOptionsMonitor<JwtTokenManagerOptions>>(); var optionsMonitor = provider.GetRequiredService<IOptionsMonitor<JwtTokenManagerOptions>>();
var options = optionsMonitor.Get(Options.DefaultName); var options = optionsMonitor.Get(Options.DefaultName);
// Apply additional configuration // Apply additional configuration