| | | 1 | | using AsiBackbone.AspNetCore.Acknowledgments; |
| | | 2 | | using AsiBackbone.AspNetCore.Actors; |
| | | 3 | | using AsiBackbone.AspNetCore.Correlation; |
| | | 4 | | using AsiBackbone.AspNetCore.Endpoints; |
| | | 5 | | using AsiBackbone.AspNetCore.Outbox; |
| | | 6 | | using AsiBackbone.AspNetCore.Results; |
| | | 7 | | using AsiBackbone.Core.Outbox; |
| | | 8 | | using Microsoft.Extensions.DependencyInjection; |
| | | 9 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 10 | | |
| | | 11 | | namespace AsiBackbone.AspNetCore.DependencyInjection; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Provides dependency injection registration helpers for ASP.NET Core host integration. |
| | | 15 | | /// </summary> |
| | | 16 | | public static class AsiBackboneAspNetCoreServiceCollectionExtensions |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Adds ASP.NET Core integration services for AsiBackbone using default options. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="services">The service collection to add services to.</param> |
| | | 22 | | /// <returns>The same service collection so calls can be chained.</returns> |
| | | 23 | | public static IServiceCollection AddAsiBackboneAspNetCore(this IServiceCollection services) |
| | | 24 | | { |
| | 38 | 25 | | return services.AddAsiBackboneAspNetCore(_ => { }); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Adds ASP.NET Core integration services for AsiBackbone using configured options. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="services">The service collection to add services to.</param> |
| | | 32 | | /// <param name="configure">The options configuration callback.</param> |
| | | 33 | | /// <returns>The same service collection so calls can be chained.</returns> |
| | | 34 | | /// <exception cref="ArgumentNullException"> |
| | | 35 | | /// Thrown when <paramref name="services" /> or <paramref name="configure" /> is <see langword="null" />. |
| | | 36 | | /// </exception> |
| | | 37 | | /// <remarks> |
| | | 38 | | /// Option validation is deferred to the Microsoft.Extensions.Options pipeline. |
| | | 39 | | /// Invalid option values are reported as <see cref="Microsoft.Extensions.Options.OptionsValidationException" /> |
| | | 40 | | /// when <see cref="Microsoft.Extensions.Options.IOptions{TOptions}.Value" /> is resolved or during host startup |
| | | 41 | | /// when <c>ValidateOnStart</c> executes. |
| | | 42 | | /// </remarks> |
| | | 43 | | public static IServiceCollection AddAsiBackboneAspNetCore( |
| | | 44 | | this IServiceCollection services, |
| | | 45 | | Action<AspNetCoreGovernanceOptions> configure) |
| | | 46 | | { |
| | 46 | 47 | | ArgumentNullException.ThrowIfNull(services); |
| | 45 | 48 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 49 | | |
| | 44 | 50 | | _ = services.AddOptions<AspNetCoreGovernanceOptions>() |
| | 44 | 51 | | .Configure(configure) |
| | 44 | 52 | | .Validate(static options => |
| | 44 | 53 | | { |
| | 44 | 54 | | try |
| | 44 | 55 | | { |
| | 44 | 56 | | options.Validate(); |
| | 44 | 57 | | return true; |
| | 44 | 58 | | } |
| | 44 | 59 | | catch (InvalidOperationException) |
| | 44 | 60 | | { |
| | 44 | 61 | | return false; |
| | 44 | 62 | | } |
| | 44 | 63 | | }, "ASP.NET Core integration options must be valid.") |
| | 44 | 64 | | .ValidateOnStart(); |
| | | 65 | | |
| | 44 | 66 | | _ = services.AddOptions<HttpGovernanceActorContextOptions>() |
| | 44 | 67 | | .Validate(static options => |
| | 44 | 68 | | { |
| | 44 | 69 | | try |
| | 44 | 70 | | { |
| | 44 | 71 | | options.Validate(); |
| | 44 | 72 | | return true; |
| | 44 | 73 | | } |
| | 44 | 74 | | catch (InvalidOperationException) |
| | 44 | 75 | | { |
| | 44 | 76 | | return false; |
| | 44 | 77 | | } |
| | 44 | 78 | | }, "HTTP actor context options must be valid.") |
| | 44 | 79 | | .ValidateOnStart(); |
| | | 80 | | |
| | 44 | 81 | | _ = services.AddOptions<GovernanceHttpResultMappingOptions>() |
| | 44 | 82 | | .Validate(static options => |
| | 44 | 83 | | { |
| | 44 | 84 | | try |
| | 44 | 85 | | { |
| | 44 | 86 | | options.Validate(); |
| | 44 | 87 | | return true; |
| | 44 | 88 | | } |
| | 44 | 89 | | catch (InvalidOperationException) |
| | 44 | 90 | | { |
| | 44 | 91 | | return false; |
| | 44 | 92 | | } |
| | 44 | 93 | | }, "HTTP result mapping options must be valid.") |
| | 44 | 94 | | .ValidateOnStart(); |
| | | 95 | | |
| | 44 | 96 | | _ = services.AddOptions<AcknowledgmentChallengeOptions>() |
| | 44 | 97 | | .Validate(static options => |
| | 44 | 98 | | { |
| | 44 | 99 | | try |
| | 44 | 100 | | { |
| | 44 | 101 | | options.Validate(); |
| | 44 | 102 | | return true; |
| | 44 | 103 | | } |
| | 44 | 104 | | catch (InvalidOperationException) |
| | 44 | 105 | | { |
| | 44 | 106 | | return false; |
| | 44 | 107 | | } |
| | 44 | 108 | | }, "Acknowledgment challenge options must be valid.") |
| | 44 | 109 | | .ValidateOnStart(); |
| | | 110 | | |
| | 44 | 111 | | _ = services.AddOptions<EndpointGovernanceOptions>() |
| | 44 | 112 | | .Validate(static options => |
| | 44 | 113 | | { |
| | 44 | 114 | | try |
| | 44 | 115 | | { |
| | 44 | 116 | | options.Validate(); |
| | 44 | 117 | | return true; |
| | 44 | 118 | | } |
| | 44 | 119 | | catch (InvalidOperationException) |
| | 44 | 120 | | { |
| | 44 | 121 | | return false; |
| | 44 | 122 | | } |
| | 44 | 123 | | }, "Endpoint governance options must be valid.") |
| | 44 | 124 | | .ValidateOnStart(); |
| | | 125 | | |
| | 44 | 126 | | _ = services.AddLogging(); |
| | 44 | 127 | | _ = services.AddHttpContextAccessor(); |
| | 44 | 128 | | _ = services.AddScoped<IHttpGovernanceActorContextResolver, HttpContextGovernanceActorContextResolver>(); |
| | 44 | 129 | | _ = services.AddScoped<IHttpGovernanceRequestCorrelationResolver, HttpContextGovernanceRequestCorrelationResolve |
| | 44 | 130 | | _ = services.AddScoped<IAcknowledgmentChallengeService, DefaultAcknowledgmentChallengeService>(); |
| | 44 | 131 | | _ = services.AddScoped<IEndpointGovernanceService, DefaultEndpointGovernanceService>(); |
| | | 132 | | |
| | 44 | 133 | | return services; |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Adds the host-owned outbox drain worker using default scheduling options. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <remarks> |
| | | 140 | | /// The worker is local to the process in which it is registered, so every replica of a horizontally scaled host run |
| | | 141 | | /// its own worker against the same durable outbox. Coordination before provider emission comes from |
| | | 142 | | /// <see cref="GovernanceOutboxOptions.UseClaimLeases" />, which is enabled by default and requires a store |
| | | 143 | | /// implementing <see cref="IGovernanceOutboxClaimStore" />; the drain throws when it is enabled against a |
| | | 144 | | /// store that cannot claim. Disabling it allows two replicas to select and emit the same envelope, so a host that o |
| | | 145 | | /// out should partition work, run the worker on a single role, or rely on provider-side idempotency. |
| | | 146 | | /// Claiming coordinates workers; it does not by itself create an exactly-once delivery guarantee. |
| | | 147 | | /// </remarks> |
| | | 148 | | /// <param name="services">The service collection to add services to.</param> |
| | | 149 | | /// <returns>The same service collection so calls can be chained.</returns> |
| | | 150 | | public static IServiceCollection AddAsiBackboneGovernanceOutboxDrainWorker(this IServiceCollection services) |
| | | 151 | | { |
| | 2 | 152 | | return services.AddAsiBackboneGovernanceOutboxDrainWorker(_ => { }); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Adds the host-owned outbox drain worker using configured scheduling options. |
| | | 157 | | /// </summary> |
| | | 158 | | /// <remarks> |
| | | 159 | | /// The worker is local to the process in which it is registered, so every replica of a horizontally scaled host run |
| | | 160 | | /// its own worker against the same durable outbox. Coordination before provider emission comes from |
| | | 161 | | /// <see cref="GovernanceOutboxOptions.UseClaimLeases" />, which is enabled by default and requires a store |
| | | 162 | | /// implementing <see cref="IGovernanceOutboxClaimStore" />; the drain throws when it is enabled against a |
| | | 163 | | /// store that cannot claim. Disabling it allows two replicas to select and emit the same envelope, so a host that o |
| | | 164 | | /// out should partition work, run the worker on a single role, or rely on provider-side idempotency. |
| | | 165 | | /// Claiming coordinates workers; it does not by itself create an exactly-once delivery guarantee. |
| | | 166 | | /// |
| | | 167 | | /// Validation of <paramref name="configure" /> values is deferred to the options pipeline. |
| | | 168 | | /// Invalid values are reported as <see cref="Microsoft.Extensions.Options.OptionsValidationException" /> |
| | | 169 | | /// when options are resolved, including hosted-service startup validation through |
| | | 170 | | /// <see cref="Microsoft.Extensions.Options.IOptionsMonitor{TOptions}.CurrentValue" />. |
| | | 171 | | /// </remarks> |
| | | 172 | | /// <param name="services">The service collection to add services to.</param> |
| | | 173 | | /// <param name="configure">The worker options configuration callback.</param> |
| | | 174 | | /// <returns>The same service collection so calls can be chained.</returns> |
| | | 175 | | /// <exception cref="ArgumentNullException"> |
| | | 176 | | /// Thrown when <paramref name="services" /> or <paramref name="configure" /> is <see langword="null" />. |
| | | 177 | | /// </exception> |
| | | 178 | | public static IServiceCollection AddAsiBackboneGovernanceOutboxDrainWorker( |
| | | 179 | | this IServiceCollection services, |
| | | 180 | | Action<GovernanceOutboxDrainWorkerOptions> configure) |
| | | 181 | | { |
| | 12 | 182 | | ArgumentNullException.ThrowIfNull(services); |
| | 12 | 183 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 184 | | |
| | 12 | 185 | | _ = services.AddOptions<GovernanceOutboxDrainWorkerOptions>() |
| | 12 | 186 | | .Configure(configure) |
| | 12 | 187 | | .Validate(static options => |
| | 12 | 188 | | { |
| | 12 | 189 | | try |
| | 12 | 190 | | { |
| | 12 | 191 | | options.Validate(); |
| | 12 | 192 | | return true; |
| | 12 | 193 | | } |
| | 12 | 194 | | catch (InvalidOperationException) |
| | 12 | 195 | | { |
| | 12 | 196 | | return false; |
| | 12 | 197 | | } |
| | 12 | 198 | | }, "Governance outbox drain worker options must be valid."); |
| | | 199 | | |
| | 12 | 200 | | _ = services.AddOptions<GovernanceOutboxOptions>() |
| | 12 | 201 | | .Validate(static options => |
| | 12 | 202 | | { |
| | 12 | 203 | | try |
| | 12 | 204 | | { |
| | 12 | 205 | | options.Validate(); |
| | 12 | 206 | | return true; |
| | 12 | 207 | | } |
| | 12 | 208 | | catch (InvalidOperationException) |
| | 12 | 209 | | { |
| | 12 | 210 | | return false; |
| | 12 | 211 | | } |
| | 12 | 212 | | }, "Governance outbox options must be valid.") |
| | 12 | 213 | | .ValidateOnStart(); |
| | | 214 | | |
| | 12 | 215 | | services.TryAddScoped<GovernanceOutboxDrain>(); |
| | 12 | 216 | | _ = services.AddHostedService<GovernanceOutboxDrainHostedService>(); |
| | | 217 | | |
| | 12 | 218 | | return services; |
| | | 219 | | } |
| | | 220 | | } |