From 892101a84dcaf793b511962c8c2a796a4eea16c5 Mon Sep 17 00:00:00 2001 From: Mathias Beaulieu-Duncan Date: Sun, 22 Dec 2024 13:03:54 -0500 Subject: [PATCH] prepare AoT support --- .../OpenHarbor.JwtTokenManager.Abstractions.csproj | 2 +- .../JwtTokenManagerJsonContext.cs | 12 ++++++++++++ OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs | 3 +++ OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs | 8 +------- .../OpenHarbor.JwtTokenManager.csproj | 4 +++- .../ServiceCollectionExtensions.cs | 10 ++++++---- 6 files changed, 26 insertions(+), 13 deletions(-) create mode 100644 OpenHarbor.JwtTokenManager/JwtTokenManagerJsonContext.cs diff --git a/OpenHarbor.JwtTokenManager.Abstractions/OpenHarbor.JwtTokenManager.Abstractions.csproj b/OpenHarbor.JwtTokenManager.Abstractions/OpenHarbor.JwtTokenManager.Abstractions.csproj index c829e60..5f95f48 100644 --- a/OpenHarbor.JwtTokenManager.Abstractions/OpenHarbor.JwtTokenManager.Abstractions.csproj +++ b/OpenHarbor.JwtTokenManager.Abstractions/OpenHarbor.JwtTokenManager.Abstractions.csproj @@ -2,6 +2,7 @@ net8.0 + true enable enable Mathias Beaulieu-Duncan @@ -14,7 +15,6 @@ MIT true true - true diff --git a/OpenHarbor.JwtTokenManager/JwtTokenManagerJsonContext.cs b/OpenHarbor.JwtTokenManager/JwtTokenManagerJsonContext.cs new file mode 100644 index 0000000..0c4ed96 --- /dev/null +++ b/OpenHarbor.JwtTokenManager/JwtTokenManagerJsonContext.cs @@ -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 +{ +} \ No newline at end of file diff --git a/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs b/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs index 3fa5c34..03ed954 100644 --- a/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs +++ b/OpenHarbor.JwtTokenManager/JwtTokenManagerOptions.cs @@ -1,5 +1,8 @@ +using System.Diagnostics.CodeAnalysis; + namespace OpenHarbor.JwtTokenManager; +[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] public class JwtTokenManagerOptions { public required string TokenEndpoint { get; set; } diff --git a/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs b/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs index 99d32f1..56ccc33 100644 --- a/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs +++ b/OpenHarbor.JwtTokenManager/JwtTokenManagerService.cs @@ -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 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(SnakeCaseOptions, cancellationToken); + var tokenResponse = await response.Content.ReadFromJsonAsync(JwtTokenManagerJsonContext.Default.JwtTokenResponse, cancellationToken); if (tokenResponse == null) { diff --git a/OpenHarbor.JwtTokenManager/OpenHarbor.JwtTokenManager.csproj b/OpenHarbor.JwtTokenManager/OpenHarbor.JwtTokenManager.csproj index 2d5006d..4382d5a 100644 --- a/OpenHarbor.JwtTokenManager/OpenHarbor.JwtTokenManager.csproj +++ b/OpenHarbor.JwtTokenManager/OpenHarbor.JwtTokenManager.csproj @@ -2,6 +2,7 @@ net8.0 + true enable enable Mathias Beaulieu-Duncan @@ -14,7 +15,8 @@ MIT true true - true + + true diff --git a/OpenHarbor.JwtTokenManager/ServiceCollectionExtensions.cs b/OpenHarbor.JwtTokenManager/ServiceCollectionExtensions.cs index 8242473..b78f42c 100644 --- a/OpenHarbor.JwtTokenManager/ServiceCollectionExtensions.cs +++ b/OpenHarbor.JwtTokenManager/ServiceCollectionExtensions.cs @@ -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(configuration.GetSection(sectionName)); + + var section = configuration.GetSection(sectionName); + services.Configure(section); // Apply the builder options var builderOptions = new JwtTokenManagerBuilderOptions(); @@ -30,7 +32,7 @@ public static class ServiceCollectionExtensions // Register the service services.AddSingleton(provider => { - var optionsMonitor = provider.GetRequiredService>(); + var optionsMonitor = provider.GetRequiredService>(); var options = optionsMonitor.Get(Options.DefaultName); // Apply additional configuration