| | | 1 | | using AsiBackbone.AspNetCore.Actors; |
| | | 2 | | using AsiBackbone.AspNetCore.Correlation; |
| | | 3 | | using AsiBackbone.AspNetCore.Endpoints; |
| | | 4 | | using AsiBackbone.AspNetCore.Handshakes; |
| | | 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 | | { |
| | 69 | 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 | | public static IServiceCollection AddAsiBackboneAspNetCore( |
| | | 38 | | this IServiceCollection services, |
| | | 39 | | Action<AsiBackboneAspNetCoreOptions> configure) |
| | | 40 | | { |
| | 39 | 41 | | ArgumentNullException.ThrowIfNull(services); |
| | 37 | 42 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 43 | | |
| | 35 | 44 | | AsiBackboneAspNetCoreOptions options = new(); |
| | 35 | 45 | | configure(options); |
| | 35 | 46 | | options.Validate(); |
| | | 47 | | |
| | 29 | 48 | | _ = services.AddOptions<AsiBackboneAspNetCoreOptions>() |
| | 29 | 49 | | .Configure(configure) |
| | 29 | 50 | | .Validate(static options => |
| | 29 | 51 | | { |
| | 29 | 52 | | try |
| | 29 | 53 | | { |
| | 15 | 54 | | options.Validate(); |
| | 15 | 55 | | return true; |
| | 29 | 56 | | } |
| | 0 | 57 | | catch (InvalidOperationException) |
| | 29 | 58 | | { |
| | 0 | 59 | | return false; |
| | 29 | 60 | | } |
| | 15 | 61 | | }, "ASP.NET Core integration options must be valid.") |
| | 29 | 62 | | .ValidateOnStart(); |
| | | 63 | | |
| | 29 | 64 | | _ = services.AddOptions<AsiBackboneHttpActorContextOptions>() |
| | 29 | 65 | | .Validate(static options => |
| | 29 | 66 | | { |
| | 29 | 67 | | try |
| | 29 | 68 | | { |
| | 13 | 69 | | options.Validate(); |
| | 11 | 70 | | return true; |
| | 29 | 71 | | } |
| | 2 | 72 | | catch (InvalidOperationException) |
| | 29 | 73 | | { |
| | 2 | 74 | | return false; |
| | 29 | 75 | | } |
| | 13 | 76 | | }, "HTTP actor context options must be valid.") |
| | 29 | 77 | | .ValidateOnStart(); |
| | | 78 | | |
| | 29 | 79 | | _ = services.AddOptions<AsiBackboneHttpResultMappingOptions>() |
| | 29 | 80 | | .Validate(static options => |
| | 29 | 81 | | { |
| | 29 | 82 | | try |
| | 29 | 83 | | { |
| | 13 | 84 | | options.Validate(); |
| | 11 | 85 | | return true; |
| | 29 | 86 | | } |
| | 2 | 87 | | catch (InvalidOperationException) |
| | 29 | 88 | | { |
| | 2 | 89 | | return false; |
| | 29 | 90 | | } |
| | 13 | 91 | | }, "HTTP result mapping options must be valid.") |
| | 29 | 92 | | .ValidateOnStart(); |
| | | 93 | | |
| | 29 | 94 | | _ = services.AddOptions<AsiBackboneAcknowledgmentChallengeOptions>() |
| | 29 | 95 | | .Validate(static options => |
| | 29 | 96 | | { |
| | 29 | 97 | | try |
| | 29 | 98 | | { |
| | 13 | 99 | | options.Validate(); |
| | 11 | 100 | | return true; |
| | 29 | 101 | | } |
| | 2 | 102 | | catch (InvalidOperationException) |
| | 29 | 103 | | { |
| | 2 | 104 | | return false; |
| | 29 | 105 | | } |
| | 13 | 106 | | }, "Acknowledgment challenge options must be valid.") |
| | 29 | 107 | | .ValidateOnStart(); |
| | | 108 | | |
| | 29 | 109 | | _ = services.AddOptions<AsiBackboneEndpointGovernanceOptions>() |
| | 29 | 110 | | .Validate(static options => |
| | 29 | 111 | | { |
| | 29 | 112 | | try |
| | 29 | 113 | | { |
| | 9 | 114 | | options.Validate(); |
| | 9 | 115 | | return true; |
| | 29 | 116 | | } |
| | 0 | 117 | | catch (InvalidOperationException) |
| | 29 | 118 | | { |
| | 0 | 119 | | return false; |
| | 29 | 120 | | } |
| | 9 | 121 | | }, "Endpoint governance options must be valid.") |
| | 29 | 122 | | .ValidateOnStart(); |
| | | 123 | | |
| | 29 | 124 | | _ = services.AddLogging(); |
| | 29 | 125 | | _ = services.AddHttpContextAccessor(); |
| | 29 | 126 | | _ = services.AddScoped<IAsiBackboneHttpActorContextResolver, HttpContextAsiBackboneActorContextResolver>(); |
| | 29 | 127 | | _ = services.AddScoped<IAsiBackboneHttpRequestCorrelationResolver, HttpContextAsiBackboneRequestCorrelationResol |
| | 29 | 128 | | _ = services.AddScoped<IAsiBackboneAcknowledgmentChallengeService, DefaultAsiBackboneAcknowledgmentChallengeServ |
| | 29 | 129 | | _ = services.AddScoped<IAsiBackboneEndpointGovernanceService, DefaultAsiBackboneEndpointGovernanceService>(); |
| | | 130 | | |
| | 29 | 131 | | return services; |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | /// <summary> |
| | | 135 | | /// Adds the host-owned governance outbox drain worker using default scheduling options. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="services">The service collection to add services to.</param> |
| | | 138 | | /// <returns>The same service collection so calls can be chained.</returns> |
| | | 139 | | public static IServiceCollection AddAsiBackboneGovernanceOutboxDrainWorker(this IServiceCollection services) |
| | | 140 | | { |
| | 0 | 141 | | return services.AddAsiBackboneGovernanceOutboxDrainWorker(_ => { }); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | /// <summary> |
| | | 145 | | /// Adds the host-owned governance outbox drain worker using configured scheduling options. |
| | | 146 | | /// </summary> |
| | | 147 | | /// <param name="services">The service collection to add services to.</param> |
| | | 148 | | /// <param name="configure">The worker options configuration callback.</param> |
| | | 149 | | /// <returns>The same service collection so calls can be chained.</returns> |
| | | 150 | | /// <exception cref="ArgumentNullException"> |
| | | 151 | | /// Thrown when <paramref name="services" /> or <paramref name="configure" /> is <see langword="null" />. |
| | | 152 | | /// </exception> |
| | | 153 | | public static IServiceCollection AddAsiBackboneGovernanceOutboxDrainWorker( |
| | | 154 | | this IServiceCollection services, |
| | | 155 | | Action<AsiBackboneGovernanceOutboxDrainWorkerOptions> configure) |
| | | 156 | | { |
| | 14 | 157 | | ArgumentNullException.ThrowIfNull(services); |
| | 14 | 158 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 159 | | |
| | 14 | 160 | | AsiBackboneGovernanceOutboxDrainWorkerOptions options = new(); |
| | 14 | 161 | | configure(options); |
| | 14 | 162 | | options.Validate(); |
| | | 163 | | |
| | 10 | 164 | | _ = services.AddOptions<AsiBackboneGovernanceOutboxDrainWorkerOptions>() |
| | 10 | 165 | | .Configure(configure) |
| | 10 | 166 | | .Validate(static options => |
| | 10 | 167 | | { |
| | 10 | 168 | | try |
| | 10 | 169 | | { |
| | 10 | 170 | | options.Validate(); |
| | 10 | 171 | | return true; |
| | 10 | 172 | | } |
| | 0 | 173 | | catch (InvalidOperationException) |
| | 10 | 174 | | { |
| | 0 | 175 | | return false; |
| | 10 | 176 | | } |
| | 10 | 177 | | }, "Governance outbox drain worker options must be valid.") |
| | 10 | 178 | | .ValidateOnStart(); |
| | | 179 | | |
| | 10 | 180 | | services.TryAddScoped<AsiBackboneGovernanceOutboxDrain>(); |
| | 10 | 181 | | _ = services.AddHostedService<AsiBackboneGovernanceOutboxDrainHostedService>(); |
| | | 182 | | |
| | 10 | 183 | | return services; |
| | | 184 | | } |
| | | 185 | | } |