| | | 1 | | using Microsoft.Extensions.Options; |
| | | 2 | | |
| | | 3 | | namespace AsyncResponse.Transports.SqlServer; |
| | | 4 | | |
| | 211 | 5 | | internal sealed class SqlServerReplyTargetProvider( |
| | 211 | 6 | | IOptions<SqlServerAsyncResponseTransportOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 7 | | { |
| | | 8 | | /// <inheritdoc /> |
| | | 9 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 10 | | { |
| | 16 | 11 | | var options = _options.Value; |
| | 16 | 12 | | SqlServerTransportOptionsValidator.ValidateCommon(options); |
| | 16 | 13 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 16 | 14 | | ? options.DefaultReplyTargetName |
| | 16 | 15 | | : name; |
| | | 16 | | |
| | 16 | 17 | | var target = ResolveTarget(options, targetName); |
| | 14 | 18 | | var queueOptionName = StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName) |
| | 14 | 19 | | ? $"{nameof(SqlServerReplyTargetOptions)}.{nameof(SqlServerReplyTargetOptions.ResponseQueue)}" |
| | 14 | 20 | | : $"{nameof(SqlServerAsyncResponseTransportOptions.ReplyTargets)}[\"{targetName}\"]." + |
| | 14 | 21 | | $"{nameof(SqlServerReplyTargetOptions.ResponseQueue)}"; |
| | 14 | 22 | | var responseQueue = SqlServerTransportOptionsValidator.Required(target.ResponseQueue, queueOptionName); |
| | | 23 | | |
| | | 24 | | // ValidateCommon covers the three transport-wide queues, but a NAMED reply target's queue |
| | | 25 | | // reaches the same nvarchar(200) column by a different route: it is emitted as the reply |
| | | 26 | | // address and as the "queue" property a remote publisher inserts with. Unvalidated, an |
| | | 27 | | // over-long name fails the insert outright and a space-padded one lands a row that the |
| | | 28 | | // exact-matching claim predicate will never hand to any subscriber. Validated here rather |
| | | 29 | | // than in ValidateCommon so a target added by mutating the dictionary directly is covered. |
| | 14 | 30 | | SqlServerTransportOptionsValidator.ValidateQueueName(responseQueue, queueOptionName); |
| | | 31 | | |
| | | 32 | | // ValidateCommon enforces three-way distinctness for the transport-wide queues because all |
| | | 33 | | // logical queues share one table; a NAMED target reaches that same table by another route |
| | | 34 | | // and must honor the same rule — a target aimed at the worker (or dead-letter) queue lands |
| | | 35 | | // responses as rows the worker subscriber claims, NAKs to the cap, and dead-letters, while |
| | | 36 | | // the waiter times out. Matching the transport-wide ResponseQueue is fine: that is |
| | | 37 | | // literally the default target's destination. |
| | 10 | 38 | | if (StringComparer.Ordinal.Equals(responseQueue, options.WorkerQueue) |
| | 10 | 39 | | || StringComparer.Ordinal.Equals(responseQueue, options.DeadLetterQueue)) |
| | | 40 | | { |
| | 4 | 41 | | throw new InvalidOperationException( |
| | 4 | 42 | | $"SQL Server async-response reply target '{targetName}' uses queue '{responseQueue}', which collides wit |
| | 4 | 43 | | $"{nameof(SqlServerAsyncResponseTransportOptions.WorkerQueue)} or {nameof(SqlServerAsyncResponseTranspor |
| | 4 | 44 | | "all queues share one table, so the target's responses would be consumed as worker jobs (or buried as de |
| | | 45 | | } |
| | | 46 | | |
| | 6 | 47 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 6 | 48 | | { |
| | 6 | 49 | | ["schema"] = options.SchemaName, |
| | 6 | 50 | | ["table"] = options.MessageTable, |
| | 6 | 51 | | ["queue"] = responseQueue, |
| | 6 | 52 | | ["correlationIdHeader"] = options.CorrelationIdHeader |
| | 6 | 53 | | }; |
| | | 54 | | |
| | 6 | 55 | | return new AsyncResponseReplyTarget |
| | 6 | 56 | | { |
| | 6 | 57 | | Name = targetName, |
| | 6 | 58 | | Transport = SqlServerAsyncResponseTransportOptions.TransportName, |
| | 6 | 59 | | Address = responseQueue, |
| | 6 | 60 | | Properties = properties |
| | 6 | 61 | | }; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | private static SqlServerReplyTargetOptions ResolveTarget( |
| | | 65 | | SqlServerAsyncResponseTransportOptions options, |
| | | 66 | | string targetName) |
| | | 67 | | { |
| | 16 | 68 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 12 | 69 | | return configured; |
| | | 70 | | |
| | 4 | 71 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName)) |
| | 2 | 72 | | return new SqlServerReplyTargetOptions { ResponseQueue = options.ResponseQueue }; |
| | | 73 | | |
| | 2 | 74 | | throw new InvalidOperationException( |
| | 2 | 75 | | $"SQL Server async-response reply target '{targetName}' is not configured. " + |
| | 2 | 76 | | $"Configure {nameof(SqlServerAsyncResponseTransportOptions.ResponseQueue)} for the default target " + |
| | 2 | 77 | | $"or add a named target with {nameof(SqlServerAsyncResponseTransportOptions.AddReplyTarget)}."); |
| | | 78 | | } |
| | | 79 | | } |