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