| | | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | | 2 | | // Licensed under the MIT License. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Azure.Core; |
| | | 10 | | using Azure.Messaging.ServiceBus.Diagnostics; |
| | | 11 | | using Azure.Messaging.ServiceBus.Plugins; |
| | | 12 | | |
| | | 13 | | namespace Azure.Messaging.ServiceBus |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// The <see cref="ServiceBusClient"/> is the top-level client through which all Service Bus |
| | | 17 | | /// entities can be interacted with. Any lower level types retrieved from here, such |
| | | 18 | | /// as <see cref="ServiceBusSender"/> and <see cref="ServiceBusReceiver"/> will share the |
| | | 19 | | /// same AMQP connection. Disposing the <see cref="ServiceBusClient"/> will cause the AMQP |
| | | 20 | | /// connection to close. |
| | | 21 | | /// </summary> |
| | | 22 | | public class ServiceBusClient : IAsyncDisposable |
| | | 23 | | { |
| | | 24 | | |
| | | 25 | | private readonly ServiceBusClientOptions _options; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// The fully qualified Service Bus namespace that the connection is associated with. This is likely |
| | | 29 | | /// to be similar to <c>{yournamespace}.servicebus.windows.net</c>. |
| | | 30 | | /// </summary> |
| | | 31 | | /// |
| | 78 | 32 | | public string FullyQualifiedNamespace => Connection.FullyQualifiedNamespace; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Indicates whether or not this <see cref="ServiceBusClient"/> has been disposed. |
| | | 36 | | /// </summary> |
| | | 37 | | /// |
| | | 38 | | /// <value> |
| | | 39 | | /// <c>true</c> if the client is disposed; otherwise, <c>false</c>. |
| | | 40 | | /// </value> |
| | 0 | 41 | | public bool IsDisposed { get; private set; } = false; |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// The transport type used for this <see cref="ServiceBusClient"/>. |
| | | 45 | | /// </summary> |
| | 0 | 46 | | public ServiceBusTransportType TransportType { get; } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// A unique name used to identify this <see cref="ServiceBusClient"/>. |
| | | 50 | | /// </summary> |
| | 44 | 51 | | internal string Identifier { get; } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// The instance of <see cref="ServiceBusEventSource" /> which can be mocked for testing. |
| | | 55 | | /// </summary> |
| | | 56 | | /// |
| | 0 | 57 | | internal ServiceBusEventSource Logger { get; set; } = ServiceBusEventSource.Log; |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// The list of plugins for the client. |
| | | 61 | | /// </summary> |
| | 0 | 62 | | internal IList<ServiceBusPlugin> Plugins { get; set; } = new List<ServiceBusPlugin>(); |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Performs the task needed to clean up resources used by the <see cref="ServiceBusConnection" />, |
| | | 66 | | /// including ensuring that the connection itself has been closed. |
| | | 67 | | /// </summary> |
| | | 68 | | /// |
| | | 69 | | /// <returns>A task to be resolved on when the operation has completed.</returns> |
| | | 70 | | [SuppressMessage("Usage", "AZC0002:Ensure all service methods take an optional CancellationToken parameter.", Ju |
| | | 71 | | public virtual async ValueTask DisposeAsync() |
| | | 72 | | { |
| | 0 | 73 | | Logger.ClientDisposeStart(typeof(ServiceBusClient), Identifier); |
| | 0 | 74 | | IsDisposed = true; |
| | | 75 | | try |
| | | 76 | | { |
| | 0 | 77 | | await Connection.CloseAsync(CancellationToken.None).ConfigureAwait(false); |
| | 0 | 78 | | } |
| | 0 | 79 | | catch (Exception ex) |
| | | 80 | | { |
| | 0 | 81 | | Logger.ClientDisposeException(typeof(ServiceBusClient), Identifier, ex); |
| | 0 | 82 | | throw; |
| | | 83 | | } |
| | | 84 | | finally |
| | | 85 | | { |
| | 0 | 86 | | Logger.ClientDisposeComplete(typeof(ServiceBusClient), Identifier); |
| | | 87 | | } |
| | 0 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <summary> |
| | | 91 | | /// Can be used for mocking. |
| | | 92 | | /// </summary> |
| | 0 | 93 | | protected ServiceBusClient() |
| | | 94 | | { |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// The connection that is used for the client. |
| | | 99 | | /// </summary> |
| | 132 | 100 | | internal ServiceBusConnection Connection { get; } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Initializes a new instance of the <see cref="ServiceBusClient"/> class. |
| | | 104 | | /// </summary> |
| | | 105 | | /// |
| | | 106 | | /// <param name="connectionString">The connection string to use for connecting to the |
| | | 107 | | /// Service Bus namespace.</param> |
| | | 108 | | /// |
| | | 109 | | /// <remarks> |
| | | 110 | | /// If the connection string specifies a specific entity name, any subsequent calls to |
| | | 111 | | /// <see cref="CreateSender(string)"/>, <see cref="CreateReceiver(string)"/>, |
| | | 112 | | /// <see cref="CreateProcessor(string)"/> etc. must still specify the same entity name. |
| | | 113 | | /// </remarks> |
| | | 114 | | public ServiceBusClient(string connectionString) : |
| | 38 | 115 | | this(connectionString, new ServiceBusClientOptions()) |
| | | 116 | | { |
| | 28 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <summary> |
| | | 120 | | /// Initializes a new instance of the <see cref="ServiceBusClient"/> class. |
| | | 121 | | /// </summary> |
| | | 122 | | /// |
| | | 123 | | /// <param name="connectionString">The connection string to use for connecting to the |
| | | 124 | | /// Service Bus namespace.</param> |
| | | 125 | | /// <param name="options">The set of <see cref="ServiceBusClientOptions"/> to use for |
| | | 126 | | /// configuring this <see cref="ServiceBusClient"/>.</param> |
| | | 127 | | /// |
| | | 128 | | /// <remarks> |
| | | 129 | | /// If the connection string specifies a specific entity name, any subsequent calls to |
| | | 130 | | /// <see cref="CreateSender(string)"/>, <see cref="CreateReceiver(string)"/>, |
| | | 131 | | /// <see cref="CreateProcessor(string)"/> etc. must still specify the same entity name. |
| | | 132 | | /// </remarks> |
| | 54 | 133 | | public ServiceBusClient(string connectionString, ServiceBusClientOptions options) |
| | | 134 | | { |
| | 54 | 135 | | _options = options?.Clone() ?? new ServiceBusClientOptions(); |
| | 54 | 136 | | Connection = new ServiceBusConnection(connectionString, _options); |
| | 38 | 137 | | Logger.ClientCreateStart(typeof(ServiceBusClient), FullyQualifiedNamespace); |
| | 38 | 138 | | Identifier = DiagnosticUtilities.GenerateIdentifier(FullyQualifiedNamespace); |
| | 38 | 139 | | Plugins = _options.Plugins; |
| | 38 | 140 | | Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier); |
| | 38 | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Initializes a new instance of the <see cref="ServiceBusClient"/> class. |
| | | 145 | | /// </summary> |
| | | 146 | | /// |
| | | 147 | | /// <param name="fullyQualifiedNamespace">The fully qualified Service Bus namespace to connect to. |
| | | 148 | | /// This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param> |
| | | 149 | | /// <param name="credential">The Azure managed identity credential to use for authorization. Access controls ma |
| | | 150 | | public ServiceBusClient(string fullyQualifiedNamespace, TokenCredential credential) : |
| | 8 | 151 | | this(fullyQualifiedNamespace, credential, new ServiceBusClientOptions()) |
| | | 152 | | { |
| | 0 | 153 | | } |
| | | 154 | | |
| | | 155 | | /// <summary> |
| | | 156 | | /// Initializes a new instance of the <see cref="ServiceBusClient"/> class. |
| | | 157 | | /// </summary> |
| | | 158 | | /// |
| | | 159 | | /// <param name="fullyQualifiedNamespace">The fully qualified Service Bus namespace to connect to. |
| | | 160 | | /// This is likely to be similar to <c>{yournamespace}.servicebus.windows.net</c>.</param> |
| | | 161 | | /// <param name="credential">The Azure managed identity credential to use for authorization. Access controls ma |
| | | 162 | | /// <param name="options">The set of <see cref="ServiceBusClientOptions"/> to use for configuring this <see cref |
| | 14 | 163 | | public ServiceBusClient( |
| | 14 | 164 | | string fullyQualifiedNamespace, |
| | 14 | 165 | | TokenCredential credential, |
| | 14 | 166 | | ServiceBusClientOptions options) |
| | | 167 | | { |
| | 14 | 168 | | _options = options?.Clone() ?? new ServiceBusClientOptions(); |
| | 14 | 169 | | Logger.ClientCreateStart(typeof(ServiceBusClient), fullyQualifiedNamespace); |
| | 14 | 170 | | Identifier = DiagnosticUtilities.GenerateIdentifier(fullyQualifiedNamespace); |
| | 14 | 171 | | Connection = new ServiceBusConnection( |
| | 14 | 172 | | fullyQualifiedNamespace, |
| | 14 | 173 | | credential, |
| | 14 | 174 | | _options); |
| | 4 | 175 | | Plugins = _options.Plugins; |
| | 4 | 176 | | Logger.ClientCreateComplete(typeof(ServiceBusClient), Identifier); |
| | 4 | 177 | | } |
| | | 178 | | |
| | | 179 | | /// <summary> |
| | | 180 | | /// Creates a <see cref="ServiceBusSender"/> instance that can be used for sending messages to a specific |
| | | 181 | | /// queue or topic. |
| | | 182 | | /// </summary> |
| | | 183 | | /// |
| | | 184 | | /// <param name="queueOrTopicName">The queue or topic to create a <see cref="ServiceBusSender"/> |
| | | 185 | | /// for.</param> |
| | | 186 | | /// |
| | | 187 | | /// <returns>A <see cref="ServiceBusSender"/> scoped to the specified queue or topic.</returns> |
| | | 188 | | public ServiceBusSender CreateSender(string queueOrTopicName) |
| | | 189 | | { |
| | 6 | 190 | | ValidateEntityName(queueOrTopicName); |
| | | 191 | | |
| | 4 | 192 | | return new ServiceBusSender( |
| | 4 | 193 | | entityPath: queueOrTopicName, |
| | 4 | 194 | | options: new ServiceBusSenderOptions(), |
| | 4 | 195 | | connection: Connection, |
| | 4 | 196 | | plugins: Plugins); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Creates a <see cref="ServiceBusSender"/> instance that can be used for sending messages to a specific |
| | | 201 | | /// queue or topic. |
| | | 202 | | /// </summary> |
| | | 203 | | /// |
| | | 204 | | /// <param name="queueOrTopicName">The queue or topic to create a <see cref="ServiceBusSender"/> for.</param> |
| | | 205 | | /// <param name="options">The set of <see cref="ServiceBusSenderOptions"/> to use for configuring |
| | | 206 | | /// this <see cref="ServiceBusSender"/>.</param> |
| | | 207 | | /// |
| | | 208 | | /// <returns>A <see cref="ServiceBusSender"/> scoped to the specified queue or topic.</returns> |
| | | 209 | | public ServiceBusSender CreateSender(string queueOrTopicName, ServiceBusSenderOptions options) |
| | | 210 | | { |
| | 12 | 211 | | ValidateSendViaEntityName(queueOrTopicName, options?.ViaQueueOrTopicName); |
| | | 212 | | |
| | 8 | 213 | | return new ServiceBusSender( |
| | 8 | 214 | | entityPath: queueOrTopicName, |
| | 8 | 215 | | options: options, |
| | 8 | 216 | | connection: Connection, |
| | 8 | 217 | | plugins: Plugins); |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving and settling messages |
| | | 222 | | /// from a specific queue. It uses <see cref="ReceiveMode"/> to specify how messages are received. Defaults to P |
| | | 223 | | /// If you want to change the <see cref="ReceiveMode"/>, use <see cref="CreateReceiver(string, ServiceBusReceive |
| | | 224 | | /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusReceiverOptions"/>. |
| | | 225 | | /// </summary> |
| | | 226 | | /// |
| | | 227 | | /// <param name="queueName">The queue to create a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 228 | | /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified queue.</returns> |
| | | 229 | | public ServiceBusReceiver CreateReceiver(string queueName) |
| | | 230 | | { |
| | 2 | 231 | | ValidateEntityName(queueName); |
| | | 232 | | |
| | 2 | 233 | | return new ServiceBusReceiver( |
| | 2 | 234 | | connection: Connection, |
| | 2 | 235 | | entityPath: queueName, |
| | 2 | 236 | | isSessionEntity: false, |
| | 2 | 237 | | plugins: Plugins, |
| | 2 | 238 | | options: new ServiceBusReceiverOptions()); |
| | | 239 | | } |
| | | 240 | | |
| | | 241 | | /// <summary> |
| | | 242 | | /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving and settling messages |
| | | 243 | | /// from a specific queue. It uses <see cref="ReceiveMode"/> to specify how messages are received. Defaults to P |
| | | 244 | | /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusReceiverOptions"/>. |
| | | 245 | | /// </summary> |
| | | 246 | | /// |
| | | 247 | | /// <param name="queueName">The queue to create a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 248 | | /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the |
| | | 249 | | /// <see cref="ServiceBusReceiver"/>.</param> |
| | | 250 | | /// |
| | | 251 | | /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified queue.</returns> |
| | | 252 | | public ServiceBusReceiver CreateReceiver( |
| | | 253 | | string queueName, |
| | | 254 | | ServiceBusReceiverOptions options) |
| | | 255 | | { |
| | 2 | 256 | | ValidateEntityName(queueName); |
| | | 257 | | |
| | 2 | 258 | | return new ServiceBusReceiver( |
| | 2 | 259 | | connection: Connection, |
| | 2 | 260 | | entityPath: queueName, |
| | 2 | 261 | | isSessionEntity: false, |
| | 2 | 262 | | plugins: Plugins, |
| | 2 | 263 | | options: options); |
| | | 264 | | } |
| | | 265 | | |
| | | 266 | | /// <summary> |
| | | 267 | | /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving and |
| | | 268 | | /// settling messages from a specific subscription. It uses <see cref="ReceiveMode"/> to specify |
| | | 269 | | /// how messages are received. Defaults to PeekLock mode. If you want to change the <see cref="ReceiveMode"/>, |
| | | 270 | | /// use <see cref="CreateReceiver(string, string, ServiceBusReceiverOptions)"/> method. |
| | | 271 | | /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusReceiverOptions"/>. |
| | | 272 | | /// </summary> |
| | | 273 | | /// |
| | | 274 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 275 | | /// <param name="subscriptionName">The subscription specific to the specified topic to create |
| | | 276 | | /// a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 277 | | /// |
| | | 278 | | /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified subscription and topic.</returns> |
| | | 279 | | public ServiceBusReceiver CreateReceiver( |
| | | 280 | | string topicName, |
| | | 281 | | string subscriptionName) |
| | | 282 | | { |
| | 0 | 283 | | ValidateEntityName(topicName); |
| | | 284 | | |
| | 0 | 285 | | return new ServiceBusReceiver( |
| | 0 | 286 | | connection: Connection, |
| | 0 | 287 | | entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName), |
| | 0 | 288 | | isSessionEntity: false, |
| | 0 | 289 | | plugins: Plugins, |
| | 0 | 290 | | options: new ServiceBusReceiverOptions()); |
| | | 291 | | } |
| | | 292 | | |
| | | 293 | | /// <summary> |
| | | 294 | | /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for |
| | | 295 | | /// receiving and settling messages from a specific subscription. It uses <see cref="ReceiveMode"/> to specify |
| | | 296 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 297 | | /// </summary> |
| | | 298 | | /// |
| | | 299 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 300 | | /// <param name="subscriptionName">The subscription specific to the specified topic to create |
| | | 301 | | /// a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 302 | | /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the |
| | | 303 | | /// <see cref="ServiceBusReceiver"/>.</param> |
| | | 304 | | /// |
| | | 305 | | /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the specified subscription and topic.</returns> |
| | | 306 | | public ServiceBusReceiver CreateReceiver( |
| | | 307 | | string topicName, |
| | | 308 | | string subscriptionName, |
| | | 309 | | ServiceBusReceiverOptions options) |
| | | 310 | | { |
| | 0 | 311 | | ValidateEntityName(topicName); |
| | | 312 | | |
| | 0 | 313 | | return new ServiceBusReceiver( |
| | 0 | 314 | | connection: Connection, |
| | 0 | 315 | | entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName), |
| | 0 | 316 | | isSessionEntity: false, |
| | 0 | 317 | | plugins: Plugins, |
| | 0 | 318 | | options: options); |
| | | 319 | | } |
| | | 320 | | |
| | | 321 | | /// <summary> |
| | | 322 | | /// Creates a <see cref="ServiceBusSessionReceiver"/> instance that can be used for receiving |
| | | 323 | | /// and settling messages from a specific session-enabled queue. It uses <see cref="ReceiveMode"/> to specify |
| | | 324 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 325 | | /// </summary> |
| | | 326 | | /// |
| | | 327 | | /// <param name="queueName">The session-enabled queue to create a <see cref="ServiceBusSessionReceiver"/> for.</ |
| | | 328 | | /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the |
| | | 329 | | /// <see cref="ServiceBusSessionReceiver"/>.</param> |
| | | 330 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | | 331 | | /// |
| | | 332 | | /// <remarks>Because this is establishing a session lock, this method performs a service call. If the |
| | | 333 | | /// sessionId parameter is not specified, and there are no available messages in the queue, this will |
| | | 334 | | /// throw a <see cref="ServiceBusException"/> with <see cref="ServiceBusException.Reason"/> of <see cref="Servic |
| | | 335 | | /// </remarks> |
| | | 336 | | /// |
| | | 337 | | /// <returns>A <see cref="ServiceBusSessionReceiver"/> scoped to the specified queue and a specific session.</re |
| | | 338 | | public virtual async Task<ServiceBusSessionReceiver> CreateSessionReceiverAsync( |
| | | 339 | | string queueName, |
| | | 340 | | ServiceBusSessionReceiverOptions options = default, |
| | | 341 | | CancellationToken cancellationToken = default) |
| | | 342 | | { |
| | 0 | 343 | | ValidateEntityName(queueName); |
| | | 344 | | |
| | 0 | 345 | | return await ServiceBusSessionReceiver.CreateSessionReceiverAsync( |
| | 0 | 346 | | entityPath: queueName, |
| | 0 | 347 | | connection: Connection, |
| | 0 | 348 | | plugins: Plugins, |
| | 0 | 349 | | options: options, |
| | 0 | 350 | | cancellationToken: cancellationToken).ConfigureAwait(false); |
| | 0 | 351 | | } |
| | | 352 | | |
| | | 353 | | /// <summary> |
| | | 354 | | /// Creates a <see cref="ServiceBusSessionReceiver"/> instance that can be used for receiving |
| | | 355 | | /// and settling messages from a specific session-enabled subscription. It uses <see cref="ReceiveMode"/> to spe |
| | | 356 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 357 | | /// </summary> |
| | | 358 | | /// |
| | | 359 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusSessionReceiver"/> for.</param> |
| | | 360 | | /// <param name="subscriptionName">The session-enabled subscription to create a <see cref="ServiceBusSessionRece |
| | | 361 | | /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the |
| | | 362 | | /// <see cref="ServiceBusSessionReceiver"/>.</param> |
| | | 363 | | /// <param name="cancellationToken">An optional <see cref="CancellationToken"/> instance to signal the request t |
| | | 364 | | /// |
| | | 365 | | /// <remarks>Because this is establishing a session lock, this method performs a service call. If the |
| | | 366 | | /// sessionId parameter is not specified, and there are no available messages in the queue, this will |
| | | 367 | | /// throw a <see cref="ServiceBusException"/> with <see cref="ServiceBusException.Reason"/> of <see cref="Servic |
| | | 368 | | /// </remarks> |
| | | 369 | | /// |
| | | 370 | | /// <returns>A <see cref="ServiceBusSessionReceiver"/> scoped to the specified queue and a specific session.</re |
| | | 371 | | public virtual async Task<ServiceBusSessionReceiver> CreateSessionReceiverAsync( |
| | | 372 | | string topicName, |
| | | 373 | | string subscriptionName, |
| | | 374 | | ServiceBusSessionReceiverOptions options = default, |
| | | 375 | | CancellationToken cancellationToken = default) |
| | | 376 | | { |
| | 0 | 377 | | ValidateEntityName(topicName); |
| | | 378 | | |
| | 0 | 379 | | return await ServiceBusSessionReceiver.CreateSessionReceiverAsync( |
| | 0 | 380 | | entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName), |
| | 0 | 381 | | connection: Connection, |
| | 0 | 382 | | plugins: Plugins, |
| | 0 | 383 | | options: options, |
| | 0 | 384 | | cancellationToken: cancellationToken).ConfigureAwait(false); |
| | 0 | 385 | | } |
| | | 386 | | |
| | | 387 | | /// <summary> |
| | | 388 | | /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving from the |
| | | 389 | | /// dead letter queue for the specified queue. It uses <see cref="ReceiveMode"/> to specify |
| | | 390 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 391 | | /// </summary> |
| | | 392 | | /// |
| | | 393 | | /// <param name="queueName">The queue to create a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 394 | | /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the |
| | | 395 | | /// <see cref="ServiceBusReceiver"/>.</param> |
| | | 396 | | /// |
| | | 397 | | /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the dead letter queue of the specified |
| | | 398 | | /// queue.</returns> |
| | | 399 | | public ServiceBusReceiver CreateDeadLetterReceiver( |
| | | 400 | | string queueName, |
| | | 401 | | ServiceBusReceiverOptions options = default) |
| | | 402 | | { |
| | 0 | 403 | | ValidateEntityName(queueName); |
| | | 404 | | |
| | 0 | 405 | | return new ServiceBusReceiver( |
| | 0 | 406 | | connection: Connection, |
| | 0 | 407 | | entityPath: EntityNameFormatter.FormatDeadLetterPath(queueName), |
| | 0 | 408 | | isSessionEntity: false, |
| | 0 | 409 | | plugins: Plugins, |
| | 0 | 410 | | options: options); |
| | | 411 | | } |
| | | 412 | | |
| | | 413 | | /// <summary> |
| | | 414 | | /// Creates a <see cref="ServiceBusReceiver"/> instance that can be used for receiving from the |
| | | 415 | | /// dead letter queue for the specified subscription. It uses <see cref="ReceiveMode"/> to specify |
| | | 416 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 417 | | /// </summary> |
| | | 418 | | /// |
| | | 419 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 420 | | /// <param name="subscriptionName">The subscription to create a <see cref="ServiceBusReceiver"/> for.</param> |
| | | 421 | | /// <param name="options">The set of <see cref="ServiceBusReceiverOptions"/> to use for configuring the |
| | | 422 | | /// <see cref="ServiceBusReceiver"/>.</param> |
| | | 423 | | /// |
| | | 424 | | /// <returns>A <see cref="ServiceBusReceiver"/> scoped to the dead letter queue of the specified |
| | | 425 | | /// queue.</returns> |
| | | 426 | | public ServiceBusReceiver CreateDeadLetterReceiver( |
| | | 427 | | string topicName, |
| | | 428 | | string subscriptionName, |
| | | 429 | | ServiceBusReceiverOptions options = default) |
| | | 430 | | { |
| | 0 | 431 | | ValidateEntityName(topicName); |
| | | 432 | | |
| | 0 | 433 | | return new ServiceBusReceiver( |
| | 0 | 434 | | connection: Connection, |
| | 0 | 435 | | entityPath: EntityNameFormatter.FormatDeadLetterPath( |
| | 0 | 436 | | EntityNameFormatter.FormatSubscriptionPath( |
| | 0 | 437 | | topicName, |
| | 0 | 438 | | subscriptionName)), |
| | 0 | 439 | | isSessionEntity: false, |
| | 0 | 440 | | plugins: Plugins, |
| | 0 | 441 | | options: options); |
| | | 442 | | } |
| | | 443 | | |
| | | 444 | | /// <summary> |
| | | 445 | | /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using |
| | | 446 | | /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify |
| | | 447 | | /// how messages are received. Defaults to PeekLock mode. If you want to change the <see cref="ReceiveMode"/>, |
| | | 448 | | /// use <see cref="CreateProcessor(string, ServiceBusProcessorOptions)"/> method. |
| | | 449 | | /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusProcessorOptions"/> type. |
| | | 450 | | /// </summary> |
| | | 451 | | /// |
| | | 452 | | /// <param name="queueName">The queue to create a <see cref="ServiceBusProcessor"/> for.</param> |
| | | 453 | | /// |
| | | 454 | | /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified queue.</returns> |
| | | 455 | | public ServiceBusProcessor CreateProcessor(string queueName) |
| | | 456 | | { |
| | 0 | 457 | | ValidateEntityName(queueName); |
| | | 458 | | |
| | 0 | 459 | | return new ServiceBusProcessor( |
| | 0 | 460 | | entityPath: queueName, |
| | 0 | 461 | | connection: Connection, |
| | 0 | 462 | | isSessionEntity: false, |
| | 0 | 463 | | plugins: Plugins, |
| | 0 | 464 | | options: new ServiceBusProcessorOptions()); |
| | | 465 | | } |
| | | 466 | | |
| | | 467 | | /// <summary> |
| | | 468 | | /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using |
| | | 469 | | /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify |
| | | 470 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 471 | | /// </summary> |
| | | 472 | | /// |
| | | 473 | | /// <param name="queueName">The queue to create a <see cref="ServiceBusProcessor"/> for.</param> |
| | | 474 | | /// <param name="options">The set of <see cref="ServiceBusProcessorOptions"/> to use for configuring the |
| | | 475 | | /// <see cref="ServiceBusProcessor"/>.</param> |
| | | 476 | | /// |
| | | 477 | | /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified queue.</returns> |
| | | 478 | | public ServiceBusProcessor CreateProcessor( |
| | | 479 | | string queueName, |
| | | 480 | | ServiceBusProcessorOptions options) |
| | | 481 | | { |
| | 2 | 482 | | ValidateEntityName(queueName); |
| | | 483 | | |
| | 2 | 484 | | return new ServiceBusProcessor( |
| | 2 | 485 | | entityPath: queueName, |
| | 2 | 486 | | connection: Connection, |
| | 2 | 487 | | isSessionEntity: false, |
| | 2 | 488 | | plugins: Plugins, |
| | 2 | 489 | | options: options); |
| | | 490 | | } |
| | | 491 | | |
| | | 492 | | /// <summary> |
| | | 493 | | /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using |
| | | 494 | | /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify |
| | | 495 | | /// how messages are received. Defaults to PeekLock mode. If you want to change the <see cref="ReceiveMode"/>, |
| | | 496 | | /// use <see cref="CreateProcessor(string, string, ServiceBusProcessorOptions)"/> method. |
| | | 497 | | /// The <see cref="ReceiveMode"/> is set in <see cref="ServiceBusProcessorOptions"/> type. |
| | | 498 | | /// </summary> |
| | | 499 | | /// |
| | | 500 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusProcessor"/> for.</param> |
| | | 501 | | /// <param name="subscriptionName">The subcription to create a <see cref="ServiceBusProcessor"/> for.</param> |
| | | 502 | | /// |
| | | 503 | | /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified topic and subscription.</returns> |
| | | 504 | | public ServiceBusProcessor CreateProcessor( |
| | | 505 | | string topicName, |
| | | 506 | | string subscriptionName) |
| | | 507 | | { |
| | 0 | 508 | | ValidateEntityName(topicName); |
| | | 509 | | |
| | 0 | 510 | | return new ServiceBusProcessor( |
| | 0 | 511 | | entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName), |
| | 0 | 512 | | connection: Connection, |
| | 0 | 513 | | isSessionEntity: false, |
| | 0 | 514 | | plugins: Plugins, |
| | 0 | 515 | | options: new ServiceBusProcessorOptions()); |
| | | 516 | | } |
| | | 517 | | |
| | | 518 | | /// <summary> |
| | | 519 | | /// Creates a <see cref="ServiceBusProcessor"/> instance that can be used to process messages using |
| | | 520 | | /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify |
| | | 521 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 522 | | /// </summary> |
| | | 523 | | /// |
| | | 524 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusProcessor"/> for.</param> |
| | | 525 | | /// <param name="subscriptionName">The subcription to create a <see cref="ServiceBusProcessor"/> for.</param> |
| | | 526 | | /// <param name="options">The set of <see cref="ServiceBusProcessorOptions"/> to use for configuring the |
| | | 527 | | /// <see cref="ServiceBusProcessor"/>.</param> |
| | | 528 | | /// |
| | | 529 | | /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified topic and subscription.</returns> |
| | | 530 | | public ServiceBusProcessor CreateProcessor( |
| | | 531 | | string topicName, |
| | | 532 | | string subscriptionName, |
| | | 533 | | ServiceBusProcessorOptions options) |
| | | 534 | | { |
| | 0 | 535 | | ValidateEntityName(topicName); |
| | | 536 | | |
| | 0 | 537 | | return new ServiceBusProcessor( |
| | 0 | 538 | | entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName), |
| | 0 | 539 | | connection: Connection, |
| | 0 | 540 | | isSessionEntity: false, |
| | 0 | 541 | | plugins: Plugins, |
| | 0 | 542 | | options: options); |
| | | 543 | | } |
| | | 544 | | |
| | | 545 | | /// <summary> |
| | | 546 | | /// Creates a <see cref="ServiceBusSessionProcessor"/> instance that can be used to process session messages usi |
| | | 547 | | /// event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify |
| | | 548 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 549 | | /// </summary> |
| | | 550 | | /// |
| | | 551 | | /// <param name="queueName">The queue to create a <see cref="ServiceBusSessionProcessor"/> for.</param> |
| | | 552 | | /// <param name="options">The set of <see cref="ServiceBusProcessorOptions"/> to use for configuring the |
| | | 553 | | /// <see cref="ServiceBusSessionProcessor"/>.</param> |
| | | 554 | | /// <returns>A <see cref="ServiceBusSessionProcessor"/> scoped to the specified queue.</returns> |
| | | 555 | | public ServiceBusSessionProcessor CreateSessionProcessor( |
| | | 556 | | string queueName, |
| | | 557 | | ServiceBusSessionProcessorOptions options = default) |
| | | 558 | | { |
| | 2 | 559 | | ValidateEntityName(queueName); |
| | | 560 | | |
| | 2 | 561 | | return new ServiceBusSessionProcessor( |
| | 2 | 562 | | entityPath: queueName, |
| | 2 | 563 | | connection: Connection, |
| | 2 | 564 | | plugins: Plugins, |
| | 2 | 565 | | options: options ?? new ServiceBusSessionProcessorOptions()); |
| | | 566 | | } |
| | | 567 | | |
| | | 568 | | /// <summary> |
| | | 569 | | /// Creates a <see cref="ServiceBusSessionProcessor"/> instance that can be used to process |
| | | 570 | | /// messages using event handlers that are set on the processor. It uses <see cref="ReceiveMode"/> to specify |
| | | 571 | | /// how messages are received. Defaults to PeekLock mode. The <see cref="ReceiveMode"/> is set in <see cref="Ser |
| | | 572 | | /// </summary> |
| | | 573 | | /// |
| | | 574 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusSessionProcessor"/> for.</param> |
| | | 575 | | /// <param name="subscriptionName">The subcription to create a <see cref="ServiceBusSessionProcessor"/> for.</pa |
| | | 576 | | /// <param name="options">The set of <see cref="ServiceBusSessionProcessor"/> to use for configuring the |
| | | 577 | | /// <see cref="ServiceBusSessionProcessor"/>.</param> |
| | | 578 | | /// |
| | | 579 | | /// <returns>A <see cref="ServiceBusProcessor"/> scoped to the specified topic and subscription.</returns> |
| | | 580 | | public ServiceBusSessionProcessor CreateSessionProcessor( |
| | | 581 | | string topicName, |
| | | 582 | | string subscriptionName, |
| | | 583 | | ServiceBusSessionProcessorOptions options = default) |
| | | 584 | | { |
| | 0 | 585 | | ValidateEntityName(topicName); |
| | | 586 | | |
| | 0 | 587 | | return new ServiceBusSessionProcessor( |
| | 0 | 588 | | entityPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName), |
| | 0 | 589 | | connection: Connection, |
| | 0 | 590 | | plugins: Plugins, |
| | 0 | 591 | | options: options ?? new ServiceBusSessionProcessorOptions()); |
| | | 592 | | } |
| | | 593 | | |
| | | 594 | | /// <summary> |
| | | 595 | | /// The <see cref="ServiceBusRuleManager"/> is used to manage the rules for a subscription. |
| | | 596 | | /// </summary> |
| | | 597 | | /// |
| | | 598 | | /// <param name="topicName">The topic to create a <see cref="ServiceBusRuleManager"/> for.</param> |
| | | 599 | | /// <param name="subscriptionName">The subscription specific to the specified topic to create |
| | | 600 | | /// a <see cref="ServiceBusRuleManager"/> for.</param> |
| | | 601 | | /// |
| | | 602 | | /// <returns>A <see cref="ServiceBusRuleManager"/> scoped to the specified subscription and topic.</returns> |
| | | 603 | | internal ServiceBusRuleManager CreateRuleManager(string topicName, string subscriptionName) |
| | | 604 | | { |
| | 0 | 605 | | ValidateEntityName(topicName); |
| | | 606 | | |
| | 0 | 607 | | return new ServiceBusRuleManager( |
| | 0 | 608 | | connection: Connection, |
| | 0 | 609 | | subscriptionPath: EntityNameFormatter.FormatSubscriptionPath(topicName, subscriptionName)); |
| | | 610 | | } |
| | | 611 | | |
| | | 612 | | /// <summary> |
| | | 613 | | /// Validates that the specified entity name matches the entity path in the Connection, |
| | | 614 | | /// if an entity path is specified in the connection. |
| | | 615 | | /// </summary> |
| | | 616 | | /// |
| | | 617 | | /// <param name="entityName">Entity name to validate</param> |
| | | 618 | | private void ValidateEntityName(string entityName) |
| | | 619 | | { |
| | | 620 | | // The entity name may only be specified in one of the possible forms, either as part of the |
| | | 621 | | // connection string or as a stand-alone parameter, but not both. If specified in both to the same |
| | | 622 | | // value, then do not consider this a failure. |
| | | 623 | | |
| | 22 | 624 | | if (!string.IsNullOrEmpty(Connection.EntityPath) && !string.Equals(entityName, Connection.EntityPath, String |
| | | 625 | | { |
| | 4 | 626 | | throw new ArgumentException(Resources.OnlyOneEntityNameMayBeSpecified); |
| | | 627 | | } |
| | 18 | 628 | | } |
| | | 629 | | |
| | | 630 | | /// <summary> |
| | | 631 | | /// Validates that the specified entity name matches the entity path in the Connection, |
| | | 632 | | /// if an entity path is specified in the connection. |
| | | 633 | | /// </summary> |
| | | 634 | | /// <param name="entityName">Entity name to validate</param> |
| | | 635 | | /// |
| | | 636 | | /// <param name="sendViaEntityName">The send via entity name to validate</param> |
| | | 637 | | private void ValidateSendViaEntityName(string entityName, string sendViaEntityName) |
| | | 638 | | { |
| | 12 | 639 | | if (sendViaEntityName == null || |
| | 12 | 640 | | string.Equals(sendViaEntityName, entityName, StringComparison.InvariantCultureIgnoreCase)) |
| | | 641 | | { |
| | 8 | 642 | | ValidateEntityName(sendViaEntityName); |
| | 6 | 643 | | return; |
| | | 644 | | } |
| | | 645 | | |
| | | 646 | | // we've established they are not equal, so anything specified in connection string will cause |
| | | 647 | | // a mismatch with one of the entities. |
| | | 648 | | |
| | 4 | 649 | | if (!string.IsNullOrEmpty(Connection.EntityPath)) |
| | | 650 | | { |
| | 2 | 651 | | throw new ArgumentException(Resources.SendViaCannotBeUsedWithEntityInConnectionString); |
| | | 652 | | } |
| | 2 | 653 | | } |
| | | 654 | | } |
| | | 655 | | } |