| | 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.Text; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using System.Transactions; |
| | 9 | | using Microsoft.Azure.Amqp; |
| | 10 | |
|
| | 11 | | namespace Azure.Messaging.ServiceBus.Amqp |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Utility methods related to the management link |
| | 15 | | /// </summary> |
| | 16 | | internal static class ManagementUtilities |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="connectionScope"></param> |
| | 22 | | /// <param name="managementLink"></param> |
| | 23 | | /// <param name="amqpRequestMessage"></param> |
| | 24 | | /// <param name="timeout"></param> |
| | 25 | | /// <returns></returns> |
| | 26 | | internal static async Task<AmqpResponseMessage> ExecuteRequestResponseAsync( |
| | 27 | | AmqpConnectionScope connectionScope, |
| | 28 | | FaultTolerantAmqpObject<RequestResponseAmqpLink> managementLink, |
| | 29 | | AmqpRequestMessage amqpRequestMessage, |
| | 30 | | TimeSpan timeout) |
| | 31 | | { |
| 0 | 32 | | AmqpMessage amqpMessage = amqpRequestMessage.AmqpMessage; |
| | 33 | |
|
| 0 | 34 | | ArraySegment<byte> transactionId = AmqpConstants.NullBinary; |
| 0 | 35 | | var ambientTransaction = Transaction.Current; |
| 0 | 36 | | if (ambientTransaction != null) |
| | 37 | | { |
| 0 | 38 | | transactionId = await AmqpTransactionManager.Instance.EnlistAsync( |
| 0 | 39 | | ambientTransaction, |
| 0 | 40 | | connectionScope, |
| 0 | 41 | | timeout) |
| 0 | 42 | | .ConfigureAwait(false); |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | if (!managementLink.TryGetOpenedObject(out var requestResponseAmqpLink)) |
| | 46 | | { |
| | 47 | | // MessagingEventSource.Log.CreatingNewLink(this.ClientId, this.isSessionReceiver, this.SessionIdInterna |
| 0 | 48 | | requestResponseAmqpLink = await managementLink.GetOrCreateAsync(timeout).ConfigureAwait(false); |
| | 49 | | } |
| | 50 | |
|
| 0 | 51 | | var responseAmqpMessage = await Task.Factory.FromAsync( |
| 0 | 52 | | (c, s) => requestResponseAmqpLink.BeginRequest(amqpMessage, transactionId, timeout, c, s), |
| 0 | 53 | | (a) => requestResponseAmqpLink.EndRequest(a), |
| 0 | 54 | | null).ConfigureAwait(false); |
| | 55 | |
|
| 0 | 56 | | return AmqpResponseMessage.CreateResponse(responseAmqpMessage); |
| 0 | 57 | | } |
| | 58 | | } |
| | 59 | | } |