public static AmqpMessage SBMessageToAmqpMessage(ServiceBusMessage sbMessage) { var amqpMessage = sbMessage.ToAmqpMessage(); amqpMessage.Properties.MessageId = sbMessage.MessageId; amqpMessage.Properties.CorrelationId = sbMessage.CorrelationId; amqpMessage.Properties.ContentType = sbMessage.ContentType; amqpMessage.Properties.Subject = sbMessage.Subject; amqpMessage.Properties.To = sbMessage.To; amqpMessage.Properties.ReplyTo = sbMessage.ReplyTo; amqpMessage.Properties.GroupId = sbMessage.SessionId; amqpMessage.Properties.ReplyToGroupId = sbMessage.ReplyToSessionId; if (sbMessage.TimeToLive != TimeSpan.MaxValue) { amqpMessage.Header.Ttl = (uint)sbMessage.TimeToLive.TotalMilliseconds; amqpMessage.Properties.CreationTime = DateTime.UtcNow; if (AmqpConstants.MaxAbsoluteExpiryTime - amqpMessage.Properties.CreationTime.Value > sbMessage.TimeToLive) { amqpMessage.Properties.AbsoluteExpiryTime = amqpMessage.Properties.CreationTime.Value + sbMessage.TimeToLive; } else { amqpMessage.Properties.AbsoluteExpiryTime = AmqpConstants.MaxAbsoluteExpiryTime; } } if ((sbMessage.ScheduledEnqueueTime != null) && sbMessage.ScheduledEnqueueTime > DateTimeOffset.MinValue) { amqpMessage.MessageAnnotations.Map.Add(AmqpMessageConstants.ScheduledEnqueueTimeUtcName, sbMessage.ScheduledEnqueueTime.UtcDateTime); } if (sbMessage.PartitionKey != null) { amqpMessage.MessageAnnotations.Map.Add(AmqpMessageConstants.PartitionKeyName, sbMessage.PartitionKey); } if (sbMessage.TransactionPartitionKey != null) { amqpMessage.MessageAnnotations.Map.Add(AmqpMessageConstants.ViaPartitionKeyName, sbMessage.TransactionPartitionKey); } if (sbMessage.ApplicationProperties != null && sbMessage.ApplicationProperties.Count > 0) { if (amqpMessage.ApplicationProperties == null) { amqpMessage.ApplicationProperties = new ApplicationProperties(); } foreach (var pair in sbMessage.ApplicationProperties) { if (TryGetAmqpObjectFromNetObject(pair.Value, MappingType.ApplicationProperty, out var amqpObject)) { amqpMessage.ApplicationProperties.Map.Add(pair.Key, amqpObject); } else { throw new NotSupportedException(Resources.InvalidAmqpMessageProperty.FormatForUser(pair.Key.GetType())); } } } return(amqpMessage); }
public static AmqpMessage SBMessageToAmqpMessage(ServiceBusMessage sbMessage) { // body var amqpMessage = sbMessage.ToAmqpMessage(); // properties amqpMessage.Properties.MessageId = sbMessage.MessageId; amqpMessage.Properties.CorrelationId = sbMessage.CorrelationId; amqpMessage.Properties.ContentType = sbMessage.ContentType; amqpMessage.Properties.ContentEncoding = sbMessage.AmqpMessage.Properties.ContentEncoding; amqpMessage.Properties.Subject = sbMessage.Subject; amqpMessage.Properties.To = sbMessage.To; amqpMessage.Properties.ReplyTo = sbMessage.ReplyTo; amqpMessage.Properties.GroupId = sbMessage.SessionId; amqpMessage.Properties.ReplyToGroupId = sbMessage.ReplyToSessionId; amqpMessage.Properties.GroupSequence = sbMessage.AmqpMessage.Properties.GroupSequence; if (sbMessage.AmqpMessage.Properties.UserId.HasValue) { ReadOnlyMemory <byte> userId = sbMessage.AmqpMessage.Properties.UserId.Value; if (MemoryMarshal.TryGetArray(userId, out ArraySegment <byte> segment)) { amqpMessage.Properties.UserId = segment; } else { amqpMessage.Properties.UserId = new ArraySegment <byte>(userId.ToArray()); } } // If TTL is set, it is used to calculate AbsoluteExpiryTime and CreationTime if (sbMessage.TimeToLive != TimeSpan.MaxValue) { amqpMessage.Header.Ttl = (uint)sbMessage.TimeToLive.TotalMilliseconds; amqpMessage.Properties.CreationTime = DateTime.UtcNow; if (AmqpConstants.MaxAbsoluteExpiryTime - amqpMessage.Properties.CreationTime.Value > sbMessage.TimeToLive) { amqpMessage.Properties.AbsoluteExpiryTime = amqpMessage.Properties.CreationTime.Value + sbMessage.TimeToLive; } else { amqpMessage.Properties.AbsoluteExpiryTime = AmqpConstants.MaxAbsoluteExpiryTime; } } else { if (sbMessage.AmqpMessage.Properties.CreationTime.HasValue) { amqpMessage.Properties.CreationTime = sbMessage.AmqpMessage.Properties.CreationTime.Value.UtcDateTime; } if (sbMessage.AmqpMessage.Properties.AbsoluteExpiryTime.HasValue) { amqpMessage.Properties.AbsoluteExpiryTime = sbMessage.AmqpMessage.Properties.AbsoluteExpiryTime.Value.UtcDateTime; } } // message annotations foreach (KeyValuePair <string, object> kvp in sbMessage.AmqpMessage.MessageAnnotations) { switch (kvp.Key) { case AmqpMessageConstants.ScheduledEnqueueTimeUtcName: if ((sbMessage.ScheduledEnqueueTime != null) && sbMessage.ScheduledEnqueueTime > DateTimeOffset.MinValue) { amqpMessage.MessageAnnotations.Map.Add(AmqpMessageConstants.ScheduledEnqueueTimeUtcName, sbMessage.ScheduledEnqueueTime.UtcDateTime); } break; case AmqpMessageConstants.PartitionKeyName: if (sbMessage.PartitionKey != null) { amqpMessage.MessageAnnotations.Map.Add(AmqpMessageConstants.PartitionKeyName, sbMessage.PartitionKey); } break; case AmqpMessageConstants.ViaPartitionKeyName: if (sbMessage.TransactionPartitionKey != null) { amqpMessage.MessageAnnotations.Map.Add(AmqpMessageConstants.ViaPartitionKeyName, sbMessage.TransactionPartitionKey); } break; default: amqpMessage.MessageAnnotations.Map.Add(kvp.Key, kvp.Value); break; } } // application properties if (sbMessage.ApplicationProperties != null && sbMessage.ApplicationProperties.Count > 0) { if (amqpMessage.ApplicationProperties == null) { amqpMessage.ApplicationProperties = new ApplicationProperties(); } foreach (KeyValuePair <string, object> pair in sbMessage.ApplicationProperties) { if (TryGetAmqpObjectFromNetObject(pair.Value, MappingType.ApplicationProperty, out var amqpObject)) { amqpMessage.ApplicationProperties.Map.Add(pair.Key, amqpObject); } else { throw new NotSupportedException(Resources.InvalidAmqpMessageProperty.FormatForUser(pair.Key.GetType())); } } } // delivery annotations foreach (KeyValuePair <string, object> kvp in sbMessage.AmqpMessage.DeliveryAnnotations) { if (TryGetAmqpObjectFromNetObject(kvp.Value, MappingType.ApplicationProperty, out var amqpObject)) { amqpMessage.DeliveryAnnotations.Map.Add(kvp.Key, amqpObject); } } // header - except for ttl which is set above with the properties if (sbMessage.AmqpMessage.Header.DeliveryCount != null) { amqpMessage.Header.DeliveryCount = sbMessage.AmqpMessage.Header.DeliveryCount; } if (sbMessage.AmqpMessage.Header.Durable != null) { amqpMessage.Header.Durable = sbMessage.AmqpMessage.Header.Durable; } if (sbMessage.AmqpMessage.Header.FirstAcquirer != null) { amqpMessage.Header.FirstAcquirer = sbMessage.AmqpMessage.Header.FirstAcquirer; } if (sbMessage.AmqpMessage.Header.Priority != null) { amqpMessage.Header.Priority = sbMessage.AmqpMessage.Header.Priority; } // footer foreach (KeyValuePair <string, object> kvp in sbMessage.AmqpMessage.Footer) { amqpMessage.Footer.Map.Add(kvp.Key, kvp.Value); } return(amqpMessage); }