| | | 1 | | using AsyncResponse; |
| | | 2 | | using AsyncResponse.Internal; |
| | | 3 | | using AsyncResponse.Transports.MongoDB; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | using Microsoft.Extensions.Options; |
| | | 7 | | using MongoDB.Driver; |
| | | 8 | | |
| | | 9 | | namespace Microsoft.Extensions.DependencyInjection; |
| | | 10 | | |
| | | 11 | | /// <summary>DI registration for the MongoDB AsyncResponse transport package.</summary> |
| | | 12 | | public static class MongoDbAsyncResponseTransportServiceCollectionExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Registers MongoDB as the worker transport and response ingress in one call: |
| | | 16 | | /// worker jobs are inserted into the configured worker queue, hosted subscribers claim documents |
| | | 17 | | /// atomically with <c>findOneAndUpdate</c>, and response documents are fed into the |
| | | 18 | | /// transport-neutral <see cref="IAsyncResponseIngress"/>. Hosts may register an |
| | | 19 | | /// <see cref="IMongoDatabase"/> (or an <see cref="IMongoClient"/> plus |
| | | 20 | | /// <see cref="MongoDbAsyncResponseTransportOptions.DatabaseName"/>) singleton, or set |
| | | 21 | | /// <see cref="MongoDbAsyncResponseTransportOptions.ConnectionString"/> here. |
| | | 22 | | /// </summary> |
| | | 23 | | public static AsyncResponseRegistrationBuilder WithMongoDbTransport( |
| | | 24 | | this AsyncResponseRegistrationBuilder builder, |
| | | 25 | | Action<MongoDbAsyncResponseTransportOptions> configure) |
| | | 26 | | { |
| | 212 | 27 | | ArgumentNullException.ThrowIfNull(configure); |
| | | 28 | | |
| | 210 | 29 | | var services = builder.Services; |
| | 210 | 30 | | services.AddOptions(); |
| | 210 | 31 | | services.Configure(configure); |
| | | 32 | | |
| | | 33 | | // The store reuses a host-registered IMongoDatabase / IMongoClient when present; otherwise it |
| | | 34 | | // creates and owns a client from the options. Nothing is registered as a bare |
| | | 35 | | // IMongoClient/IMongoDatabase service, so unrelated resolutions of those types are never |
| | | 36 | | // answered — or broken — by this package. |
| | 210 | 37 | | services.TryAddSingleton<IMongoNamespaceRegistry, MongoNamespaceRegistry>(); |
| | 210 | 38 | | services.TryAddSingleton(provider => |
| | 210 | 39 | | { |
| | 204 | 40 | | var options = provider.GetRequiredService<IOptions<MongoDbAsyncResponseTransportOptions>>(); |
| | 204 | 41 | | var logger = provider.GetService<ILogger<MongoDbTransportStore>>(); |
| | 204 | 42 | | var registry = provider.GetRequiredService<IMongoNamespaceRegistry>(); |
| | 210 | 43 | | |
| | 204 | 44 | | var database = provider.GetService<IMongoDatabase>(); |
| | 204 | 45 | | if (database is not null) |
| | 198 | 46 | | return new MongoDbTransportStore(database, options, logger: logger, namespaceRegistry: registry); |
| | 210 | 47 | | |
| | 6 | 48 | | if (string.IsNullOrWhiteSpace(options.Value.DatabaseName)) |
| | 2 | 49 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(MongoDbAsyn |
| | 210 | 50 | | |
| | 4 | 51 | | var sharedClient = provider.GetService<IMongoClient>(); |
| | 4 | 52 | | if (sharedClient is not null) |
| | 2 | 53 | | return new MongoDbTransportStore(sharedClient.GetDatabase(options.Value.DatabaseName), options, logger: |
| | 210 | 54 | | |
| | 2 | 55 | | if (string.IsNullOrWhiteSpace(options.Value.ConnectionString)) |
| | 0 | 56 | | throw new InvalidOperationException($"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(MongoDbAsyn |
| | 210 | 57 | | |
| | 2 | 58 | | var ownedClient = new MongoClient(options.Value.ConnectionString); |
| | 2 | 59 | | return new MongoDbTransportStore(ownedClient.GetDatabase(options.Value.DatabaseName), options, ownedClient, |
| | 210 | 60 | | }); |
| | | 61 | | |
| | 406 | 62 | | services.TryAddSingleton(provider => new MongoDbWorkerTransport( |
| | 406 | 63 | | provider.GetRequiredService<IOptions<MongoDbAsyncResponseTransportOptions>>(), |
| | 406 | 64 | | provider.GetRequiredService<MongoDbTransportStore>())); |
| | 406 | 65 | | services.Replace(ServiceDescriptor.Singleton<IWorkerTransport>(provider => provider.GetRequiredService<MongoDbWo |
| | 210 | 66 | | services.Replace(ServiceDescriptor.Singleton<IAsyncResponseReplyTargetProvider, MongoDbReplyTargetProvider>()); |
| | 210 | 67 | | services.AddSingleton(provider => |
| | 210 | 68 | | { |
| | 210 | 69 | | // Resolved ack modes declared to the Core startup validator, which vetoes early ACK on |
| | 210 | 70 | | // the worker queue durable-flow wake-ups ride (see AsyncResponseStartupValidator). |
| | 200 | 71 | | var options = provider.GetRequiredService<Microsoft.Extensions.Options.IOptions<MongoDbAsyncResponseTranspor |
| | 200 | 72 | | return new AsyncResponseTransportMarker(MongoDbAsyncResponseTransportOptions.TransportName) |
| | 200 | 73 | | { |
| | 200 | 74 | | WorkerSubscriberUsesEarlyAck = options.WorkerSubscriber.AckMode == MongoDbAckMode.AckAfterEnqueue, |
| | 200 | 75 | | WorkerAckModePath = $"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.WorkerSubscriber)}. |
| | 200 | 76 | | ResponseSubscriberUsesEarlyAck = options.ResponseSubscriber.AckMode == MongoDbAckMode.AckAfterEnqueue, |
| | 200 | 77 | | ResponseAckModePath = $"{nameof(MongoDbAsyncResponseTransportOptions)}.{nameof(options.ResponseSubscribe |
| | 200 | 78 | | }; |
| | 210 | 79 | | }); |
| | | 80 | | |
| | 210 | 81 | | services.AddHostedService<MongoDbWorkerSubscriber>(); |
| | 210 | 82 | | services.AddHostedService<MongoDbResponseIngressSubscriber>(); |
| | | 83 | | |
| | 210 | 84 | | return builder; |
| | | 85 | | } |
| | | 86 | | } |