| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.AzureServiceBus; |
| | | 4 | | |
| | 3 | 5 | | internal sealed class AzureServiceBusReplyTargetProvider( |
| | 3 | 6 | | IOptions<AzureServiceBusAsyncResponseOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <summary>Gets the configured reply target.</summary> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 3 | 11 | | var options = _options.Value; |
| | 3 | 12 | | AzureServiceBusOptionsValidator.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 queue = AzureServiceBusOptionsValidator.Required( |
| | 3 | 19 | | target.Queue, |
| | 3 | 20 | | $"{nameof(AzureServiceBusReplyTargetOptions)}.{nameof(AzureServiceBusReplyTargetOptions.Queue)}"); |
| | | 21 | | |
| | 3 | 22 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 3 | 23 | | { |
| | 3 | 24 | | ["queue"] = queue, |
| | 3 | 25 | | ["correlationIdProperty"] = options.CorrelationIdProperty |
| | 3 | 26 | | }; |
| | | 27 | | |
| | 3 | 28 | | return new AsyncResponseReplyTarget |
| | 3 | 29 | | { |
| | 3 | 30 | | Name = targetName, |
| | 3 | 31 | | Transport = AzureServiceBusAsyncResponseOptions.TransportName, |
| | 3 | 32 | | Address = queue, |
| | 3 | 33 | | Properties = properties |
| | 3 | 34 | | }; |
| | | 35 | | } |
| | | 36 | | |
| | | 37 | | private static AzureServiceBusReplyTargetOptions ResolveTarget( |
| | | 38 | | AzureServiceBusAsyncResponseOptions options, |
| | | 39 | | string targetName) |
| | | 40 | | { |
| | 3 | 41 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 3 | 42 | | return configured; |
| | | 43 | | |
| | 3 | 44 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | 3 | 45 | | return new AzureServiceBusReplyTargetOptions { Queue = options.ResponseQueue }; |
| | | 46 | | |
| | 3 | 47 | | throw new InvalidOperationException( |
| | 3 | 48 | | $"Azure Service Bus async-response reply target '{targetName}' is not configured. " + |
| | 3 | 49 | | $"Configure {nameof(AzureServiceBusAsyncResponseOptions.ResponseQueue)} for the default target " + |
| | 3 | 50 | | $"or add a named target with {nameof(AzureServiceBusAsyncResponseOptions.AddReplyTarget)}."); |
| | | 51 | | } |
| | | 52 | | } |