public static void NormalizeDescription(this QueueDescription description, string baseAddress)
        {
            if (!string.IsNullOrWhiteSpace(description.ForwardTo))
            {
                description.ForwardTo = NormalizeForwardToAddress(description.ForwardTo, baseAddress);
            }

            if (!string.IsNullOrWhiteSpace(description.ForwardDeadLetteredMessagesTo))
            {
                description.ForwardDeadLetteredMessagesTo = NormalizeForwardToAddress(description.ForwardDeadLetteredMessagesTo, baseAddress);
            }
        }
        /// <summary>
        /// Creates a queue if not exists.
        /// </summary>
        /// <param name="manager">The manager.</param>
        /// <param name="queueName">Name of the queue.</param>
        /// <returns><see cref="QueueDescription"/> Queue description.</returns>
        /// <exception cref="ArgumentException">Cannot create queue as a topic with this name exists already - queueName</exception>
        public static QueueDescription CreateQueueIfNotExists(this ManagementClient manager, string queueName)
        {
            lock (CreationLock)
            {
                QueueDescription queue;

                try
                {
                    queue = manager.GetQueueAsync(queueName).GetAwaiter().GetResult();
                }
                catch (Exception)
                {
                    try
                    {
                        // Check to see if a topic exists already with the name of the queue you are trying to create.
                        if (manager.GetTopicAsync(queueName).GetAwaiter().GetResult() != null)
                        {
                            throw new ArgumentException($"Cannot create queue as a topic with this name exists already: {queueName}", "queueName");
                        }
                    }
                    catch (ArgumentException)
                    {
                        throw;
                    }
                    catch (Exception)
                    {
                        // do nothing in this case as topic does not exist (and its ok to create a queue with this name).
                    }


                    // Has failed as the topic does not exist, therefore, create!
                    var instanceInfo = manager.GetNamespaceInfoAsync().GetAwaiter().GetResult();
                    var queueDesc    = new QueueDescription(queueName)
                    {
                        // Add duplicate detection.
                        RequiresDuplicateDetection          = true,
                        DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(60),

                        // Set the max topic size - allowed values: 1024;2048;3072;4096;5120;8192000;
                        // Our premium SB instances should all be 80gb (819200mb), otherwise 5gb (5120mb).
                        MaxSizeInMB = (instanceInfo.MessagingSku == MessagingSku.Premium ? 8192000 : 5120)
                    };

                    queue = manager.CreateQueueAsync(queueDesc).GetAwaiter().GetResult();
                }

                return(queue);
            }
        }
