| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.MongoDB; |
| | | 4 | | |
| | 3 | 5 | | internal sealed class MongoDbReplyTargetProvider( |
| | 3 | 6 | | IOptions<MongoDbAsyncResponseTransportOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 3 | 11 | | var options = _options.Value; |
| | 3 | 12 | | MongoDbTransportOptionsValidator.ValidateCommon(options); |
| | 3 | 13 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 3 | 14 | | ? options.DefaultReplyTargetName |
| | 3 | 15 | | : name; |
| | | 16 | | |
| | 3 | 17 | | var target = ResolveTarget(options, targetName); |
| | 3 | 18 | | var responseQueue = MongoDbTransportOptionsValidator.Required( |
| | 3 | 19 | | target.ResponseQueue, |
| | 3 | 20 | | $"{nameof(MongoDbReplyTargetOptions)}.{nameof(MongoDbReplyTargetOptions.ResponseQueue)}"); |
| | | 21 | | |
| | 3 | 22 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 3 | 23 | | { |
| | 3 | 24 | | ["collection"] = options.MessageCollection, |
| | 3 | 25 | | ["queue"] = responseQueue, |
| | 3 | 26 | | ["correlationIdHeader"] = options.CorrelationIdHeader |
| | 3 | 27 | | }; |
| | 3 | 28 | | if (!string.IsNullOrWhiteSpace(options.DatabaseName)) |
| | 3 | 29 | | properties["database"] = options.DatabaseName!; |
| | | 30 | | |
| | 3 | 31 | | return new AsyncResponseReplyTarget |
| | 3 | 32 | | { |
| | 3 | 33 | | Name = targetName, |
| | 3 | 34 | | Transport = MongoDbAsyncResponseTransportOptions.TransportName, |
| | 3 | 35 | | Address = responseQueue, |
| | 3 | 36 | | Properties = properties |
| | 3 | 37 | | }; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | private static MongoDbReplyTargetOptions ResolveTarget( |
| | | 41 | | MongoDbAsyncResponseTransportOptions options, |
| | | 42 | | string targetName) |
| | | 43 | | { |
| | 3 | 44 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 3 | 45 | | return configured; |
| | | 46 | | |
| | 3 | 47 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | 3 | 48 | | return new MongoDbReplyTargetOptions { ResponseQueue = options.ResponseQueue }; |
| | | 49 | | |
| | 3 | 50 | | throw new InvalidOperationException( |
| | 3 | 51 | | $"MongoDB async-response reply target '{targetName}' is not configured. " + |
| | 3 | 52 | | $"Configure {nameof(MongoDbAsyncResponseTransportOptions.ResponseQueue)} for the default target " + |
| | 3 | 53 | | $"or add a named target with {nameof(MongoDbAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 54 | | } |
| | | 55 | | } |