Skip to content

Commit d45774b

Browse files
committed
Automatically register IOptionsChangeTokenSource<TOptions> implementations to enable cascade re-evaluation of options
1 parent 42bd6cc commit d45774b

6 files changed

Lines changed: 51 additions & 3 deletions

File tree

src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreConfiguration.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.ComponentModel;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Options;
10+
using Microsoft.Extensions.Primitives;
1011

1112
namespace OpenIddict.Client.AspNetCore;
1213

@@ -19,7 +20,8 @@ public sealed class OpenIddictClientAspNetCoreConfiguration : IConfigureOptions<
1920
IPostConfigureOptions<AuthenticationOptions>,
2021
IPostConfigureOptions<OpenIddictClientAspNetCoreOptions>,
2122
IValidateOptions<AuthenticationOptions>,
22-
IValidateOptions<OpenIddictClientAspNetCoreOptions>
23+
IValidateOptions<OpenIddictClientAspNetCoreOptions>,
24+
IOptionsChangeTokenSource<OpenIddictClientAspNetCoreOptions>
2325
{
2426
private readonly IServiceProvider _provider;
2527

@@ -203,4 +205,15 @@ public ValidateOptionsResult Validate(string? name, OpenIddictClientAspNetCoreOp
203205

204206
return builder.Build();
205207
}
208+
209+
/// <inheritdoc/>
210+
IChangeToken IOptionsChangeTokenSource<OpenIddictClientAspNetCoreOptions>.GetChangeToken() => new CompositeChangeToken(
211+
[
212+
// Force the options to be re-evaluated when the related instances from which they are populated are changed.
213+
.. from source in _provider.GetServices<IOptionsChangeTokenSource<OpenIddictClientOptions>>()
214+
select source.GetChangeToken()
215+
]);
216+
217+
/// <inheritdoc/>
218+
string? IOptionsChangeTokenSource<OpenIddictClientAspNetCoreOptions>.Name => Options.DefaultName;
206219
}

src/OpenIddict.Client.AspNetCore/OpenIddictClientAspNetCoreExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public static OpenIddictClientAspNetCoreBuilder UseAspNetCore(this OpenIddictCli
6363
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
6464
IValidateOptions<OpenIddictClientAspNetCoreOptions>, OpenIddictClientAspNetCoreConfiguration>());
6565

66+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
67+
IOptionsChangeTokenSource<OpenIddictClientAspNetCoreOptions>, OpenIddictClientAspNetCoreConfiguration>());
68+
6669
return new OpenIddictClientAspNetCoreBuilder(builder.Services);
6770
}
6871

src/OpenIddict.Client.Owin/OpenIddictClientOwinConfiguration.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.ComponentModel;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Options;
10+
using Microsoft.Extensions.Primitives;
1011
using Owin;
1112

1213
namespace OpenIddict.Client.Owin;
@@ -17,7 +18,8 @@ namespace OpenIddict.Client.Owin;
1718
[EditorBrowsable(EditorBrowsableState.Advanced)]
1819
public sealed class OpenIddictClientOwinConfiguration : IConfigureOptions<OpenIddictClientOptions>,
1920
IPostConfigureOptions<OpenIddictClientOwinOptions>,
20-
IValidateOptions<OpenIddictClientOwinOptions>
21+
IValidateOptions<OpenIddictClientOwinOptions>,
22+
IOptionsChangeTokenSource<OpenIddictClientOwinOptions>
2123
{
2224
private readonly IServiceProvider _provider;
2325

@@ -117,4 +119,15 @@ public ValidateOptionsResult Validate(string? name, OpenIddictClientOwinOptions
117119

118120
return builder.Build();
119121
}
122+
123+
/// <inheritdoc/>
124+
IChangeToken IOptionsChangeTokenSource<OpenIddictClientOwinOptions>.GetChangeToken() => new CompositeChangeToken(
125+
[
126+
// Force the options to be re-evaluated when the related instances from which they are populated are changed.
127+
.. from source in _provider.GetServices<IOptionsChangeTokenSource<OpenIddictClientOptions>>()
128+
select source.GetChangeToken()
129+
]);
130+
131+
/// <inheritdoc/>
132+
string? IOptionsChangeTokenSource<OpenIddictClientOwinOptions>.Name => Options.DefaultName;
120133
}

src/OpenIddict.Client.Owin/OpenIddictClientOwinExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public static OpenIddictClientOwinBuilder UseOwin(this OpenIddictClientBuilder b
5656
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
5757
IValidateOptions<OpenIddictClientOwinOptions>, OpenIddictClientOwinConfiguration>());
5858

59+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
60+
IOptionsChangeTokenSource<OpenIddictClientOwinOptions>, OpenIddictClientOwinConfiguration>());
61+
5962
return new OpenIddictClientOwinBuilder(builder.Services);
6063
}
6164

src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationConfiguration.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.ComponentModel;
88
using Microsoft.Extensions.DependencyInjection;
99
using Microsoft.Extensions.Options;
10+
using Microsoft.Extensions.Primitives;
1011
using OpenIddict.Server;
1112

1213
namespace OpenIddict.Validation.ServerIntegration;
@@ -16,7 +17,8 @@ namespace OpenIddict.Validation.ServerIntegration;
1617
/// </summary>
1718
[EditorBrowsable(EditorBrowsableState.Advanced)]
1819
public sealed class OpenIddictValidationServerIntegrationConfiguration : IConfigureOptions<OpenIddictValidationOptions>,
19-
IValidateOptions<OpenIddictValidationOptions>
20+
IValidateOptions<OpenIddictValidationOptions>,
21+
IOptionsChangeTokenSource<OpenIddictValidationOptions>
2022
{
2123
private readonly IServiceProvider _provider;
2224

@@ -87,4 +89,15 @@ public ValidateOptionsResult Validate(string? name, OpenIddictValidationOptions
8789

8890
return builder.Build();
8991
}
92+
93+
/// <inheritdoc/>
94+
IChangeToken IOptionsChangeTokenSource<OpenIddictValidationOptions>.GetChangeToken() => new CompositeChangeToken(
95+
[
96+
// Force the options to be re-evaluated when the related instances from which they are populated are changed.
97+
.. from source in _provider.GetServices<IOptionsChangeTokenSource<OpenIddictServerOptions>>()
98+
select source.GetChangeToken()
99+
]);
100+
101+
/// <inheritdoc/>
102+
string? IOptionsChangeTokenSource<OpenIddictValidationOptions>.Name => Options.DefaultName;
90103
}

src/OpenIddict.Validation.ServerIntegration/OpenIddictValidationServerIntegrationExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public static OpenIddictValidationServerIntegrationBuilder UseLocalServer(this O
3434
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
3535
IValidateOptions<OpenIddictValidationOptions>, OpenIddictValidationServerIntegrationConfiguration>());
3636

37+
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<
38+
IOptionsChangeTokenSource<OpenIddictValidationOptions>, OpenIddictValidationServerIntegrationConfiguration>());
39+
3740
return new OpenIddictValidationServerIntegrationBuilder(builder.Services);
3841
}
3942

0 commit comments

Comments
 (0)