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>
<TargetFramework>net8.0</TargetFramework>
<IsAotCompatible>true</IsAotCompatible>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>Mathias Beaulieu-Duncan</Authors>
@ -14,7 +15,6 @@
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<IncludeSymbols>true</IncludeSymbols>
<IncludeSource>true</IncludeSource>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>
<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;
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
public class JwtTokenManagerOptions
{
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 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)
{
if (memoryCache != null)
@ -49,7 +43,7 @@ public class JwtTokenManagerService(JwtTokenManagerOptions options, IHttpClientF
response.EnsureSuccessStatusCode();
var tokenResponse = await response.Content.ReadFromJsonAsync<JwtTokenResponse>(SnakeCaseOptions, cancellationToken);
var tokenResponse = await response.Content.ReadFromJsonAsync(JwtTokenManagerJsonContext.Default.JwtTokenResponse, cancellationToken);
if (tokenResponse == null)
{

View File

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

View File

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