| | 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.Globalization; |
| | 7 | | using Azure.Core; |
| | 8 | |
|
| | 9 | | namespace Azure.Messaging.ServiceBus |
| | 10 | | { |
| | 11 | | /// <summary> |
| | 12 | | /// The message object used to communicate and transfer data with Service Bus. |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks> |
| | 15 | | /// The message structure is discussed in detail in the <a href="https://docs.microsoft.com/azure/service-bus-messag |
| | 16 | | /// </remarks> |
| | 17 | | public class ServiceBusMessage |
| | 18 | | { |
| | 19 | | private string _messageId; |
| | 20 | | private string _sessionId; |
| | 21 | | private string _replyToSessionId; |
| | 22 | | private string _partitionKey; |
| | 23 | | private string _viaPartitionKey; |
| | 24 | | private TimeSpan _timeToLive; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Creates a new message. |
| | 28 | | /// </summary> |
| | 29 | | public ServiceBusMessage() : |
| 84 | 30 | | this(default(ReadOnlyMemory<byte>)) |
| | 31 | | { |
| 84 | 32 | | } |
| | 33 | |
|
| | 34 | | /// <summary> |
| | 35 | | /// Creates a new message from the specified string, using UTF-8 encoding. |
| | 36 | | /// </summary> |
| | 37 | | /// <param name="body">The payload of the message as a string.</param> |
| 2 | 38 | | public ServiceBusMessage(string body) |
| | 39 | | { |
| 2 | 40 | | Body = new BinaryData(body); |
| 2 | 41 | | Properties = new Dictionary<string, object>(); |
| 2 | 42 | | } |
| | 43 | |
|
| | 44 | | /// <summary> |
| | 45 | | /// Creates a new message from the specified payload. |
| | 46 | | /// </summary> |
| | 47 | | /// <param name="body">The payload of the message in bytes.</param> |
| | 48 | | public ServiceBusMessage(ReadOnlyMemory<byte> body) : |
| 198 | 49 | | this(BinaryData.FromMemory(body)) |
| | 50 | | { |
| 198 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Creates a new message from the specified <see cref="BinaryData"/> instance. |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="body">The payload of the message.</param> |
| 202 | 57 | | public ServiceBusMessage(BinaryData body) |
| | 58 | | { |
| 202 | 59 | | Body = body; |
| 202 | 60 | | Properties = new Dictionary<string, object>(); |
| 202 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Creates a new message from the specified received message by copying the properties. |
| | 65 | | /// </summary> |
| | 66 | | /// <param name="receivedMessage">The received message to copy the data from.</param> |
| 0 | 67 | | public ServiceBusMessage(ServiceBusReceivedMessage receivedMessage) |
| | 68 | | { |
| 0 | 69 | | Argument.AssertNotNull(receivedMessage, nameof(receivedMessage)); |
| | 70 | |
|
| 0 | 71 | | Body = receivedMessage.SentMessage.Body; |
| 0 | 72 | | ContentType = receivedMessage.ContentType; |
| 0 | 73 | | CorrelationId = receivedMessage.CorrelationId; |
| 0 | 74 | | Label = receivedMessage.Label; |
| 0 | 75 | | MessageId = receivedMessage.MessageId; |
| 0 | 76 | | PartitionKey = receivedMessage.PartitionKey; |
| 0 | 77 | | Properties = new Dictionary<string, object>(receivedMessage.SentMessage.Properties); |
| 0 | 78 | | ReplyTo = receivedMessage.ReplyTo; |
| 0 | 79 | | ReplyToSessionId = receivedMessage.ReplyToSessionId; |
| 0 | 80 | | SessionId = receivedMessage.SessionId; |
| 0 | 81 | | ScheduledEnqueueTime = receivedMessage.ScheduledEnqueueTime; |
| 0 | 82 | | TimeToLive = receivedMessage.TimeToLive; |
| 0 | 83 | | To = receivedMessage.To; |
| 0 | 84 | | ViaPartitionKey = receivedMessage.ViaPartitionKey; |
| 0 | 85 | | } |
| | 86 | |
|
| | 87 | | /// <summary> |
| | 88 | | /// Gets or sets the body of the message. |
| | 89 | | /// </summary> |
| | 90 | | /// <remarks> |
| | 91 | | /// The easiest way to create a new message from a string is the following: |
| | 92 | | /// <code> |
| | 93 | | /// message.Body = System.Text.Encoding.UTF8.GetBytes("Message1"); |
| | 94 | | /// </code> |
| | 95 | | /// </remarks> |
| 306 | 96 | | public BinaryData Body { get; set; } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Gets or sets the MessageId to identify the message. |
| | 100 | | /// </summary> |
| | 101 | | /// <remarks> |
| | 102 | | /// The message identifier is an application-defined value that uniquely identifies the |
| | 103 | | /// message and its payload. The identifier is a free-form string and can reflect a GUID |
| | 104 | | /// or an identifier derived from the application context. If enabled, the |
| | 105 | | /// <a href="https://docs.microsoft.com/azure/service-bus-messaging/duplicate-detection">duplicate detection< |
| | 106 | | /// feature identifies and removes second and further submissions of messages with the |
| | 107 | | /// same MessageId. |
| | 108 | | /// </remarks> |
| | 109 | | public string MessageId |
| | 110 | | { |
| 150 | 111 | | get => _messageId; |
| | 112 | |
|
| | 113 | | set |
| | 114 | | { |
| 34 | 115 | | ValidateMessageId(value); |
| 30 | 116 | | _messageId = value; |
| 30 | 117 | | } |
| | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary>Gets or sets a partition key for sending a message to a partitioned entity.</summary> |
| | 121 | | /// <value>The partition key. Maximum length is 128 characters.</value> |
| | 122 | | /// <remarks> |
| | 123 | | /// For <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-partitioning">partitioned |
| | 124 | | /// setting this value enables assigning related messages to the same internal partition, so that submission |
| | 125 | | /// order is correctly recorded. The partition is chosen by a hash function over this value and cannot be cho |
| | 126 | | /// directly. For session-aware entities, the <see cref="SessionId"/> property overrides this value. |
| | 127 | | /// </remarks> |
| | 128 | | public string PartitionKey |
| | 129 | | { |
| 86 | 130 | | get => _partitionKey; |
| | 131 | |
|
| | 132 | | set |
| | 133 | | { |
| 8 | 134 | | ValidatePartitionKey(value); |
| 8 | 135 | | _partitionKey = value; |
| 8 | 136 | | } |
| | 137 | | } |
| | 138 | |
|
| | 139 | | /// <summary>Gets or sets a partition key for sending a message into an entity via a partitioned transfer queue. |
| | 140 | | /// <value>The partition key. Maximum length is 128 characters. </value> |
| | 141 | | /// <remarks> |
| | 142 | | /// If a message is sent via a transfer queue in the scope of a transaction, this value selects the |
| | 143 | | /// transfer queue partition: This is functionally equivalent to <see cref="PartitionKey"/> and ensures that |
| | 144 | | /// messages are kept together and in order as they are transferred. |
| | 145 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-transactions#transfers-an |
| | 146 | | /// </remarks> |
| | 147 | | public string ViaPartitionKey |
| | 148 | | { |
| 82 | 149 | | get => _viaPartitionKey; |
| | 150 | |
|
| | 151 | | set |
| | 152 | | { |
| 6 | 153 | | ValidatePartitionKey(value); |
| 6 | 154 | | _viaPartitionKey = value; |
| 6 | 155 | | } |
| | 156 | | } |
| | 157 | |
|
| | 158 | | /// <summary>Gets or sets the session identifier for a session-aware entity.</summary> |
| | 159 | | /// <value>The session identifier. Maximum length is 128 characters.</value> |
| | 160 | | /// <remarks> |
| | 161 | | /// For session-aware entities, this application-defined value specifies the session |
| | 162 | | /// affiliation of the message. Messages with the same session identifier are subject |
| | 163 | | /// to summary locking and enable exact in-order processing and demultiplexing. |
| | 164 | | /// For session-unaware entities, this value is ignored. |
| | 165 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/message-sessions">Message Sessions</a |
| | 166 | | /// </remarks> |
| | 167 | | public string SessionId |
| | 168 | | { |
| 126 | 169 | | get => _sessionId; |
| | 170 | |
|
| | 171 | | set |
| | 172 | | { |
| 8 | 173 | | ValidateSessionId(value); |
| 8 | 174 | | _sessionId = value; |
| 8 | 175 | | } |
| | 176 | | } |
| | 177 | |
|
| | 178 | | /// <summary>Gets or sets a session identifier augmenting the <see cref="ReplyTo"/> address.</summary> |
| | 179 | | /// <value>Session identifier. Maximum length is 128 characters.</value> |
| | 180 | | /// <remarks> |
| | 181 | | /// This value augments the ReplyTo information and specifies which SessionId should be set |
| | 182 | | /// for the reply when sent to the reply entity. See <a href="https://docs.microsoft.com/azure/service-bus-me |
| | 183 | | /// </remarks> |
| | 184 | | public string ReplyToSessionId |
| | 185 | | { |
| 82 | 186 | | get => _replyToSessionId; |
| | 187 | |
|
| | 188 | | set |
| | 189 | | { |
| 8 | 190 | | ValidateSessionId(value); |
| 8 | 191 | | _replyToSessionId = value; |
| 8 | 192 | | } |
| | 193 | | } |
| | 194 | |
|
| | 195 | | /// <summary> |
| | 196 | | /// Gets or sets the message’s "time to live" value. |
| | 197 | | /// </summary> |
| | 198 | | /// <value>The message’s time to live value.</value> |
| | 199 | | /// <remarks> |
| | 200 | | /// This value is the relative duration after which the message expires, starting from the instant |
| | 201 | | /// the message has been accepted and stored by the broker, as captured in "SystemPropertiesCollection.Enqu |
| | 202 | | /// When not set explicitly, the assumed value is the DefaultTimeToLive for the respective queue or topic. |
| | 203 | | /// A message-level <see cref="TimeToLive"/> value cannot be longer than the entity's DefaultTimeToLive |
| | 204 | | /// setting and it is silently adjusted if it does. |
| | 205 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/message-expiration">Expiration</a> |
| | 206 | | /// </remarks> |
| | 207 | | public TimeSpan TimeToLive |
| | 208 | | { |
| | 209 | | get |
| | 210 | | { |
| 88 | 211 | | if (_timeToLive == TimeSpan.Zero) |
| | 212 | | { |
| 74 | 213 | | return TimeSpan.MaxValue; |
| | 214 | | } |
| 14 | 215 | | return _timeToLive; |
| | 216 | | } |
| | 217 | |
|
| | 218 | | set |
| | 219 | | { |
| 8 | 220 | | Argument.AssertPositive(value, nameof(TimeToLive)); |
| 8 | 221 | | _timeToLive = value; |
| 8 | 222 | | } |
| | 223 | | } |
| | 224 | |
|
| | 225 | | /// <summary>Gets or sets the a correlation identifier.</summary> |
| | 226 | | /// <value>Correlation identifier.</value> |
| | 227 | | /// <remarks> |
| | 228 | | /// Allows an application to specify a context for the message for the purposes of correlation, |
| | 229 | | /// for example reflecting the MessageId of a message that is being replied to. |
| | 230 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-messages-payloads?#messag |
| | 231 | | /// </remarks> |
| 90 | 232 | | public string CorrelationId { get; set; } |
| | 233 | |
|
| | 234 | | /// <summary>Gets or sets an application specific label.</summary> |
| | 235 | | /// <value>The application specific label</value> |
| | 236 | | /// <remarks> |
| | 237 | | /// This property enables the application to indicate the purpose of the message to the receiver in a standard |
| | 238 | | /// fashion, similar to an email subject line. The mapped AMQP property is "subject". |
| | 239 | | /// </remarks> |
| 110 | 240 | | public string Label { get; set; } |
| | 241 | |
|
| | 242 | | /// <summary>Gets or sets the "to" address.</summary> |
| | 243 | | /// <value>The "to" address.</value> |
| | 244 | | /// <remarks> |
| | 245 | | /// This property is reserved for future use in routing scenarios and presently ignored by the broker itself. |
| | 246 | | /// Applications can use this value in rule-driven |
| | 247 | | /// <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-auto-forwarding">auto-forwar |
| | 248 | | /// intended logical destination of the message. |
| | 249 | | /// </remarks> |
| 92 | 250 | | public string To { get; set; } |
| | 251 | |
|
| | 252 | | /// <summary>Gets or sets the content type descriptor.</summary> |
| | 253 | | /// <value>RFC2045 Content-Type descriptor.</value> |
| | 254 | | /// <remarks> |
| | 255 | | /// Optionally describes the payload of the message, with a descriptor following the format of |
| | 256 | | /// RFC2045, Section 5, for example "application/json". |
| | 257 | | /// </remarks> |
| 92 | 258 | | public string ContentType { get; set; } |
| | 259 | |
|
| | 260 | | /// <summary>Gets or sets the address of an entity to send replies to.</summary> |
| | 261 | | /// <value>The reply entity address.</value> |
| | 262 | | /// <remarks> |
| | 263 | | /// This optional and application-defined value is a standard way to express a reply path |
| | 264 | | /// to the receiver of the message. When a sender expects a reply, it sets the value to the |
| | 265 | | /// absolute or relative path of the queue or topic it expects the reply to be sent to. |
| | 266 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-messages-payloads?#messag |
| | 267 | | /// </remarks> |
| 92 | 268 | | public string ReplyTo { get; set; } |
| | 269 | |
|
| | 270 | | /// <summary>Gets or sets the date and time in UTC at which the message will be enqueued. This |
| | 271 | | /// property returns the time in UTC; when setting the property, the supplied DateTime value must also be in UTC |
| | 272 | | /// <value>The scheduled enqueue time in UTC. This value is for delayed message sending. |
| | 273 | | /// It is utilized to delay messages sending to a specific time in the future.</value> |
| | 274 | | /// <remarks> Message enqueuing time does not mean that the message will be sent at the same time. It will get e |
| | 275 | | /// depends on the queue's workload and its state.</remarks> |
| 164 | 276 | | public DateTimeOffset ScheduledEnqueueTime { get; set; } |
| | 277 | |
|
| | 278 | | /// <summary> |
| | 279 | | /// Gets the "user properties" bag, which can be used for custom message metadata. |
| | 280 | | /// </summary> |
| | 281 | | /// <remarks> |
| | 282 | | /// Only following value types are supported: |
| | 283 | | /// byte, sbyte, char, short, ushort, int, uint, long, ulong, float, double, decimal, |
| | 284 | | /// bool, Guid, string, Uri, DateTime, DateTimeOffset, TimeSpan |
| | 285 | | /// </remarks> |
| 404 | 286 | | public IDictionary<string, object> Properties { get; internal set; } |
| | 287 | |
|
| | 288 | | /////// <summary> |
| | 289 | | ///// Gets the <see cref="SystemPropertiesCollection"/>, which is used to store properties that are set by the s |
| | 290 | | ///// </summary> |
| | 291 | | //public SystemPropertiesCollection SystemProperties { get; internal set; } |
| | 292 | |
|
| | 293 | | /// <summary>Returns a string that represents the current message.</summary> |
| | 294 | | /// <returns>The string representation of the current message.</returns> |
| | 295 | | public override string ToString() |
| | 296 | | { |
| 6 | 297 | | return string.Format(CultureInfo.CurrentCulture, "{{MessageId:{0}}}", MessageId); |
| | 298 | | } |
| | 299 | |
|
| | 300 | | private static void ValidateMessageId(string messageId) |
| | 301 | | { |
| 34 | 302 | | if (string.IsNullOrEmpty(messageId) || |
| 34 | 303 | | messageId.Length > Constants.MaxMessageIdLength) |
| | 304 | | { |
| 4 | 305 | | throw new ArgumentException("MessageIdIsNullOrEmptyOrOverMaxValue"); |
| | 306 | | } |
| 30 | 307 | | } |
| | 308 | |
|
| | 309 | | private static void ValidateSessionId(string sessionId) |
| | 310 | | { |
| 16 | 311 | | if (sessionId != null && sessionId.Length > Constants.MaxSessionIdLength) |
| | 312 | | { |
| 0 | 313 | | throw new ArgumentException("SessionIdIsOverMaxValue"); |
| | 314 | | } |
| 16 | 315 | | } |
| | 316 | |
|
| | 317 | | private static void ValidatePartitionKey(string partitionKey) |
| | 318 | | { |
| 14 | 319 | | if (partitionKey != null && partitionKey.Length > Constants.MaxPartitionKeyLength) |
| | 320 | | { |
| 0 | 321 | | throw new ArgumentException("PropertyValueOverMaxValue"); |
| | 322 | | } |
| 14 | 323 | | } |
| | 324 | | } |
| | 325 | | } |