| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Channels.MongoDB; |
| | | 3 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | using MongoDB.Driver; |
| | | 6 | | |
| | | 7 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 8 | | |
| | | 9 | | /// <summary>DI registration for the MongoDB-backed AsyncResponse channel package.</summary> |
| | | 10 | | public static class MongoDbAsyncResponseChannelServiceCollectionExtensions |
| | | 11 | | { |
| | | 12 | | /// <summary> |
| | | 13 | | /// Registers MongoDB change streams as the response channel — behind |
| | | 14 | | /// <see cref="IAsyncResponsePublisher"/>, <see cref="IAsyncResponseSubscriber"/>, and |
| | | 15 | | /// <see cref="IRecoverableAsyncResponseSubscriber"/> — together with the durable TTL-indexed |
| | | 16 | | /// MongoDB recovery-state store. Hosts may register an <see cref="IMongoDatabase"/> (or an |
| | | 17 | | /// <see cref="IMongoClient"/> plus <see cref="MongoDbAsyncResponseChannelOptions.DatabaseName"/>) |
| | | 18 | | /// singleton, or set <see cref="MongoDbAsyncResponseChannelOptions.ConnectionString"/> here. |
| | | 19 | | /// Change streams require the server to run as a replica set (single-node is sufficient). |
| | | 20 | | /// </summary> |
| | | 21 | | public static AsyncResponseRegistrationBuilder WithMongoDbChannel( |
| | | 22 | | this AsyncResponseRegistrationBuilder builder, |
| | | 23 | | Action<MongoDbAsyncResponseChannelOptions>? configure = null) |
| | | 24 | | { |
| | 3 | 25 | | var services = builder.Services; |
| | 3 | 26 | | services.AddOptions(); |
| | 3 | 27 | | if (configure is not null) |
| | 3 | 28 | | services.Configure(configure); |
| | | 29 | | |
| | | 30 | | // The store reuses a host-registered IMongoDatabase / IMongoClient when present; otherwise it |
| | | 31 | | // creates and owns a client from the options. Nothing is registered as a bare |
| | | 32 | | // IMongoClient/IMongoDatabase service, so unrelated resolutions of those types are never |
| | | 33 | | // answered — or broken — by this package. |
| | 3 | 34 | | services.TryAddSingleton(provider => |
| | 3 | 35 | | { |
| | 3 | 36 | | var options = provider.GetRequiredService<IOptions<MongoDbAsyncResponseChannelOptions>>(); |
| | 3 | 37 | | |
| | 3 | 38 | | var database = provider.GetService<IMongoDatabase>(); |
| | 3 | 39 | | if (database is not null) |
| | 3 | 40 | | return new MongoDbChannelStore(database, options); |
| | 3 | 41 | | |
| | 3 | 42 | | if (string.IsNullOrWhiteSpace(options.Value.DatabaseName)) |
| | 3 | 43 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseChannelOptions)}.{nameof(MongoDbAsyncR |
| | 3 | 44 | | |
| | 3 | 45 | | var sharedClient = provider.GetService<IMongoClient>(); |
| | 3 | 46 | | if (sharedClient is not null) |
| | 3 | 47 | | return new MongoDbChannelStore(sharedClient.GetDatabase(options.Value.DatabaseName), options); |
| | 3 | 48 | | |
| | 3 | 49 | | if (string.IsNullOrWhiteSpace(options.Value.ConnectionString)) |
| | 2 | 50 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseChannelOptions)}.{nameof(MongoDbAsyncR |
| | 3 | 51 | | |
| | 2 | 52 | | var ownedClient = new MongoClient(options.Value.ConnectionString); |
| | 3 | 53 | | return new MongoDbChannelStore(ownedClient.GetDatabase(options.Value.DatabaseName), options, ownedClient); |
| | 3 | 54 | | }); |
| | | 55 | | |
| | 3 | 56 | | services.TryAddSingleton<MongoDbRecoveryStateStore>(); |
| | 3 | 57 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateStore>(provider => provider.GetRequiredService<MongoD |
| | 3 | 58 | | services.Replace(ServiceDescriptor.Singleton<IRecoveryStateScanner>(provider => provider.GetRequiredService<Mong |
| | | 59 | | |
| | 3 | 60 | | services.TryAddSingleton<MongoDbAsyncResponseChannel>(); |
| | 3 | 61 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponsePublisher>(provider => provider.GetRequiredService<Mo |
| | 3 | 62 | | services.Replace(ServiceDescriptor.Singleton<IRawAsyncResponsePublisher>(provider => provider.GetRequiredService |
| | 3 | 63 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseSubscriber>(provider => provider.GetRequiredService<M |
| | 3 | 64 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseSubscriber>(provider => provider.GetRequir |
| | 3 | 65 | | services.Replace(ServiceDescriptor.Singleton<IActiveSubscriberProbe>(provider => provider.GetRequiredService<Mon |
| | | 66 | | |
| | 3 | 67 | | services.Replace(ServiceDescriptor.Singleton<IRecoverableAsyncResponseBuilder>(provider => new RecoverableAsyncR |
| | 3 | 68 | | provider.GetRequiredService<IRecoverableAsyncResponseSubscriber>(), |
| | 3 | 69 | | provider.GetService<IWorkerTransport>(), |
| | 3 | 70 | | provider.GetService<IAsyncResponseReplyTargetProvider>(), |
| | 3 | 71 | | provider.GetRequiredService<AsyncResponseContextPropagation>()))); |
| | 3 | 72 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseBuilder>(provider => provider.GetRequiredService<IRec |
| | | 73 | | |
| | 3 | 74 | | services.AddSingleton(new AsyncResponseChannelMarker(MongoDbAsyncResponseChannelOptions.ChannelName)); |
| | 3 | 75 | | return builder; |
| | | 76 | | } |
| | | 77 | | } |