| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Channels.Redis; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | |
| | | 5 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// DI registration for the Redis-backed AsyncResponse channel package. |
| | | 9 | | /// </summary> |
| | | 10 | | public static class RedisAsyncResponseServiceCollectionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Registers Redis pub/sub as the response channel — behind <see cref="IAsyncResponsePublisher"/>, |
| | | 14 | | /// <see cref="IAsyncResponseSubscriber"/>, and <see cref="IRecoverableAsyncResponseSubscriber"/> — |
| | | 15 | | /// together with the durable Redis recovery-state store, so a response that arrives after the |
| | | 16 | | /// waiter died (e.g. a redeploy) can still resume or fail the owning flow. It also registers |
| | | 17 | | /// <see cref="IRecoverableAsyncResponseBuilder"/> for fluent durable recovery flows. The same |
| | | 18 | | /// channel instance serves the engine's recovery-state scanner and active-subscriber probe, so the |
| | | 19 | | /// watchdog works against Redis automatically. |
| | | 20 | | /// <para> |
| | | 21 | | /// Requires a <c>StackExchange.Redis.IConnectionMultiplexer</c> singleton registered by the |
| | | 22 | | /// host — reuse the application's existing multiplexer; don't create a second connection. |
| | | 23 | | /// Publisher/subscriber resolve to one shared instance: per-interface instances would split the |
| | | 24 | | /// channel's internal per-channel state. |
| | | 25 | | /// </para> |
| | | 26 | | /// </summary> |
| | | 27 | | public static AsyncResponseRegistrationBuilder WithRedisChannel( |
| | | 28 | | this AsyncResponseRegistrationBuilder builder, |
| | | 29 | | Action<RedisAsyncResponseOptions>? configure = null) |
| | | 30 | | { |
| | 3 | 31 | | var services = builder.Services; |
| | 3 | 32 | | services.AddOptions(); |
| | 3 | 33 | | if (configure is not null) |
| | | 34 | | { |
| | 3 | 35 | | services.Configure(configure); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | // Durable recovery store; also the watchdog's recovery-state scanner. |
| | 3 | 39 | | services.TryAddSingleton<RedisRecoveryStateStore>(); |
| | 3 | 40 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateStore>(provider => provider.GetRequiredService<RedisR |
| | 3 | 41 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateScanner>(provider => provider.GetRequiredService<Redi |
| | | 42 | | |
| | | 43 | | // Redis pub/sub response channel: publisher + subscriber + the watchdog's liveness probe, |
| | | 44 | | // all one shared instance. |
| | 3 | 45 | | services.TryAddSingleton<RedisAsyncResponseChannel>(); |
| | 3 | 46 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponsePublisher>(provider => provider.GetRequiredService<Re |
| | 3 | 47 | | services.Replace(ServiceDescriptor.Singleton<IRawAsyncResponsePublisher>(provider => provider.GetRequiredService |
| | 3 | 48 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseSubscriber>(provider => provider.GetRequiredService<R |
| | 3 | 49 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseSubscriber>(provider => provider.GetRequir |
| | 3 | 50 | | services.Replace(ServiceDescriptor.Singleton<IActiveSubscriberProbe>(provider => provider.GetRequiredService<Red |
| | | 51 | | |
| | | 52 | | // Durable recovery capability: expose the recoverable fluent builder only when the Redis |
| | | 53 | | // channel package is the selected response channel. Plain IAsyncResponseBuilder still works |
| | | 54 | | // and shares the same implementation, but its static type does not offer recovery callbacks. |
| | 3 | 55 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseBuilder>(provider => new RecoverableAsyncR |
| | 3 | 56 | | provider.GetRequiredService<IRecoverableAsyncResponseSubscriber>(), |
| | 3 | 57 | | provider.GetService<IWorkerTransport>(), |
| | 3 | 58 | | provider.GetService<IAsyncResponseReplyTargetProvider>(), |
| | 3 | 59 | | provider.GetRequiredService<AsyncResponseContextPropagation>()))); |
| | 3 | 60 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseBuilder>(provider => provider.GetRequiredService<IRec |
| | | 61 | | |
| | 3 | 62 | | services.AddSingleton(new AsyncResponseChannelMarker("Redis")); |
| | 3 | 63 | | return builder; |
| | | 64 | | } |
| | | 65 | | } |