/// <summary>
        /// Creates a topic and subscription if they do not exist and adds a filter to the subscription.
        /// </summary>
        /// <param name="manager">The manager.</param>
        /// <param name="topicName">Name of the topic.</param>
        /// <param name="subscriptionName">Name of the subscription.</param>
        /// <param name="filter">The SQL filter.</param>
        /// <returns><see cref="TopicDescription"/> Topic description.</returns>
        public static TopicDescription CreateTopicIfNotExists(this ManagementClient manager, string topicName, string subscriptionName, KeyValuePair <string, string>?filter)
        {
            var filters = new List <KeyValuePair <string, string> >();

            if (filter.HasValue)
            {
                filters.Add(filter.Value);
            }

            // Create topic, subscriptions and filter if not exists.
            return(manager.CreateTopicIfNotExists(topicName, subscriptionName, filters));
        }
 /// <summary>
 /// Creates a topic and subscription if either do not exist.
 /// </summary>
 /// <param name="manager">The manager.</param>
 /// <param name="topicName">Name of the topic.</param>
 /// <param name="subscriptionName">Name of the subscription.</param>
 /// <returns>Task.</returns>
 public static TopicDescription CreateTopicIfNotExists(this ManagementClient manager, string topicName, string subscriptionName)
 {
     // Create topic and subscription if not exists.
     return(manager.CreateTopicIfNotExists(topicName, subscriptionName, default(List <KeyValuePair <string, string> >)));
 }
        /// <summary>
        /// Purges the specified topic.  Purge is done by deleting and re-creating the topic - fastest way to purge.
        /// Topic will have same susbcriptions and filters re-applied after recreation if specified.
        /// </summary>
        /// <param name="manager">The manager to extend.</param>
        /// <param name="topicName">Name of the topic.</param>
        /// <param name="reapplyState">if set to <c>true</c> [reapply state] - reapplies all subscriptions and filters.</param>
        /// <returns>Task.</returns>
        /// <exception cref="ArgumentException">Cannot purge queue as it has been defined as a queue - topicName</exception>
        public static async Task PurgeTopic(this ManagementClient manager, string topicName, bool reapplyState)
        {
            try
            {
                // Get topic to begin with.
                var topic = await manager.GetTopicAsync(topicName);

                IList <SubscriptionDescription> subscriptions            = null;
                Dictionary <string, IList <RuleDescription> > allFilters = null;

                // If we need to reapply state, being by preserving information.
                if (reapplyState)
                {
                    // Preserve subscriptions and filters.
                    subscriptions = await manager.GetSubscriptionsAsync(topicName);

                    allFilters = new Dictionary <string, IList <RuleDescription> >();

                    foreach (var sub in subscriptions)
                    {
                        allFilters.Add(sub.SubscriptionName, await manager.GetRulesAsync(topicName, sub.SubscriptionName));
                    }
                }

                // Delete the topic first.
                await manager.DeleteTopicIfExists(topicName);

                if (reapplyState)
                {
                    // Create topic, all subscriptions and all filters again.
                    if (subscriptions.Count > 0)
                    {
                        foreach (var sub in subscriptions)
                        {
                            allFilters.TryGetValue(sub.SubscriptionName, out var filters);

                            List <KeyValuePair <string, string> > sqlFilters = null;

                            if (filters != null && filters.Count > 0)
                            {
                                sqlFilters = filters.Select(r => new KeyValuePair <string, string>(r.Name, ((SqlFilter)r.Filter).SqlExpression)).ToList();
                            }

                            manager.CreateTopicIfNotExists(topicName, sub.SubscriptionName, sqlFilters);
                        }
                    }
                }
                else
                {
                    // Recreate ONLY the topic if we don't need to repply state.
                    manager.CreateTopicIfNotExists(topicName);
                }
            }
            catch (Exception)
            {
                try
                {
                    // Check to see if a queue exists already with the name of the topic you are trying to create.
                    if (await manager.GetQueueAsync(topicName) != null)
                    {
                        throw new ArgumentException($"Cannot purge topic as it has been defined as a queue: {topicName}", "topicName");
                    }
                }
                catch (ArgumentException)
                {
                    throw;
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
 /// <summary>
 /// Create a topic if it does not exist.
 /// </summary>
 /// <param name="manager">The manager.</param>
 /// <param name="topicName">Name of the topic.</param>
 /// <returns><see cref="TopicDescription"/> Topic description.</returns>
 public static TopicDescription CreateTopicIfNotExists(this ManagementClient manager, string topicName)
 {
     // Setup Topic (if not exists).
     return(manager.CreateTopicIfNotExists(topicName, null, default(List <KeyValuePair <string, string> >)));
 }