| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Threading.Tasks; |
| | 6 | | using Azure.Core; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.ServiceBus.Plugins |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// This class provides methods that can be overridden to manipulate messages for custom plugin functionality. |
| | 12 | | /// </summary> |
| | 13 | | internal abstract class ServiceBusPlugin |
| | 14 | | { |
| | 15 | | /// <summary> |
| | 16 | | /// This operation is called before a message is sent and can be |
| | 17 | | /// overridden to alter the body and the properties of an outgoing message. |
| | 18 | | /// </summary> |
| | 19 | | /// <param name="message">The <see cref="ServiceBusMessage"/> to be modified by the plugin.</param> |
| | 20 | | public virtual ValueTask BeforeMessageSendAsync(ServiceBusMessage message) => |
| 0 | 21 | | default; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// This operation is called after a message is received, but before it is returned to the <see cref="ServiceBus |
| | 25 | | /// It can be overridden to alter the body and the properties of an |
| | 26 | | /// incoming message. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="message">The <see cref="ServiceBusReceivedMessage"/> to be modified by the plugin.</param> |
| | 29 | | public virtual ValueTask AfterMessageReceiveAsync(ServiceBusReceivedMessage message) => |
| 0 | 30 | | default; |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Set the <see cref="ServiceBusReceivedMessage.Body"/>. |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="message">The message to modify.</param> |
| | 36 | | /// <param name="body">The body to set on the message. This will overwrite any existing body.</param> |
| | 37 | | #pragma warning disable CA1822 // Mark members as static |
| | 38 | | protected void SetBody(ServiceBusReceivedMessage message, BinaryData body) |
| | 39 | | { |
| 2 | 40 | | message.SentMessage.Body = body; |
| 2 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Set a key/value pair on the <see cref="ServiceBusReceivedMessage.Properties"/>. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="message">The message to modify.</param> |
| | 47 | | /// <param name="key">The key to add or update the value of.</param> |
| | 48 | | /// <param name="value">The value to set for the associated key.</param> |
| | 49 | | protected void SetUserProperty(ServiceBusReceivedMessage message, string key, object value) |
| | 50 | | { |
| 6 | 51 | | message.SentMessage.Properties[key] = value; |
| 6 | 52 | | } |
| | 53 | |
|
| | 54 | | /// <summary> |
| | 55 | | /// Sets the |
| | 56 | | /// <see cref="ServiceBusReceivedMessage.ContentType"/>. |
| | 57 | | /// </summary> |
| | 58 | | /// <param name="message">The message to modify.</param> |
| | 59 | | /// <param name="contentType">The content type to set on |
| | 60 | | /// the message.</param> |
| | 61 | |
|
| | 62 | | protected void SetContentType(ServiceBusReceivedMessage message, string contentType) |
| | 63 | | { |
| 2 | 64 | | message.SentMessage.ContentType = contentType; |
| 2 | 65 | | } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Sets the <see cref="ServiceBusReceivedMessage.CorrelationId"/>. |
| | 69 | | /// </summary> |
| | 70 | | /// <param name="message">The message to modify.</param> |
| | 71 | | /// <param name="correlationId">The correlationId to set |
| | 72 | | /// on the message.</param> |
| | 73 | |
|
| | 74 | | protected void SetCorrelationId(ServiceBusReceivedMessage message, string correlationId) |
| | 75 | | { |
| 2 | 76 | | message.SentMessage.CorrelationId = correlationId; |
| 2 | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Sets the <see cref="ServiceBusReceivedMessage.Label"/>. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="message">The message to modify.</param> |
| | 83 | | /// <param name="label">The label to set on the message.</param> |
| | 84 | |
|
| | 85 | | protected void SetLabel(ServiceBusReceivedMessage message, string label) |
| | 86 | | { |
| 2 | 87 | | message.SentMessage.Label = label; |
| 2 | 88 | | } |
| | 89 | |
|
| | 90 | | /// <summary> |
| | 91 | | /// Sets the <see cref="ServiceBusReceivedMessage.MessageId"/>. |
| | 92 | | /// </summary> |
| | 93 | | /// <param name="message">The message to modify.</param> |
| | 94 | | /// <param name="messageId">The message ID to set on the message.</param> |
| | 95 | |
|
| | 96 | | protected void SetMessageId(ServiceBusReceivedMessage message, string messageId) |
| | 97 | | { |
| 2 | 98 | | message.SentMessage.MessageId = messageId; |
| 2 | 99 | | } |
| | 100 | |
|
| | 101 | | /// <summary> |
| | 102 | | /// Sets the <see cref="ServiceBusReceivedMessage.PartitionKey"/>. |
| | 103 | | /// </summary> |
| | 104 | | /// <param name="message">The message to modify.</param> |
| | 105 | | /// <param name="partitionKey">The partition key to set on the message.</param> |
| | 106 | |
|
| | 107 | | protected void SetPartitionKey(ServiceBusReceivedMessage message, string partitionKey) |
| | 108 | | { |
| 2 | 109 | | message.SentMessage.PartitionKey = partitionKey; |
| 2 | 110 | | } |
| | 111 | |
|
| | 112 | | /// <summary> |
| | 113 | | /// Sets the <see cref="ServiceBusReceivedMessage.ReplyTo"/>. |
| | 114 | | /// </summary> |
| | 115 | | /// <param name="message">The message to modify.</param> |
| | 116 | | /// <param name="replyTo">The reply to value to set on the message.</param> |
| | 117 | |
|
| | 118 | | protected void SetReplyTo(ServiceBusReceivedMessage message, string replyTo) |
| | 119 | | { |
| 2 | 120 | | message.SentMessage.ReplyTo = replyTo; |
| 2 | 121 | | } |
| | 122 | |
|
| | 123 | | /// <summary> |
| | 124 | | /// Sets the <see cref="ServiceBusReceivedMessage.ReplyToSessionId"/>. |
| | 125 | | /// </summary> |
| | 126 | | /// <param name="message">The message to modify.</param> |
| | 127 | | /// <param name="replyToSessionId">The reply to session ID value to set on the message.</param> |
| | 128 | |
|
| | 129 | | protected void SetReplyToSessionId(ServiceBusReceivedMessage message, string replyToSessionId) |
| | 130 | | { |
| 2 | 131 | | message.SentMessage.ReplyToSessionId = replyToSessionId; |
| 2 | 132 | | } |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// Sets the <see cref="ServiceBusReceivedMessage.SessionId"/>. |
| | 136 | | /// </summary> |
| | 137 | | /// <param name="message">The message to modify.</param> |
| | 138 | | /// <param name="sessionId">The session ID to set on the message.</param> |
| | 139 | | protected void SetSessionId(ServiceBusReceivedMessage message, string sessionId) |
| | 140 | | { |
| 2 | 141 | | message.SentMessage.SessionId = sessionId; |
| 2 | 142 | | } |
| | 143 | |
|
| | 144 | | /// <summary> |
| | 145 | | /// Sets the <see cref="ServiceBusReceivedMessage.ScheduledEnqueueTime"/>. |
| | 146 | | /// </summary> |
| | 147 | | /// <param name="message">The message to modify.</param> |
| | 148 | | /// <param name="scheduledEnqueueTime">The scheduled enqueue time to set on the message.</param> |
| | 149 | | protected void SetScheduledEnqueueTime(ServiceBusReceivedMessage message, DateTimeOffset scheduledEnqueueTime) |
| | 150 | | { |
| 2 | 151 | | message.SentMessage.ScheduledEnqueueTime = scheduledEnqueueTime; |
| 2 | 152 | | } |
| | 153 | |
|
| | 154 | | /// <summary> |
| | 155 | | /// Sets the <see cref="ServiceBusReceivedMessage.TimeToLive"/>. |
| | 156 | | /// </summary> |
| | 157 | | /// <param name="message">The message to modify.</param> |
| | 158 | | /// <param name="timeToLive">The time to live to set on the message.</param> |
| | 159 | |
|
| | 160 | | protected void SetTimeToLive(ServiceBusReceivedMessage message, TimeSpan timeToLive) |
| | 161 | | { |
| 2 | 162 | | message.SentMessage.TimeToLive = timeToLive; |
| 2 | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// Sets the <see cref="ServiceBusReceivedMessage.To"/>. |
| | 167 | | /// </summary> |
| | 168 | | /// <param name="message">The message to modify.</param> |
| | 169 | | /// <param name="to">The to value to set on the message.</param> |
| | 170 | | protected void SetTo(ServiceBusReceivedMessage message, string to) |
| | 171 | | { |
| 2 | 172 | | message.SentMessage.To = to; |
| 2 | 173 | | } |
| | 174 | |
|
| | 175 | | /// <summary> |
| | 176 | | /// Sets the <see cref="ServiceBusReceivedMessage.ViaPartitionKey"/>. |
| | 177 | | /// </summary> |
| | 178 | | /// <param name="message">The message to modify.</param> |
| | 179 | | /// <param name="viaPartitionKey">The via partition key to set on the message.</param> |
| | 180 | | protected void SetViaPartitionKey(ServiceBusReceivedMessage message, string viaPartitionKey) |
| | 181 | | { |
| 0 | 182 | | message.SentMessage.ViaPartitionKey = viaPartitionKey; |
| 0 | 183 | | } |
| | 184 | | #pragma warning restore CA1822 // Mark members as static |
| | 185 | | } |
| | 186 | | } |