prepare AoT support
This commit is contained in:
parent
3478750bc9
commit
892101a84d
@ -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>
|
||||
|
12
OpenHarbor.JwtTokenManager/JwtTokenManagerJsonContext.cs
Normal file
12
OpenHarbor.JwtTokenManager/JwtTokenManagerJsonContext.cs
Normal 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
|
||||
{
|
||||
}
|
@ -1,5 +1,8 @@
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace OpenHarbor.JwtTokenManager;
|
||||
|
||||
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
|
||||
public class JwtTokenManagerOptions
|
||||
{
|
||||
public required string TokenEndpoint { get; set; }
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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>
|
||||
|
@ -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,
|
||||
@ -20,8 +22,8 @@ public static class ServiceCollectionExtensions
|
||||
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
|
||||
|
Loading…
Reference in New Issue
Block a user