| | 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.Collections.ObjectModel; |
| | 7 | | using System.Globalization; |
| | 8 | | using Azure.Core; |
| | 9 | |
|
| | 10 | | namespace Azure.Messaging.ServiceBus |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// |
| | 14 | | /// </summary> |
| | 15 | | public class ServiceBusReceivedMessage |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Indicates whether the user has settled the message as part of their callback. |
| | 19 | | /// If they have done so, we will not autocomplete. |
| | 20 | | /// </summary> |
| 108 | 21 | | internal bool IsSettled { get; set; } |
| | 22 | |
|
| 0 | 23 | | internal ServiceBusMessage SentMessage { get; set; } = new ServiceBusMessage(); |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Gets the body of the message. |
| | 27 | | /// </summary> |
| 10 | 28 | | public BinaryData Body => SentMessage.Body; |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Gets or sets the MessageId to identify the message. |
| | 32 | | /// </summary> |
| | 33 | | /// <remarks> |
| | 34 | | /// The message identifier is an application-defined value that uniquely identifies the |
| | 35 | | /// message and its payload. The identifier is a free-form string and can reflect a GUID |
| | 36 | | /// or an identifier derived from the application context. If enabled, the |
| | 37 | | /// <a href="https://docs.microsoft.com/azure/service-bus-messaging/duplicate-detection">duplicate detection< |
| | 38 | | /// feature identifies and removes second and further submissions of messages with the |
| | 39 | | /// same MessageId. |
| | 40 | | /// </remarks> |
| 34 | 41 | | public string MessageId => SentMessage.MessageId; |
| | 42 | |
|
| | 43 | | /// <summary>Gets a partition key for sending a message to a partitioned entity.</summary> |
| | 44 | | /// <value>The partition key. Maximum length is 128 characters.</value> |
| | 45 | | /// <remarks> |
| | 46 | | /// For <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-partitioning">partitioned |
| | 47 | | /// setting this value enables assigning related messages to the same internal partition, so that submission |
| | 48 | | /// order is correctly recorded. The partition is chosen by a hash function over this value and cannot be cho |
| | 49 | | /// directly. For session-aware entities, the <see cref="SessionId"/> property overrides this value. |
| | 50 | | /// </remarks> |
| 10 | 51 | | public string PartitionKey => SentMessage.PartitionKey; |
| | 52 | |
|
| | 53 | | /// <summary>Gets a partition key for sending a message into an entity via a partitioned transfer queue.</summar |
| | 54 | | /// <value>The partition key. Maximum length is 128 characters. </value> |
| | 55 | | /// <remarks> |
| | 56 | | /// If a message is sent via a transfer queue in the scope of a transaction, this value selects the |
| | 57 | | /// transfer queue partition: This is functionally equivalent to <see cref="PartitionKey"/> and ensures that |
| | 58 | | /// messages are kept together and in order as they are transferred. |
| | 59 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-transactions#transfers-an |
| | 60 | | /// </remarks> |
| 6 | 61 | | public string ViaPartitionKey => SentMessage.ViaPartitionKey; |
| | 62 | |
|
| | 63 | | /// <summary>Gets the session identifier for a session-aware entity.</summary> |
| | 64 | | /// <value>The session identifier. Maximum length is 128 characters.</value> |
| | 65 | | /// <remarks> |
| | 66 | | /// For session-aware entities, this application-defined value specifies the session |
| | 67 | | /// affiliation of the message. Messages with the same session identifier are subject |
| | 68 | | /// to summary locking and enable exact in-order processing and demultiplexing. |
| | 69 | | /// For session-unaware entities, this value is ignored. |
| | 70 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/message-sessions">Message Sessions</a |
| | 71 | | /// </remarks> |
| 34 | 72 | | public string SessionId => SentMessage.SessionId; |
| | 73 | |
|
| | 74 | | /// <summary>Gets a session identifier augmenting the <see cref="ReplyTo"/> address.</summary> |
| | 75 | | /// <value>Session identifier. Maximum length is 128 characters.</value> |
| | 76 | | /// <remarks> |
| | 77 | | /// This value augments the ReplyTo information and specifies which SessionId should be set |
| | 78 | | /// for the reply when sent to the reply entity. See <a href="https://docs.microsoft.com/azure/service-bus-me |
| | 79 | | /// </remarks> |
| 8 | 80 | | public string ReplyToSessionId => SentMessage.ReplyToSessionId; |
| | 81 | |
|
| | 82 | | /// <summary> |
| | 83 | | /// Gets the message’s "time to live" value. |
| | 84 | | /// </summary> |
| | 85 | | /// <value>The message’s time to live value.</value> |
| | 86 | | /// <remarks> |
| | 87 | | /// This value is the relative duration after which the message expires, starting from the instant |
| | 88 | | /// the message has been accepted and stored by the broker, as captured in "SystemPropertiesCollection.Enqu |
| | 89 | | /// When not set explicitly, the assumed value is the DefaultTimeToLive for the respective queue or topic. |
| | 90 | | /// A message-level <see cref="TimeToLive"/> value cannot be longer than the entity's DefaultTimeToLive |
| | 91 | | /// setting and it is silently adjusted if it does. |
| | 92 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/message-expiration">Expiration</a> |
| | 93 | | /// </remarks> |
| 8 | 94 | | public TimeSpan TimeToLive => SentMessage.TimeToLive; |
| | 95 | |
|
| | 96 | | /// <summary>Gets the a correlation identifier.</summary> |
| | 97 | | /// <value>Correlation identifier.</value> |
| | 98 | | /// <remarks> |
| | 99 | | /// Allows an application to specify a context for the message for the purposes of correlation, |
| | 100 | | /// for example reflecting the MessageId of a message that is being replied to. |
| | 101 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-messages-payloads?#messag |
| | 102 | | /// </remarks> |
| 6 | 103 | | public string CorrelationId => SentMessage.CorrelationId; |
| | 104 | |
|
| | 105 | | /// <summary>Gets an application specific label.</summary> |
| | 106 | | /// <value>The application specific label</value> |
| | 107 | | /// <remarks> |
| | 108 | | /// This property enables the application to indicate the purpose of the message to the receiver in a standard |
| | 109 | | /// fashion, similar to an email subject line. The mapped AMQP property is "subject". |
| | 110 | | /// </remarks> |
| 8 | 111 | | public string Label => SentMessage.Label; |
| | 112 | |
|
| | 113 | | /// <summary>Gets the "to" address.</summary> |
| | 114 | | /// <value>The "to" address.</value> |
| | 115 | | /// <remarks> |
| | 116 | | /// This property is reserved for future use in routing scenarios and presently ignored by the broker itself. |
| | 117 | | /// Applications can use this value in rule-driven |
| | 118 | | /// <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-auto-forwarding">auto-forwar |
| | 119 | | /// intended logical destination of the message. |
| | 120 | | /// </remarks> |
| 8 | 121 | | public string To => SentMessage.To; |
| | 122 | |
|
| | 123 | | /// <summary>Gets the content type descriptor.</summary> |
| | 124 | | /// <value>RFC2045 Content-Type descriptor.</value> |
| | 125 | | /// <remarks> |
| | 126 | | /// Optionally describes the payload of the message, with a descriptor following the format of |
| | 127 | | /// RFC2045, Section 5, for example "application/json". |
| | 128 | | /// </remarks> |
| 8 | 129 | | public string ContentType => SentMessage.ContentType; |
| | 130 | |
|
| | 131 | | /// <summary>Gets the address of an entity to send replies to.</summary> |
| | 132 | | /// <value>The reply entity address.</value> |
| | 133 | | /// <remarks> |
| | 134 | | /// This optional and application-defined value is a standard way to express a reply path |
| | 135 | | /// to the receiver of the message. When a sender expects a reply, it sets the value to the |
| | 136 | | /// absolute or relative path of the queue or topic it expects the reply to be sent to. |
| | 137 | | /// See <a href="https://docs.microsoft.com/azure/service-bus-messaging/service-bus-messages-payloads?#messag |
| | 138 | | /// </remarks> |
| 8 | 139 | | public string ReplyTo => SentMessage.ReplyTo; |
| | 140 | |
|
| | 141 | | /// <summary>Gets the date and time in UTC at which the message will be enqueued. This |
| | 142 | | /// property returns the time in UTC; when setting the property, the supplied DateTime value must also be in UTC |
| | 143 | | /// <value>The scheduled enqueue time in UTC. This value is for delayed message sending. |
| | 144 | | /// It is utilized to delay messages sending to a specific time in the future.</value> |
| | 145 | | /// <remarks> Message enqueuing time does not mean that the message will be sent at the same time. It will get e |
| | 146 | | /// depends on the queue's workload and its state.</remarks> |
| 6 | 147 | | public DateTimeOffset ScheduledEnqueueTime => SentMessage.ScheduledEnqueueTime; |
| | 148 | |
|
| | 149 | | /// <summary> |
| | 150 | | /// Gets the "user properties" bag, which can be used for custom message metadata. |
| | 151 | | /// </summary> |
| | 152 | | /// <remarks> |
| | 153 | | /// Only following value types are supported: |
| | 154 | | /// byte, sbyte, char, short, ushort, int, uint, long, ulong, float, double, decimal, |
| | 155 | | /// bool, Guid, string, Uri, DateTime, DateTimeOffset, TimeSpan |
| | 156 | | /// </remarks> |
| 20 | 157 | | public IReadOnlyDictionary<string, object> Properties => new ReadOnlyDictionary<string, object> (SentMessage.Pro |
| | 158 | |
|
| | 159 | | /// <summary> |
| | 160 | | /// User property key representing deadletter reason, when a message is received from a deadletter subqueue of a |
| | 161 | | /// </summary> |
| | 162 | | internal const string DeadLetterReasonHeader = "DeadLetterReason"; |
| | 163 | |
|
| | 164 | | /// <summary> |
| | 165 | | /// User property key representing detailed error description, when a message is received from a deadletter subq |
| | 166 | | /// </summary> |
| | 167 | | internal const string DeadLetterErrorDescriptionHeader = "DeadLetterErrorDescription"; |
| | 168 | |
|
| | 169 | | /// <summary> |
| | 170 | | /// Gets the lock token for the current message. |
| | 171 | | /// </summary> |
| | 172 | | /// <remarks> |
| | 173 | | /// The lock token is a reference to the lock that is being held by the broker in ReceiveMode.PeekLock mode. |
| | 174 | | /// Locks are used to explicitly settle messages as explained in the <a href="https://docs.microsoft.com/azure |
| | 175 | | /// The token can also be used to pin the lock permanently through the <a href="https://docs.microsoft.com/azu |
| | 176 | | /// regular delivery state flow. This property is read-only. |
| | 177 | | /// </remarks> |
| 62 | 178 | | public string LockToken => LockTokenGuid.ToString(); |
| | 179 | |
|
| | 180 | | /// <summary> |
| | 181 | | /// Get the current delivery count. |
| | 182 | | /// </summary> |
| | 183 | | /// <value>This value starts at 1.</value> |
| | 184 | | /// <remarks> |
| | 185 | | /// Number of deliveries that have been attempted for this message. The count is incremented when a message l |
| | 186 | | /// or the message is explicitly abandoned by the receiver. This property is read-only. |
| | 187 | | /// </remarks> |
| 16 | 188 | | public int DeliveryCount { get; internal set; } |
| | 189 | |
|
| | 190 | | /// <summary>Gets the date and time in UTC until which the message will be locked in the queue/subscription.</su |
| | 191 | | /// <value>The date and time until which the message will be locked in the queue/subscription.</value> |
| | 192 | | /// <remarks> |
| | 193 | | /// For messages retrieved under a lock (peek-lock receive mode, not pre-settled) this property reflects the U |
| | 194 | | /// instant until which the message is held locked in the queue/subscription. When the lock expires, the <se |
| | 195 | | /// is incremented and the message is again available for retrieval. This property is read-only. |
| | 196 | | /// </remarks> |
| 10 | 197 | | public DateTimeOffset LockedUntil { get; internal set; } |
| | 198 | |
|
| | 199 | | /// <summary>Gets the unique number assigned to a message by Service Bus.</summary> |
| | 200 | | /// <remarks> |
| | 201 | | /// The sequence number is a unique 64-bit integer assigned to a message as it is accepted |
| | 202 | | /// and stored by the broker and functions as its true identifier. For partitioned entities, |
| | 203 | | /// the topmost 16 bits reflect the partition identifier. Sequence numbers monotonically increase. |
| | 204 | | /// They roll over to 0 when the 48-64 bit range is exhausted. This property is read-only. |
| | 205 | | /// </remarks> |
| 0 | 206 | | public long SequenceNumber { get; internal set; } = -1; |
| | 207 | |
|
| | 208 | | /// <summary> |
| | 209 | | /// Gets the name of the queue or subscription that this message was enqueued on, before it was deadlettered. |
| | 210 | | /// </summary> |
| | 211 | | /// <remarks> |
| | 212 | | /// Only set in messages that have been dead-lettered and subsequently auto-forwarded from the dead-letter que |
| | 213 | | /// to another entity. Indicates the entity in which the message was dead-lettered. This property is read-on |
| | 214 | | /// </remarks> |
| 8 | 215 | | public string DeadLetterSource { get; internal set; } |
| | 216 | |
|
| 0 | 217 | | internal short PartitionId { get; set; } |
| | 218 | |
|
| | 219 | | /// <summary>Gets or sets the original sequence number of the message.</summary> |
| | 220 | | /// <value>The enqueued sequence number of the message.</value> |
| | 221 | | /// <remarks> |
| | 222 | | /// For messages that have been auto-forwarded, this property reflects the sequence number |
| | 223 | | /// that had first been assigned to the message at its original point of submission. This property is read-only. |
| | 224 | | /// </remarks> |
| 8 | 225 | | public long EnqueuedSequenceNumber { get; internal set; } |
| | 226 | |
|
| | 227 | | /// <summary>Gets or sets the date and time of the sent time in UTC.</summary> |
| | 228 | | /// <value>The enqueue time in UTC. </value> |
| | 229 | | /// <remarks> |
| | 230 | | /// The UTC instant at which the message has been accepted and stored in the entity. |
| | 231 | | /// This value can be used as an authoritative and neutral arrival time indicator when |
| | 232 | | /// the receiver does not want to trust the sender's clock. This property is read-only. |
| | 233 | | /// </remarks> |
| 8 | 234 | | public DateTimeOffset EnqueuedTime { get; internal set; } |
| | 235 | |
|
| 92 | 236 | | internal Guid LockTokenGuid { get; set; } |
| | 237 | |
|
| | 238 | | /// <summary>Gets the date and time in UTC at which the message is set to expire.</summary> |
| | 239 | | /// <value>The message expiration time in UTC. This property is read-only.</value> |
| | 240 | | /// <exception cref="System.InvalidOperationException">If the message has not been received. For example if a ne |
| | 241 | | /// <remarks> |
| | 242 | | /// The UTC instant at which the message is marked for removal and no longer available for retrieval |
| | 243 | | /// from the entity due to expiration. Expiry is controlled by the <see cref="ServiceBusMessage.TimeToLive"/> p |
| | 244 | | /// and this property is computed from <see cref="EnqueuedTime"/>+<see cref="ServiceBusMessage.TimeToLive"/></r |
| | 245 | | public DateTimeOffset ExpiresAt |
| | 246 | | { |
| | 247 | | get |
| | 248 | | { |
| 0 | 249 | | if (TimeToLive >= DateTimeOffset.MaxValue.Subtract(EnqueuedTime)) |
| | 250 | | { |
| 0 | 251 | | return DateTimeOffset.MaxValue; |
| | 252 | | } |
| | 253 | |
|
| 0 | 254 | | return EnqueuedTime.Add(TimeToLive); |
| | 255 | | } |
| | 256 | | } |
| | 257 | |
|
| | 258 | | /// <summary> |
| | 259 | | /// |
| | 260 | | /// </summary> |
| | 261 | | public string DeadLetterReason |
| | 262 | | { |
| | 263 | | get |
| | 264 | | { |
| 2 | 265 | | if (Properties.TryGetValue(DeadLetterReasonHeader, out object reason)) |
| | 266 | | { |
| 2 | 267 | | return reason as string; |
| | 268 | | } |
| 0 | 269 | | return null; |
| | 270 | | } |
| | 271 | | } |
| | 272 | |
|
| | 273 | | /// <summary> |
| | 274 | | /// |
| | 275 | | /// </summary> |
| | 276 | | public string DeadLetterErrorDescription |
| | 277 | | { |
| | 278 | | get |
| | 279 | | { |
| 2 | 280 | | if (Properties.TryGetValue(DeadLetterErrorDescriptionHeader, out object description)) |
| | 281 | | { |
| 2 | 282 | | return description as string; |
| | 283 | | } |
| 0 | 284 | | return null; |
| | 285 | | } |
| | 286 | | } |
| | 287 | |
|
| | 288 | | /// <summary> |
| | 289 | | /// Creates a new message from the specified payload. |
| | 290 | | /// </summary> |
| | 291 | | /// <param name="body">The payload of the message in bytes</param> |
| 0 | 292 | | internal ServiceBusReceivedMessage(ReadOnlyMemory<byte> body) |
| | 293 | | { |
| 0 | 294 | | SentMessage = new ServiceBusMessage(body); |
| 0 | 295 | | } |
| | 296 | |
|
| | 297 | | /// <summary> |
| | 298 | | /// Creates a new message from the specified payload. |
| | 299 | | /// </summary> |
| 66 | 300 | | internal ServiceBusReceivedMessage() |
| | 301 | | { |
| 66 | 302 | | } |
| | 303 | |
|
| | 304 | | /////// <summary> |
| | 305 | | ///// Gets the <see cref="SystemPropertiesCollection"/>, which is used to store properties that are set by the s |
| | 306 | | ///// </summary> |
| | 307 | | //public SystemPropertiesCollection SystemProperties { get; internal set; } |
| | 308 | |
|
| | 309 | | /// <summary>Returns a string that represents the current message.</summary> |
| | 310 | | /// <returns>The string representation of the current message.</returns> |
| | 311 | | public override string ToString() |
| | 312 | | { |
| 0 | 313 | | return string.Format(CultureInfo.CurrentCulture, "{{MessageId:{0}}}", MessageId); |
| | 314 | | } |
| | 315 | | } |
| | 316 | | } |