| | | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | | 3 | | |
| | | 4 | | namespace Microsoft.Azure.ServiceBus.Core |
| | | 5 | | { |
| | | 6 | | using System; |
| | | 7 | | using System.Collections.Generic; |
| | | 8 | | using System.Diagnostics; |
| | | 9 | | using System.Linq; |
| | | 10 | | using System.Threading; |
| | | 11 | | using System.Threading.Tasks; |
| | | 12 | | using System.Transactions; |
| | | 13 | | using Microsoft.Azure.Amqp; |
| | | 14 | | using Microsoft.Azure.Amqp.Encoding; |
| | | 15 | | using Microsoft.Azure.Amqp.Framing; |
| | | 16 | | using Microsoft.Azure.ServiceBus.Amqp; |
| | | 17 | | using Microsoft.Azure.ServiceBus.Primitives; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// The MessageSender can be used to send messages to Queues or Topics. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <example> |
| | | 23 | | /// Create a new MessageSender to send to a Queue |
| | | 24 | | /// <code> |
| | | 25 | | /// IMessageSender messageSender = new MessageSender( |
| | | 26 | | /// namespaceConnectionString, |
| | | 27 | | /// queueName) |
| | | 28 | | /// </code> |
| | | 29 | | /// |
| | | 30 | | /// Send message |
| | | 31 | | /// <code> |
| | | 32 | | /// byte[] data = GetData(); |
| | | 33 | | /// await messageSender.SendAsync(data); |
| | | 34 | | /// </code> |
| | | 35 | | /// </example> |
| | | 36 | | /// <remarks>This uses AMQP protocol to communicate with service.</remarks> |
| | | 37 | | public class MessageSender : ClientEntity, IMessageSender |
| | | 38 | | { |
| | | 39 | | int deliveryCount; |
| | | 40 | | readonly ActiveClientLinkManager clientLinkManager; |
| | | 41 | | readonly ServiceBusDiagnosticSource diagnosticSource; |
| | | 42 | | readonly bool isViaSender; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Creates a new AMQP MessageSender. |
| | | 46 | | /// </summary> |
| | | 47 | | /// <param name="connectionStringBuilder">The <see cref="ServiceBusConnectionStringBuilder"/> having entity leve |
| | | 48 | | /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with Service Bu |
| | | 49 | | /// <remarks>Creates a new connection to the entity, which is opened during the first operation.</remarks> |
| | | 50 | | public MessageSender( |
| | | 51 | | ServiceBusConnectionStringBuilder connectionStringBuilder, |
| | | 52 | | RetryPolicy retryPolicy = null) |
| | 0 | 53 | | : this(connectionStringBuilder?.GetNamespaceConnectionString(), connectionStringBuilder?.EntityPath, retryPo |
| | | 54 | | { |
| | 0 | 55 | | } |
| | | 56 | | |
| | | 57 | | /// <summary> |
| | | 58 | | /// Creates a new AMQP MessageSender. |
| | | 59 | | /// </summary> |
| | | 60 | | /// <param name="connectionString">Namespace connection string used to communicate with Service Bus. Must not co |
| | | 61 | | /// <param name="entityPath">The path of the entity this sender should connect to.</param> |
| | | 62 | | /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with Service Bu |
| | | 63 | | /// <remarks>Creates a new connection to the entity, which is opened during the first operation.</remarks> |
| | | 64 | | public MessageSender( |
| | | 65 | | string connectionString, |
| | | 66 | | string entityPath, |
| | | 67 | | RetryPolicy retryPolicy = null) |
| | 0 | 68 | | : this(entityPath, null, null, new ServiceBusConnection(connectionString), null, retryPolicy) |
| | | 69 | | { |
| | 0 | 70 | | if (string.IsNullOrWhiteSpace(connectionString)) |
| | | 71 | | { |
| | 0 | 72 | | throw Fx.Exception.ArgumentNullOrWhiteSpace(connectionString); |
| | | 73 | | } |
| | | 74 | | |
| | 0 | 75 | | this.OwnsConnection = true; |
| | 0 | 76 | | } |
| | | 77 | | |
| | | 78 | | /// <summary> |
| | | 79 | | /// Creates a new MessageSender |
| | | 80 | | /// </summary> |
| | | 81 | | /// <param name="endpoint">Fully qualified domain name for Service Bus. Most likely, {yournamespace}.servicebus. |
| | | 82 | | /// <param name="entityPath">Queue path.</param> |
| | | 83 | | /// <param name="tokenProvider">Token provider which will generate security tokens for authorization.</param> |
| | | 84 | | /// <param name="transportType">Transport type.</param> |
| | | 85 | | /// <param name="retryPolicy">Retry policy for queue operations. Defaults to <see cref="RetryPolicy.Default"/></ |
| | | 86 | | /// <remarks>Creates a new connection to the entity, which is opened during the first operation.</remarks> |
| | | 87 | | public MessageSender( |
| | | 88 | | string endpoint, |
| | | 89 | | string entityPath, |
| | | 90 | | ITokenProvider tokenProvider, |
| | | 91 | | TransportType transportType = TransportType.Amqp, |
| | | 92 | | RetryPolicy retryPolicy = null) |
| | 0 | 93 | | : this(entityPath, null, null, new ServiceBusConnection(endpoint, transportType, retryPolicy) {TokenProvider |
| | | 94 | | { |
| | 0 | 95 | | this.OwnsConnection = true; |
| | 0 | 96 | | } |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Creates a new AMQP MessageSender on a given <see cref="ServiceBusConnection"/> |
| | | 100 | | /// </summary> |
| | | 101 | | /// <param name="serviceBusConnection">Connection object to the service bus namespace.</param> |
| | | 102 | | /// <param name="entityPath">The path of the entity this sender should connect to.</param> |
| | | 103 | | /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with Service Bu |
| | | 104 | | public MessageSender( |
| | | 105 | | ServiceBusConnection serviceBusConnection, |
| | | 106 | | string entityPath, |
| | | 107 | | RetryPolicy retryPolicy = null) |
| | 6 | 108 | | : this(entityPath, null, null, serviceBusConnection, null, retryPolicy) |
| | | 109 | | { |
| | 6 | 110 | | this.OwnsConnection = false; |
| | 6 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <summary> |
| | | 114 | | /// Creates a ViaMessageSender. This can be used to send messages to a destination entity via another another en |
| | | 115 | | /// </summary> |
| | | 116 | | /// <param name="serviceBusConnection">Connection object to the service bus namespace.</param> |
| | | 117 | | /// <param name="entityPath">The final destination of the message.</param> |
| | | 118 | | /// <param name="viaEntityPath">The first destination of the message.</param> |
| | | 119 | | /// <param name="retryPolicy">The <see cref="RetryPolicy"/> that will be used when communicating with Service Bu |
| | | 120 | | /// <remarks> |
| | | 121 | | /// This is mainly to be used when sending messages in a transaction. |
| | | 122 | | /// When messages need to be sent across entities in a single transaction, this can be used to ensure |
| | | 123 | | /// all the messages land initially in the same entity/partition for local transactions, and then |
| | | 124 | | /// let service bus handle transferring the message to the actual destination. |
| | | 125 | | /// </remarks> |
| | | 126 | | public MessageSender( |
| | | 127 | | ServiceBusConnection serviceBusConnection, |
| | | 128 | | string entityPath, |
| | | 129 | | string viaEntityPath, |
| | | 130 | | RetryPolicy retryPolicy = null) |
| | 6 | 131 | | :this(viaEntityPath, entityPath, null, serviceBusConnection, null, retryPolicy) |
| | | 132 | | { |
| | 6 | 133 | | this.OwnsConnection = false; |
| | 6 | 134 | | } |
| | | 135 | | |
| | | 136 | | internal MessageSender( |
| | | 137 | | string entityPath, |
| | | 138 | | string transferDestinationPath, |
| | | 139 | | MessagingEntityType? entityType, |
| | | 140 | | ServiceBusConnection serviceBusConnection, |
| | | 141 | | ICbsTokenProvider cbsTokenProvider, |
| | | 142 | | RetryPolicy retryPolicy) |
| | 12 | 143 | | : base(nameof(MessageSender), entityPath, retryPolicy ?? RetryPolicy.Default) |
| | | 144 | | { |
| | 12 | 145 | | MessagingEventSource.Log.MessageSenderCreateStart(serviceBusConnection?.Endpoint.Authority, entityPath); |
| | | 146 | | |
| | 12 | 147 | | if (string.IsNullOrWhiteSpace(entityPath)) |
| | | 148 | | { |
| | 0 | 149 | | throw Fx.Exception.ArgumentNullOrWhiteSpace(entityPath); |
| | | 150 | | } |
| | | 151 | | |
| | 12 | 152 | | this.ServiceBusConnection = serviceBusConnection ?? throw new ArgumentNullException(nameof(serviceBusConnect |
| | 12 | 153 | | this.Path = entityPath; |
| | 12 | 154 | | this.SendingLinkDestination = entityPath; |
| | 12 | 155 | | this.EntityType = entityType; |
| | 12 | 156 | | this.ServiceBusConnection.ThrowIfClosed(); |
| | | 157 | | |
| | 12 | 158 | | if (cbsTokenProvider != null) |
| | | 159 | | { |
| | 0 | 160 | | this.CbsTokenProvider = cbsTokenProvider; |
| | | 161 | | } |
| | 12 | 162 | | else if (this.ServiceBusConnection.TokenProvider != null) |
| | | 163 | | { |
| | 12 | 164 | | this.CbsTokenProvider = new TokenProviderAdapter(this.ServiceBusConnection.TokenProvider, this.ServiceBu |
| | | 165 | | } |
| | | 166 | | else |
| | | 167 | | { |
| | 0 | 168 | | throw new ArgumentNullException($"{nameof(ServiceBusConnection)} doesn't have a valid token provider"); |
| | | 169 | | } |
| | | 170 | | |
| | 12 | 171 | | this.SendLinkManager = new FaultTolerantAmqpObject<SendingAmqpLink>(this.CreateLinkAsync, CloseSession); |
| | 12 | 172 | | this.RequestResponseLinkManager = new FaultTolerantAmqpObject<RequestResponseAmqpLink>(this.CreateRequestRes |
| | 12 | 173 | | this.clientLinkManager = new ActiveClientLinkManager(this, this.CbsTokenProvider); |
| | 12 | 174 | | this.diagnosticSource = new ServiceBusDiagnosticSource(entityPath, serviceBusConnection.Endpoint); |
| | | 175 | | |
| | 12 | 176 | | if (!string.IsNullOrWhiteSpace(transferDestinationPath)) |
| | | 177 | | { |
| | 6 | 178 | | this.isViaSender = true; |
| | 6 | 179 | | this.TransferDestinationPath = transferDestinationPath; |
| | 6 | 180 | | this.ViaEntityPath = entityPath; |
| | | 181 | | } |
| | | 182 | | |
| | 12 | 183 | | MessagingEventSource.Log.MessageSenderCreateStop(serviceBusConnection.Endpoint.Authority, entityPath, this.C |
| | 12 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Gets a list of currently registered plugins for this sender. |
| | | 188 | | /// </summary> |
| | | 189 | | /// <seealso cref="RegisterPlugin"/> |
| | 0 | 190 | | public override IList<ServiceBusPlugin> RegisteredPlugins { get; } = new List<ServiceBusPlugin>(); |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// Gets the entity path of the MessageSender. |
| | | 194 | | /// In the case of a via-sender, this returns the path of the via entity. |
| | | 195 | | /// </summary> |
| | 4 | 196 | | public override string Path { get; } |
| | | 197 | | |
| | | 198 | | /// <summary> |
| | | 199 | | /// In the case of a via-sender, gets the final destination path of the messages; null otherwise. |
| | | 200 | | /// </summary> |
| | 4 | 201 | | public string TransferDestinationPath { get; } |
| | | 202 | | |
| | | 203 | | /// <summary> |
| | | 204 | | /// In the case of a via-sender, the message is sent to <see cref="TransferDestinationPath"/> via <see cref="Via |
| | | 205 | | /// </summary> |
| | 4 | 206 | | public string ViaEntityPath { get; } |
| | | 207 | | |
| | | 208 | | /// <summary> |
| | | 209 | | /// Duration after which individual operations will timeout. |
| | | 210 | | /// </summary> |
| | | 211 | | public override TimeSpan OperationTimeout |
| | | 212 | | { |
| | 0 | 213 | | get => this.ServiceBusConnection.OperationTimeout; |
| | 0 | 214 | | set => this.ServiceBusConnection.OperationTimeout = value; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | /// <summary> |
| | | 218 | | /// Connection object to the service bus namespace. |
| | | 219 | | /// </summary> |
| | 48 | 220 | | public override ServiceBusConnection ServiceBusConnection { get; } |
| | | 221 | | |
| | 0 | 222 | | internal MessagingEntityType? EntityType { get; } |
| | | 223 | | |
| | 0 | 224 | | internal string SendingLinkDestination { get; set; } |
| | | 225 | | |
| | 12 | 226 | | ICbsTokenProvider CbsTokenProvider { get; } |
| | | 227 | | |
| | 0 | 228 | | FaultTolerantAmqpObject<SendingAmqpLink> SendLinkManager { get; } |
| | | 229 | | |
| | 0 | 230 | | FaultTolerantAmqpObject<RequestResponseAmqpLink> RequestResponseLinkManager { get; } |
| | | 231 | | |
| | | 232 | | /// <summary> |
| | | 233 | | /// Sends a message to the entity as described by <see cref="Path"/>. |
| | | 234 | | /// </summary> |
| | | 235 | | public Task SendAsync(Message message) |
| | | 236 | | { |
| | 0 | 237 | | return this.SendAsync(new[] { message }); |
| | | 238 | | } |
| | | 239 | | |
| | | 240 | | /// <summary> |
| | | 241 | | /// Sends a list of messages to the entity as described by <see cref="Path"/>. |
| | | 242 | | /// When called on partitioned entities, messages meant for different partitions cannot be batched together. |
| | | 243 | | /// </summary> |
| | | 244 | | public async Task SendAsync(IList<Message> messageList) |
| | | 245 | | { |
| | 0 | 246 | | this.ThrowIfClosed(); |
| | | 247 | | |
| | 0 | 248 | | var count = MessageSender.ValidateMessages(messageList); |
| | 0 | 249 | | if (count <= 0) |
| | | 250 | | { |
| | 0 | 251 | | return; |
| | | 252 | | } |
| | | 253 | | |
| | 0 | 254 | | MessagingEventSource.Log.MessageSendStart(this.ClientId, count); |
| | | 255 | | |
| | 0 | 256 | | bool isDiagnosticSourceEnabled = ServiceBusDiagnosticSource.IsEnabled(); |
| | 0 | 257 | | Activity activity = isDiagnosticSourceEnabled ? this.diagnosticSource.SendStart(messageList) : null; |
| | 0 | 258 | | Task sendTask = null; |
| | | 259 | | |
| | | 260 | | try |
| | | 261 | | { |
| | 0 | 262 | | var processedMessages = await this.ProcessMessages(messageList).ConfigureAwait(false); |
| | | 263 | | |
| | 0 | 264 | | sendTask = this.RetryPolicy.RunOperation(() => this.OnSendAsync(processedMessages), this.OperationTimeou |
| | 0 | 265 | | await sendTask.ConfigureAwait(false); |
| | 0 | 266 | | } |
| | 0 | 267 | | catch (Exception exception) |
| | | 268 | | { |
| | 0 | 269 | | if (isDiagnosticSourceEnabled) |
| | | 270 | | { |
| | 0 | 271 | | this.diagnosticSource.ReportException(exception); |
| | | 272 | | } |
| | | 273 | | |
| | 0 | 274 | | MessagingEventSource.Log.MessageSendException(this.ClientId, exception); |
| | 0 | 275 | | throw; |
| | | 276 | | } |
| | | 277 | | finally |
| | | 278 | | { |
| | 0 | 279 | | this.diagnosticSource.SendStop(activity, messageList, sendTask?.Status); |
| | | 280 | | } |
| | | 281 | | |
| | 0 | 282 | | MessagingEventSource.Log.MessageSendStop(this.ClientId); |
| | 0 | 283 | | } |
| | | 284 | | |
| | | 285 | | /// <summary> |
| | | 286 | | /// Schedules a message to appear on Service Bus at a later time. |
| | | 287 | | /// </summary> |
| | | 288 | | /// <param name="message">The <see cref="Message"/> that needs to be scheduled.</param> |
| | | 289 | | /// <param name="scheduleEnqueueTimeUtc">The UTC time at which the message should be available for processing</p |
| | | 290 | | /// <returns>The sequence number of the message that was scheduled.</returns> |
| | | 291 | | public async Task<long> ScheduleMessageAsync(Message message, DateTimeOffset scheduleEnqueueTimeUtc) |
| | | 292 | | { |
| | 0 | 293 | | this.ThrowIfClosed(); |
| | 0 | 294 | | if (message == null) |
| | | 295 | | { |
| | 0 | 296 | | throw Fx.Exception.ArgumentNull(nameof(message)); |
| | | 297 | | } |
| | | 298 | | |
| | 0 | 299 | | if (scheduleEnqueueTimeUtc.CompareTo(DateTimeOffset.UtcNow) < 0) |
| | | 300 | | { |
| | 0 | 301 | | throw Fx.Exception.ArgumentOutOfRange( |
| | 0 | 302 | | nameof(scheduleEnqueueTimeUtc), |
| | 0 | 303 | | scheduleEnqueueTimeUtc.ToString(), |
| | 0 | 304 | | "Cannot schedule messages in the past"); |
| | | 305 | | } |
| | | 306 | | |
| | 0 | 307 | | if (this.isViaSender && Transaction.Current != null) |
| | | 308 | | { |
| | 0 | 309 | | throw new ServiceBusException(false, $"{nameof(ScheduleMessageAsync)} method is not supported in a Via-S |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | message.ScheduledEnqueueTimeUtc = scheduleEnqueueTimeUtc.UtcDateTime; |
| | 0 | 313 | | MessageSender.ValidateMessage(message); |
| | 0 | 314 | | MessagingEventSource.Log.ScheduleMessageStart(this.ClientId, scheduleEnqueueTimeUtc); |
| | 0 | 315 | | long result = 0; |
| | | 316 | | |
| | 0 | 317 | | bool isDiagnosticSourceEnabled = ServiceBusDiagnosticSource.IsEnabled(); |
| | 0 | 318 | | Activity activity = isDiagnosticSourceEnabled ? this.diagnosticSource.ScheduleStart(message, scheduleEnqueue |
| | 0 | 319 | | Task scheduleTask = null; |
| | | 320 | | |
| | | 321 | | try |
| | | 322 | | { |
| | 0 | 323 | | var processedMessage = await this.ProcessMessage(message).ConfigureAwait(false); |
| | | 324 | | |
| | 0 | 325 | | scheduleTask = this.RetryPolicy.RunOperation( |
| | 0 | 326 | | async () => |
| | 0 | 327 | | { |
| | 0 | 328 | | result = await this.OnScheduleMessageAsync(processedMessage).ConfigureAwait(false); |
| | 0 | 329 | | }, this.OperationTimeout); |
| | 0 | 330 | | await scheduleTask.ConfigureAwait(false); |
| | 0 | 331 | | } |
| | 0 | 332 | | catch (Exception exception) |
| | | 333 | | { |
| | 0 | 334 | | if (isDiagnosticSourceEnabled) |
| | | 335 | | { |
| | 0 | 336 | | this.diagnosticSource.ReportException(exception); |
| | | 337 | | } |
| | | 338 | | |
| | 0 | 339 | | MessagingEventSource.Log.ScheduleMessageException(this.ClientId, exception); |
| | 0 | 340 | | throw; |
| | | 341 | | } |
| | | 342 | | finally |
| | | 343 | | { |
| | 0 | 344 | | this.diagnosticSource.ScheduleStop(activity, message, scheduleEnqueueTimeUtc, scheduleTask?.Status, resu |
| | | 345 | | } |
| | | 346 | | |
| | 0 | 347 | | MessagingEventSource.Log.ScheduleMessageStop(this.ClientId); |
| | 0 | 348 | | return result; |
| | 0 | 349 | | } |
| | | 350 | | |
| | | 351 | | /// <summary> |
| | | 352 | | /// Cancels a message that was scheduled. |
| | | 353 | | /// </summary> |
| | | 354 | | /// <param name="sequenceNumber">The <see cref="Message.SystemPropertiesCollection.SequenceNumber"/> of the mess |
| | | 355 | | public async Task CancelScheduledMessageAsync(long sequenceNumber) |
| | | 356 | | { |
| | 0 | 357 | | this.ThrowIfClosed(); |
| | 0 | 358 | | if (Transaction.Current != null) |
| | | 359 | | { |
| | 0 | 360 | | throw new ServiceBusException(false, $"{nameof(CancelScheduledMessageAsync)} method is not supported wit |
| | | 361 | | } |
| | | 362 | | |
| | 0 | 363 | | MessagingEventSource.Log.CancelScheduledMessageStart(this.ClientId, sequenceNumber); |
| | | 364 | | |
| | 0 | 365 | | bool isDiagnosticSourceEnabled = ServiceBusDiagnosticSource.IsEnabled(); |
| | 0 | 366 | | Activity activity = isDiagnosticSourceEnabled ? this.diagnosticSource.CancelStart(sequenceNumber) : null; |
| | 0 | 367 | | Task cancelTask = null; |
| | | 368 | | |
| | | 369 | | try |
| | | 370 | | { |
| | 0 | 371 | | cancelTask = this.RetryPolicy.RunOperation(() => this.OnCancelScheduledMessageAsync(sequenceNumber), |
| | 0 | 372 | | this.OperationTimeout); |
| | 0 | 373 | | await cancelTask.ConfigureAwait(false); |
| | 0 | 374 | | } |
| | 0 | 375 | | catch (Exception exception) |
| | | 376 | | { |
| | 0 | 377 | | if (isDiagnosticSourceEnabled) |
| | | 378 | | { |
| | 0 | 379 | | this.diagnosticSource.ReportException(exception); |
| | | 380 | | } |
| | | 381 | | |
| | 0 | 382 | | MessagingEventSource.Log.CancelScheduledMessageException(this.ClientId, exception); |
| | 0 | 383 | | throw; |
| | | 384 | | } |
| | | 385 | | finally |
| | | 386 | | { |
| | 0 | 387 | | this.diagnosticSource.CancelStop(activity, sequenceNumber, cancelTask?.Status); |
| | | 388 | | } |
| | 0 | 389 | | MessagingEventSource.Log.CancelScheduledMessageStop(this.ClientId); |
| | 0 | 390 | | } |
| | | 391 | | |
| | | 392 | | /// <summary> |
| | | 393 | | /// Registers a <see cref="ServiceBusPlugin"/> to be used with this sender. |
| | | 394 | | /// </summary> |
| | | 395 | | /// <param name="serviceBusPlugin">The <see cref="ServiceBusPlugin"/> to register.</param> |
| | | 396 | | public override void RegisterPlugin(ServiceBusPlugin serviceBusPlugin) |
| | | 397 | | { |
| | 0 | 398 | | this.ThrowIfClosed(); |
| | 0 | 399 | | if (serviceBusPlugin == null) |
| | | 400 | | { |
| | 0 | 401 | | throw new ArgumentNullException(nameof(serviceBusPlugin), Resources.ArgumentNullOrWhiteSpace.FormatForUs |
| | | 402 | | } |
| | | 403 | | |
| | 0 | 404 | | if (this.RegisteredPlugins.Any(p => p.GetType() == serviceBusPlugin.GetType())) |
| | | 405 | | { |
| | 0 | 406 | | throw new ArgumentException(nameof(serviceBusPlugin), Resources.PluginAlreadyRegistered.FormatForUser(se |
| | | 407 | | } |
| | 0 | 408 | | this.RegisteredPlugins.Add(serviceBusPlugin); |
| | 0 | 409 | | } |
| | | 410 | | |
| | | 411 | | /// <summary> |
| | | 412 | | /// Unregisters a <see cref="ServiceBusPlugin"/>. |
| | | 413 | | /// </summary> |
| | | 414 | | /// <param name="serviceBusPluginName">The name <see cref="ServiceBusPlugin.Name"/> to be unregistered</param> |
| | | 415 | | public override void UnregisterPlugin(string serviceBusPluginName) |
| | | 416 | | { |
| | 0 | 417 | | this.ThrowIfClosed(); |
| | 0 | 418 | | if (string.IsNullOrWhiteSpace(serviceBusPluginName)) |
| | | 419 | | { |
| | 0 | 420 | | throw new ArgumentNullException(nameof(serviceBusPluginName), Resources.ArgumentNullOrWhiteSpace.FormatF |
| | | 421 | | } |
| | 0 | 422 | | if (this.RegisteredPlugins.Any(p => p.Name == serviceBusPluginName)) |
| | | 423 | | { |
| | 0 | 424 | | var plugin = this.RegisteredPlugins.First(p => p.Name == serviceBusPluginName); |
| | 0 | 425 | | this.RegisteredPlugins.Remove(plugin); |
| | | 426 | | } |
| | 0 | 427 | | } |
| | | 428 | | |
| | | 429 | | internal async Task<AmqpResponseMessage> ExecuteRequestResponseAsync(AmqpRequestMessage amqpRequestMessage) |
| | | 430 | | { |
| | 0 | 431 | | var amqpMessage = amqpRequestMessage.AmqpMessage; |
| | 0 | 432 | | var timeoutHelper = new TimeoutHelper(this.OperationTimeout, true); |
| | | 433 | | |
| | 0 | 434 | | ArraySegment<byte> transactionId = AmqpConstants.NullBinary; |
| | 0 | 435 | | var ambientTransaction = Transaction.Current; |
| | 0 | 436 | | if (ambientTransaction != null) |
| | | 437 | | { |
| | 0 | 438 | | transactionId = await AmqpTransactionManager.Instance.EnlistAsync(ambientTransaction, this.ServiceBusCon |
| | | 439 | | } |
| | | 440 | | |
| | 0 | 441 | | if (!this.RequestResponseLinkManager.TryGetOpenedObject(out var requestResponseAmqpLink)) |
| | | 442 | | { |
| | 0 | 443 | | requestResponseAmqpLink = await this.RequestResponseLinkManager.GetOrCreateAsync(timeoutHelper.Remaining |
| | | 444 | | } |
| | | 445 | | |
| | 0 | 446 | | var responseAmqpMessage = await Task.Factory.FromAsync( |
| | 0 | 447 | | (c, s) => requestResponseAmqpLink.BeginRequest(amqpMessage, transactionId, timeoutHelper.RemainingTime() |
| | 0 | 448 | | a => requestResponseAmqpLink.EndRequest(a), |
| | 0 | 449 | | this).ConfigureAwait(false); |
| | | 450 | | |
| | 0 | 451 | | return AmqpResponseMessage.CreateResponse(responseAmqpMessage); |
| | 0 | 452 | | } |
| | | 453 | | |
| | | 454 | | /// <summary>Closes the connection.</summary> |
| | | 455 | | protected override async Task OnClosingAsync() |
| | | 456 | | { |
| | 0 | 457 | | this.clientLinkManager.Close(); |
| | 0 | 458 | | await this.SendLinkManager.CloseAsync().ConfigureAwait(false); |
| | 0 | 459 | | await this.RequestResponseLinkManager.CloseAsync().ConfigureAwait(false); |
| | 0 | 460 | | } |
| | | 461 | | |
| | | 462 | | static int ValidateMessages(IList<Message> messageList) |
| | | 463 | | { |
| | 0 | 464 | | var count = 0; |
| | 0 | 465 | | if (messageList == null) |
| | | 466 | | { |
| | 0 | 467 | | throw Fx.Exception.ArgumentNull(nameof(messageList)); |
| | | 468 | | } |
| | | 469 | | |
| | 0 | 470 | | foreach (var message in messageList) |
| | | 471 | | { |
| | 0 | 472 | | count++; |
| | 0 | 473 | | ValidateMessage(message); |
| | | 474 | | } |
| | | 475 | | |
| | 0 | 476 | | return count; |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | static void ValidateMessage(Message message) |
| | | 480 | | { |
| | 0 | 481 | | if (message.SystemProperties.IsLockTokenSet) |
| | | 482 | | { |
| | 0 | 483 | | throw Fx.Exception.Argument(nameof(message), "Cannot send a message that was already received."); |
| | | 484 | | } |
| | 0 | 485 | | } |
| | | 486 | | |
| | | 487 | | static void CloseSession(SendingAmqpLink link) |
| | | 488 | | { |
| | | 489 | | // Note we close the session (which includes the link). |
| | 0 | 490 | | link.Session.SafeClose(); |
| | 0 | 491 | | } |
| | | 492 | | |
| | | 493 | | static void CloseRequestResponseSession(RequestResponseAmqpLink requestResponseAmqpLink) |
| | | 494 | | { |
| | 0 | 495 | | requestResponseAmqpLink.Session.SafeClose(); |
| | 0 | 496 | | } |
| | | 497 | | |
| | | 498 | | async Task<Message> ProcessMessage(Message message) |
| | | 499 | | { |
| | 0 | 500 | | var processedMessage = message; |
| | 0 | 501 | | foreach (var plugin in this.RegisteredPlugins) |
| | | 502 | | { |
| | | 503 | | try |
| | | 504 | | { |
| | 0 | 505 | | MessagingEventSource.Log.PluginCallStarted(plugin.Name, message.MessageId); |
| | 0 | 506 | | processedMessage = await plugin.BeforeMessageSend(message).ConfigureAwait(false); |
| | 0 | 507 | | MessagingEventSource.Log.PluginCallCompleted(plugin.Name, message.MessageId); |
| | 0 | 508 | | } |
| | 0 | 509 | | catch (Exception ex) |
| | | 510 | | { |
| | 0 | 511 | | MessagingEventSource.Log.PluginCallFailed(plugin.Name, message.MessageId, ex); |
| | 0 | 512 | | if (!plugin.ShouldContinueOnException) |
| | | 513 | | { |
| | 0 | 514 | | throw; |
| | | 515 | | } |
| | 0 | 516 | | } |
| | 0 | 517 | | } |
| | 0 | 518 | | return processedMessage; |
| | 0 | 519 | | } |
| | | 520 | | |
| | | 521 | | async Task<IList<Message>> ProcessMessages(IList<Message> messageList) |
| | | 522 | | { |
| | 0 | 523 | | if (this.RegisteredPlugins.Count < 1) |
| | | 524 | | { |
| | 0 | 525 | | return messageList; |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | var processedMessageList = new List<Message>(); |
| | 0 | 529 | | foreach (var message in messageList) |
| | | 530 | | { |
| | 0 | 531 | | var processedMessage = await this.ProcessMessage(message).ConfigureAwait(false); |
| | 0 | 532 | | processedMessageList.Add(processedMessage); |
| | | 533 | | } |
| | | 534 | | |
| | 0 | 535 | | return processedMessageList; |
| | 0 | 536 | | } |
| | | 537 | | |
| | | 538 | | async Task OnSendAsync(IList<Message> messageList) |
| | | 539 | | { |
| | 0 | 540 | | var timeoutHelper = new TimeoutHelper(this.OperationTimeout, true); |
| | 0 | 541 | | using (var amqpMessage = AmqpMessageConverter.BatchSBMessagesAsAmqpMessage(messageList)) |
| | | 542 | | { |
| | 0 | 543 | | SendingAmqpLink amqpLink = null; |
| | | 544 | | try |
| | | 545 | | { |
| | 0 | 546 | | ArraySegment<byte> transactionId = AmqpConstants.NullBinary; |
| | 0 | 547 | | var ambientTransaction = Transaction.Current; |
| | 0 | 548 | | if (ambientTransaction != null) |
| | | 549 | | { |
| | 0 | 550 | | transactionId = await AmqpTransactionManager.Instance.EnlistAsync(ambientTransaction, this.Servi |
| | | 551 | | } |
| | | 552 | | |
| | 0 | 553 | | if (!this.SendLinkManager.TryGetOpenedObject(out amqpLink)) |
| | | 554 | | { |
| | 0 | 555 | | amqpLink = await this.SendLinkManager.GetOrCreateAsync(timeoutHelper.RemainingTime()).ConfigureA |
| | | 556 | | } |
| | 0 | 557 | | if (amqpLink.Settings.MaxMessageSize.HasValue) |
| | | 558 | | { |
| | 0 | 559 | | var size = (ulong)amqpMessage.SerializedMessageSize; |
| | 0 | 560 | | if (size > amqpLink.Settings.MaxMessageSize.Value) |
| | | 561 | | { |
| | 0 | 562 | | throw new MessageSizeExceededException(Resources.AmqpMessageSizeExceeded.FormatForUser(amqpM |
| | | 563 | | } |
| | | 564 | | } |
| | | 565 | | |
| | 0 | 566 | | var outcome = await amqpLink.SendMessageAsync(amqpMessage, this.GetNextDeliveryTag(), transactionId, |
| | | 567 | | |
| | 0 | 568 | | if (outcome.DescriptorCode != Accepted.Code) |
| | | 569 | | { |
| | 0 | 570 | | var rejected = (Rejected)outcome; |
| | 0 | 571 | | throw Fx.Exception.AsError(rejected.Error.ToMessagingContractException()); |
| | | 572 | | } |
| | 0 | 573 | | } |
| | | 574 | | catch (Exception exception) |
| | | 575 | | { |
| | 0 | 576 | | throw AmqpExceptionHelper.GetClientException(exception, amqpLink?.GetTrackingId(), null, amqpLink?.S |
| | | 577 | | } |
| | 0 | 578 | | } |
| | 0 | 579 | | } |
| | | 580 | | |
| | | 581 | | async Task<long> OnScheduleMessageAsync(Message message) |
| | | 582 | | { |
| | 0 | 583 | | using (var amqpMessage = AmqpMessageConverter.SBMessageToAmqpMessage(message)) |
| | | 584 | | { |
| | 0 | 585 | | var request = AmqpRequestMessage.CreateRequest( |
| | 0 | 586 | | ManagementConstants.Operations.ScheduleMessageOperation, |
| | 0 | 587 | | this.OperationTimeout, |
| | 0 | 588 | | null); |
| | | 589 | | |
| | 0 | 590 | | SendingAmqpLink sendLink = null; |
| | | 591 | | |
| | | 592 | | try |
| | | 593 | | { |
| | 0 | 594 | | if (this.SendLinkManager.TryGetOpenedObject(out sendLink)) |
| | | 595 | | { |
| | 0 | 596 | | request.AmqpMessage.ApplicationProperties.Map[ManagementConstants.Request.AssociatedLinkName] = |
| | | 597 | | } |
| | | 598 | | |
| | 0 | 599 | | ArraySegment<byte>[] payload = amqpMessage.GetPayload(); |
| | 0 | 600 | | var buffer = new BufferListStream(payload); |
| | 0 | 601 | | ArraySegment<byte> value = buffer.ReadBytes((int)buffer.Length); |
| | | 602 | | |
| | 0 | 603 | | var entry = new AmqpMap(); |
| | | 604 | | { |
| | 0 | 605 | | entry[ManagementConstants.Properties.Message] = value; |
| | 0 | 606 | | entry[ManagementConstants.Properties.MessageId] = message.MessageId; |
| | | 607 | | |
| | 0 | 608 | | if (!string.IsNullOrWhiteSpace(message.SessionId)) |
| | | 609 | | { |
| | 0 | 610 | | entry[ManagementConstants.Properties.SessionId] = message.SessionId; |
| | | 611 | | } |
| | | 612 | | |
| | 0 | 613 | | if (!string.IsNullOrWhiteSpace(message.PartitionKey)) |
| | | 614 | | { |
| | 0 | 615 | | entry[ManagementConstants.Properties.PartitionKey] = message.PartitionKey; |
| | | 616 | | } |
| | | 617 | | |
| | 0 | 618 | | if (!string.IsNullOrWhiteSpace(message.ViaPartitionKey)) |
| | | 619 | | { |
| | 0 | 620 | | entry[ManagementConstants.Properties.ViaPartitionKey] = message.ViaPartitionKey; |
| | | 621 | | } |
| | | 622 | | } |
| | | 623 | | |
| | 0 | 624 | | request.Map[ManagementConstants.Properties.Messages] = new List<AmqpMap> { entry }; |
| | | 625 | | |
| | 0 | 626 | | var response = await this.ExecuteRequestResponseAsync(request).ConfigureAwait(false); |
| | 0 | 627 | | if (response.StatusCode == AmqpResponseStatusCode.OK) |
| | | 628 | | { |
| | 0 | 629 | | var sequenceNumbers = response.GetValue<long[]>(ManagementConstants.Properties.SequenceNumbers); |
| | 0 | 630 | | if (sequenceNumbers == null || sequenceNumbers.Length < 1) |
| | | 631 | | { |
| | 0 | 632 | | throw new ServiceBusException(true, "Could not schedule message successfully."); |
| | | 633 | | } |
| | | 634 | | |
| | 0 | 635 | | return sequenceNumbers[0]; |
| | | 636 | | |
| | | 637 | | } |
| | | 638 | | else |
| | | 639 | | { |
| | 0 | 640 | | throw response.ToMessagingContractException(); |
| | | 641 | | } |
| | | 642 | | } |
| | | 643 | | catch (Exception exception) |
| | | 644 | | { |
| | 0 | 645 | | throw AmqpExceptionHelper.GetClientException(exception, sendLink?.GetTrackingId(), null, sendLink?.S |
| | | 646 | | } |
| | | 647 | | } |
| | 0 | 648 | | } |
| | | 649 | | |
| | | 650 | | async Task OnCancelScheduledMessageAsync(long sequenceNumber) |
| | | 651 | | { |
| | 0 | 652 | | var request = |
| | 0 | 653 | | AmqpRequestMessage.CreateRequest( |
| | 0 | 654 | | ManagementConstants.Operations.CancelScheduledMessageOperation, |
| | 0 | 655 | | this.OperationTimeout, |
| | 0 | 656 | | null); |
| | | 657 | | |
| | 0 | 658 | | SendingAmqpLink sendLink = null; |
| | | 659 | | |
| | | 660 | | try |
| | | 661 | | { |
| | 0 | 662 | | if (this.SendLinkManager.TryGetOpenedObject(out sendLink)) |
| | | 663 | | { |
| | 0 | 664 | | request.AmqpMessage.ApplicationProperties.Map[ManagementConstants.Request.AssociatedLinkName] = send |
| | | 665 | | } |
| | | 666 | | |
| | 0 | 667 | | request.Map[ManagementConstants.Properties.SequenceNumbers] = new[] { sequenceNumber }; |
| | | 668 | | |
| | 0 | 669 | | var response = await this.ExecuteRequestResponseAsync(request).ConfigureAwait(false); |
| | | 670 | | |
| | 0 | 671 | | if (response.StatusCode != AmqpResponseStatusCode.OK) |
| | | 672 | | { |
| | 0 | 673 | | throw response.ToMessagingContractException(); |
| | | 674 | | } |
| | 0 | 675 | | } |
| | | 676 | | catch (Exception exception) |
| | | 677 | | { |
| | 0 | 678 | | throw AmqpExceptionHelper.GetClientException(exception, sendLink?.GetTrackingId(), null, sendLink?.Sessi |
| | | 679 | | } |
| | 0 | 680 | | } |
| | | 681 | | |
| | | 682 | | async Task<SendingAmqpLink> CreateLinkAsync(TimeSpan timeout) |
| | | 683 | | { |
| | 0 | 684 | | MessagingEventSource.Log.AmqpSendLinkCreateStart(this.ClientId, this.EntityType, this.SendingLinkDestination |
| | | 685 | | |
| | 0 | 686 | | var amqpLinkSettings = new AmqpLinkSettings |
| | 0 | 687 | | { |
| | 0 | 688 | | Role = false, |
| | 0 | 689 | | InitialDeliveryCount = 0, |
| | 0 | 690 | | Target = new Target { Address = this.SendingLinkDestination }, |
| | 0 | 691 | | Source = new Source { Address = this.ClientId }, |
| | 0 | 692 | | }; |
| | 0 | 693 | | if (this.EntityType != null) |
| | | 694 | | { |
| | 0 | 695 | | amqpLinkSettings.AddProperty(AmqpClientConstants.EntityTypeName, (int)this.EntityType); |
| | | 696 | | } |
| | | 697 | | |
| | 0 | 698 | | var endpointUri = new Uri(this.ServiceBusConnection.Endpoint, this.SendingLinkDestination); |
| | | 699 | | |
| | | 700 | | string[] audience; |
| | 0 | 701 | | if (this.isViaSender) |
| | | 702 | | { |
| | 0 | 703 | | var transferDestinationEndpointUri = new Uri(this.ServiceBusConnection.Endpoint, this.TransferDestinatio |
| | 0 | 704 | | audience = new string[] { endpointUri.AbsoluteUri, transferDestinationEndpointUri.AbsoluteUri }; |
| | 0 | 705 | | amqpLinkSettings.AddProperty(AmqpClientConstants.TransferDestinationAddress, this.TransferDestinationPat |
| | | 706 | | } |
| | | 707 | | else |
| | | 708 | | { |
| | 0 | 709 | | audience = new string[] { endpointUri.AbsoluteUri }; |
| | | 710 | | } |
| | | 711 | | |
| | 0 | 712 | | string[] claims = {ClaimConstants.Send}; |
| | 0 | 713 | | var amqpSendReceiveLinkCreator = new AmqpSendReceiveLinkCreator(this.SendingLinkDestination, this.ServiceBus |
| | 0 | 714 | | Tuple<AmqpObject, DateTime> linkDetails = await amqpSendReceiveLinkCreator.CreateAndOpenAmqpLinkAsync().Conf |
| | | 715 | | |
| | 0 | 716 | | var sendingAmqpLink = (SendingAmqpLink) linkDetails.Item1; |
| | 0 | 717 | | var activeSendReceiveClientLink = new ActiveSendReceiveClientLink( |
| | 0 | 718 | | sendingAmqpLink, |
| | 0 | 719 | | endpointUri, |
| | 0 | 720 | | audience, |
| | 0 | 721 | | claims, |
| | 0 | 722 | | linkDetails.Item2); |
| | | 723 | | |
| | 0 | 724 | | this.clientLinkManager.SetActiveSendReceiveLink(activeSendReceiveClientLink); |
| | | 725 | | |
| | 0 | 726 | | MessagingEventSource.Log.AmqpSendLinkCreateStop(this.ClientId); |
| | 0 | 727 | | return sendingAmqpLink; |
| | 0 | 728 | | } |
| | | 729 | | |
| | | 730 | | async Task<RequestResponseAmqpLink> CreateRequestResponseLinkAsync(TimeSpan timeout) |
| | | 731 | | { |
| | 0 | 732 | | var entityPath = this.SendingLinkDestination + '/' + AmqpClientConstants.ManagementAddress; |
| | 0 | 733 | | var amqpLinkSettings = new AmqpLinkSettings(); |
| | 0 | 734 | | amqpLinkSettings.AddProperty(AmqpClientConstants.EntityTypeName, AmqpClientConstants.EntityTypeManagement); |
| | | 735 | | |
| | 0 | 736 | | var endpointUri = new Uri(this.ServiceBusConnection.Endpoint, entityPath); |
| | | 737 | | |
| | | 738 | | string[] audience; |
| | 0 | 739 | | if (this.isViaSender) |
| | | 740 | | { |
| | 0 | 741 | | var transferDestinationEndpointUri = new Uri(this.ServiceBusConnection.Endpoint, this.TransferDestinatio |
| | 0 | 742 | | audience = new string[] { endpointUri.AbsoluteUri, transferDestinationEndpointUri.AbsoluteUri }; |
| | 0 | 743 | | amqpLinkSettings.AddProperty(AmqpClientConstants.TransferDestinationAddress, this.TransferDestinationPat |
| | | 744 | | } |
| | | 745 | | else |
| | | 746 | | { |
| | 0 | 747 | | audience = new string[] { endpointUri.AbsoluteUri }; |
| | | 748 | | } |
| | | 749 | | |
| | 0 | 750 | | string[] claims = { ClaimConstants.Manage, ClaimConstants.Send }; |
| | 0 | 751 | | var amqpRequestResponseLinkCreator = new AmqpRequestResponseLinkCreator( |
| | 0 | 752 | | entityPath, |
| | 0 | 753 | | this.ServiceBusConnection, |
| | 0 | 754 | | endpointUri, |
| | 0 | 755 | | audience, |
| | 0 | 756 | | claims, |
| | 0 | 757 | | this.CbsTokenProvider, |
| | 0 | 758 | | amqpLinkSettings, |
| | 0 | 759 | | this.ClientId); |
| | | 760 | | |
| | 0 | 761 | | Tuple<AmqpObject, DateTime> linkDetails = |
| | 0 | 762 | | await amqpRequestResponseLinkCreator.CreateAndOpenAmqpLinkAsync().ConfigureAwait(false); |
| | | 763 | | |
| | 0 | 764 | | var requestResponseAmqpLink = (RequestResponseAmqpLink) linkDetails.Item1; |
| | 0 | 765 | | var activeRequestResponseClientLink = new ActiveRequestResponseLink( |
| | 0 | 766 | | requestResponseAmqpLink, |
| | 0 | 767 | | endpointUri, |
| | 0 | 768 | | audience, |
| | 0 | 769 | | claims, |
| | 0 | 770 | | linkDetails.Item2); |
| | 0 | 771 | | this.clientLinkManager.SetActiveRequestResponseLink(activeRequestResponseClientLink); |
| | | 772 | | |
| | 0 | 773 | | return requestResponseAmqpLink; |
| | 0 | 774 | | } |
| | | 775 | | |
| | | 776 | | ArraySegment<byte> GetNextDeliveryTag() |
| | | 777 | | { |
| | 0 | 778 | | var deliveryId = Interlocked.Increment(ref this.deliveryCount); |
| | 0 | 779 | | return new ArraySegment<byte>(BitConverter.GetBytes(deliveryId)); |
| | | 780 | | } |
| | | 781 | | } |
| | | 782 | | } |