Пример #3
0
        public static XDocument Serialize(this QueueDescription description)
        {
            var queueDescriptionElements = new List <XElement>()
            {
                new XElement(XName.Get("LockDuration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.LockDuration)),
                new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.MaxSizeInMB)),
                new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.RequiresDuplicateDetection)),
                new XElement(XName.Get("RequiresSession", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.RequiresSession)),
                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeToLive", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.DefaultMessageTimeToLive)) : null,
                new XElement(XName.Get("DeadLetteringOnMessageExpiration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.EnableDeadLetteringOnMessageExpiration)),
                description.RequiresDuplicateDetection&& description.DuplicateDetectionHistoryTimeWindow != default ?
                new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.DuplicateDetectionHistoryTimeWindow))
                    : null,
                new XElement(XName.Get("MaxDeliveryCount", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.MaxDeliveryCount)),
                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.EnableBatchedOperations)),
                new XElement(XName.Get("IsAnonymousAccessible", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.IsAnonymousAccessible)),
                description.AuthorizationRules?.Serialize(),
                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToString()),
                description.ForwardTo != null ? new XElement(XName.Get("ForwardTo", ManagementClientConstants.ServiceBusNamespace), description.ForwardTo) : null,
                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.ServiceBusNamespace), description.UserMetadata) : null,
                description.internalSupportOrdering.HasValue ? new XElement(XName.Get("SupportOrdering", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.internalSupportOrdering.Value)) : null,
                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.AutoDeleteOnIdle)) : null,
                new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.EnablePartitioning)),
                description.ForwardDeadLetteredMessagesTo != null ? new XElement(XName.Get("ForwardDeadLetteredMessagesTo", ManagementClientConstants.ServiceBusNamespace), description.ForwardDeadLetteredMessagesTo) : null,
                new XElement(XName.Get("EnableExpress", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.EnableExpress))
            };

            // Insert unknown properties in the exact order they were in the received xml.
            // Expectation is that servicebus will add any new elements only at the bottom of the xml tree.
            if (description.UnknownProperties != null)
            {
                queueDescriptionElements.AddRange(description.UnknownProperties);
            }

            return(new XDocument(
                       new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
                                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
                                                 new XAttribute("type", "application/xml"),
                                                 new XElement(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace),
                                                              queueDescriptionElements.ToArray())))));
        }
        public static XDocument Serialize(this QueueDescription description)
        {
            var queueDescriptionElements = new List <object>()
            {
                new XElement(XName.Get("LockDuration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.LockDuration)),
                new XElement(XName.Get("MaxSizeInMegabytes", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.MaxSizeInMB)),
                new XElement(XName.Get("RequiresDuplicateDetection", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.RequiresDuplicateDetection)),
                new XElement(XName.Get("RequiresSession", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.RequiresSession)),
                description.DefaultMessageTimeToLive != TimeSpan.MaxValue ? new XElement(XName.Get("DefaultMessageTimeToLive", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.DefaultMessageTimeToLive)) : null,
                new XElement(XName.Get("DeadLetteringOnMessageExpiration", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.EnableDeadLetteringOnMessageExpiration)),
                description.RequiresDuplicateDetection&& description.DuplicateDetectionHistoryTimeWindow != default ?
                new XElement(XName.Get("DuplicateDetectionHistoryTimeWindow", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.DuplicateDetectionHistoryTimeWindow))
                    : null,
                new XElement(XName.Get("MaxDeliveryCount", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.MaxDeliveryCount)),
                new XElement(XName.Get("EnableBatchedOperations", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.EnableBatchedOperations)),
                description.AuthorizationRules?.Serialize(),
                new XElement(XName.Get("Status", ManagementClientConstants.ServiceBusNamespace), description.Status.ToString()),
                description.ForwardTo != null ? new XElement(XName.Get("ForwardTo", ManagementClientConstants.ServiceBusNamespace), description.ForwardTo) : null,
                description.UserMetadata != null ? new XElement(XName.Get("UserMetadata", ManagementClientConstants.ServiceBusNamespace), description.UserMetadata) : null,
                description.AutoDeleteOnIdle != TimeSpan.MaxValue ? new XElement(XName.Get("AutoDeleteOnIdle", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.AutoDeleteOnIdle)) : null,
                new XElement(XName.Get("EnablePartitioning", ManagementClientConstants.ServiceBusNamespace), XmlConvert.ToString(description.EnablePartitioning)),
                description.ForwardDeadLetteredMessagesTo != null ? new XElement(XName.Get("ForwardDeadLetteredMessagesTo", ManagementClientConstants.ServiceBusNamespace), description.ForwardDeadLetteredMessagesTo) : null
            };

            if (description.UnknownProperties != null)
            {
                queueDescriptionElements.AddRange(description.UnknownProperties);
            }

            return(new XDocument(
                       new XElement(XName.Get("entry", ManagementClientConstants.AtomNamespace),
                                    new XElement(XName.Get("content", ManagementClientConstants.AtomNamespace),
                                                 new XAttribute("type", "application/xml"),
                                                 new XElement(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace),
                                                              queueDescriptionElements.ToArray())))));
        }
        private static QueueDescription ParseFromEntryElement(XElement xEntry)
        {
            var name = xEntry.Element(XName.Get("title", ManagementClientConstants.AtomNamespace)).Value;
            var qd   = new QueueDescription(name);

            var qdXml = xEntry.Element(XName.Get("content", ManagementClientConstants.AtomNamespace))?
                        .Element(XName.Get("QueueDescription", ManagementClientConstants.ServiceBusNamespace));

            if (qdXml == null)
            {
                throw new MessagingEntityNotFoundException("Queue was not found");
            }

            foreach (var element in qdXml.Elements())
            {
                switch (element.Name.LocalName)
                {
                case "MaxSizeInMegabytes":
                    qd.MaxSizeInMB = Int64.Parse(element.Value);
                    break;

                case "RequiresDuplicateDetection":
                    qd.RequiresDuplicateDetection = Boolean.Parse(element.Value);
                    break;

                case "RequiresSession":
                    qd.RequiresSession = Boolean.Parse(element.Value);
                    break;

                case "DeadLetteringOnMessageExpiration":
                    qd.EnableDeadLetteringOnMessageExpiration = Boolean.Parse(element.Value);
                    break;

                case "DuplicateDetectionHistoryTimeWindow":
                    qd.duplicateDetectionHistoryTimeWindow = XmlConvert.ToTimeSpan(element.Value);
                    break;

                case "LockDuration":
                    qd.LockDuration = XmlConvert.ToTimeSpan(element.Value);
                    break;

                case "DefaultMessageTimeToLive":
                    qd.DefaultMessageTimeToLive = XmlConvert.ToTimeSpan(element.Value);
                    break;

                case "MaxDeliveryCount":
                    qd.MaxDeliveryCount = Int32.Parse(element.Value);
                    break;

                case "EnableBatchedOperations":
                    qd.EnableBatchedOperations = Boolean.Parse(element.Value);
                    break;

                case "Status":
                    qd.Status = (EntityStatus)Enum.Parse(typeof(EntityStatus), element.Value);
                    break;

                case "AutoDeleteOnIdle":
                    qd.AutoDeleteOnIdle = XmlConvert.ToTimeSpan(element.Value);
                    break;

                case "EnablePartitioning":
                    qd.EnablePartitioning = bool.Parse(element.Value);
                    break;

                case "UserMetadata":
                    qd.UserMetadata = element.Value;
                    break;

                case "ForwardTo":
                    if (!string.IsNullOrWhiteSpace(element.Value))
                    {
                        qd.ForwardTo = element.Value;
                    }
                    break;

                case "ForwardDeadLetteredMessagesTo":
                    if (!string.IsNullOrWhiteSpace(element.Value))
                    {
                        qd.ForwardDeadLetteredMessagesTo = element.Value;
                    }
                    break;

                case "AuthorizationRules":
                    qd.AuthorizationRules = AuthorizationRules.ParseFromXElement(element);
                    break;

                case "AccessedAt":
                case "CreatedAt":
                case "MessageCount":
                case "SizeInBytes":
                case "UpdatedAt":
                case "CountDetails":
                    // Ignore known properties
                    // Do nothing
                    break;

                default:
                    // For unknown properties, keep them as-is for forward proof.
                    if (qd.UnknownProperties == null)
                    {
                        qd.UnknownProperties = new List <object>();
                    }

                    qd.UnknownProperties.Add(element);
                    break;
                }
            }

            return(qd);
        }