11using Microsoft . Extensions . DependencyInjection ;
2+ using Microsoft . Extensions . DependencyInjection . Extensions ;
23
34namespace Vulthil . Messaging . Inbox ;
45
@@ -14,9 +15,31 @@ public static class InboxRetentionServiceCollectionExtensions
1415 /// do).
1516 /// </summary>
1617 /// <param name="services">The service collection.</param>
17- /// <param name="configure">An optional action to configure <see cref="InboxOptions"/>.</param>
18+ /// <param name="configure">
19+ /// An optional action to configure <see cref="InboxOptions"/>. Invoked once eagerly (to evaluate
20+ /// <see cref="InboxRetentionOptions.Enabled"/>) and once more when the options system materializes
21+ /// <see cref="Microsoft.Extensions.Options.IOptions{TOptions}"/> for injected consumers.
22+ /// </param>
1823 /// <returns>The same service collection, for chaining.</returns>
19- public static IServiceCollection AddInboxRetention ( this IServiceCollection services , Action < InboxOptions > ? configure )
24+ public static IServiceCollection AddInboxRetention ( this IServiceCollection services , Action < InboxOptions > ? configure = null )
25+ {
26+ ArgumentNullException . ThrowIfNull ( services ) ;
27+
28+ services . TryAddSingleton ( TimeProvider . System ) ;
29+
30+ var options = new InboxOptions ( ) ;
31+ configure ? . Invoke ( options ) ;
32+
33+ return services . RegisterInboxRetention ( configure , options ) ;
34+ }
35+
36+ /// <summary>
37+ /// Registers the options validation and the conditional hosted-service registration for the inbox retention
38+ /// sweep, using an already-materialized <paramref name="options"/> instance so a shared caller (e.g.
39+ /// <c>AddInboxCore</c>) does not need to invoke <paramref name="configure"/> a second time just to re-evaluate
40+ /// the <see cref="InboxRetentionOptions.Enabled"/> gate.
41+ /// </summary>
42+ internal static IServiceCollection RegisterInboxRetention ( this IServiceCollection services , Action < InboxOptions > ? configure , InboxOptions options )
2043 {
2144 services . AddOptions < InboxOptions > ( )
2245 . Configure ( configure ?? ( static _ => { } ) )
@@ -26,11 +49,8 @@ public static IServiceCollection AddInboxRetention(this IServiceCollection servi
2649 && o . Retention . SweepInterval > TimeSpan . Zero
2750 && o . Retention . BatchSize >= 1 ,
2851 "Inbox retention requires RetentionPeriod and SweepInterval greater than zero and BatchSize of at least 1 when enabled." )
29- . ValidateDataAnnotations ( )
3052 . ValidateOnStart ( ) ;
3153
32- var options = new InboxOptions ( ) ;
33- configure ? . Invoke ( options ) ;
3454 if ( options . Retention . Enabled )
3555 {
3656 services . AddHostedService < InboxRetentionBackgroundService > ( ) ;
0 commit comments