private static async Task <ServiceBusAdministrationClient> Cleanup(string connectionString, string inputQueue,
                                                                           string topicName, string rushSubscription, string currencySubscription)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            if (await client.SubscriptionExistsAsync(topicName, rushSubscription))
            {
                await client.DeleteSubscriptionAsync(topicName, rushSubscription);
            }

            if (await client.SubscriptionExistsAsync(topicName, currencySubscription))
            {
                await client.DeleteSubscriptionAsync(topicName, currencySubscription);
            }

            if (await client.TopicExistsAsync(topicName))
            {
                await client.DeleteTopicAsync(topicName);
            }

            var topicDescription = new CreateTopicOptions(topicName);
            await client.CreateTopicAsync(topicDescription);

            if (await client.QueueExistsAsync(inputQueue))
            {
                await client.DeleteQueueAsync(inputQueue);
            }

            var queueDescription = new CreateQueueOptions(inputQueue);
            await client.CreateQueueAsync(queueDescription);

            return(client);
        }
        public async Task DisposeAsync()
        {
            var adminClient = new ServiceBusAdministrationClient(_fixture.Configuration.ConnectionString);

            await adminClient.DeleteSubscriptionAsync(_topicName, _subscriptionName);

            await adminClient.DeleteTopicAsync(_topicName);
        }
示例#3
0
        public async Task DisposeAsync()
        {
            var adminClient = new ServiceBusAdministrationClient(_sbFixture.Configuration.ConnectionString);

            await foreach (var subscription in adminClient.GetSubscriptionsAsync(_topicName))
            {
                await adminClient.DeleteSubscriptionAsync(_topicName, subscription.SubscriptionName);
            }
            await adminClient.DeleteTopicAsync(_topicName);
        }
        public async Task GetUpdateDeleteTopicAndSubscription()
        {
            string topicName           = Guid.NewGuid().ToString("D").Substring(0, 8);
            string subscriptionName    = Guid.NewGuid().ToString("D").Substring(0, 8);
            string connectionString    = TestEnvironment.ServiceBusConnectionString;
            var    client              = new ServiceBusAdministrationClient(connectionString);
            var    topicOptions        = new CreateTopicOptions(topicName);
            var    subscriptionOptions = new CreateSubscriptionOptions(topicName, subscriptionName);
            await client.CreateTopicAsync(topicOptions);

            await client.CreateSubscriptionAsync(subscriptionOptions);

            #region Snippet:GetTopic
            TopicProperties topic = await client.GetTopicAsync(topicName);

            #endregion
            #region Snippet:GetSubscription
            SubscriptionProperties subscription = await client.GetSubscriptionAsync(topicName, subscriptionName);

            #endregion
            #region Snippet:UpdateTopic
            topic.UserMetadata = "some metadata";
            TopicProperties updatedTopic = await client.UpdateTopicAsync(topic);

            #endregion
            Assert.AreEqual("some metadata", updatedTopic.UserMetadata);

            #region Snippet:UpdateSubscription
            subscription.UserMetadata = "some metadata";
            SubscriptionProperties updatedSubscription = await client.UpdateSubscriptionAsync(subscription);

            #endregion
            Assert.AreEqual("some metadata", updatedSubscription.UserMetadata);

            // need to delete the subscription before the topic, as deleting
            // the topic would automatically delete the subscription
            #region Snippet:DeleteSubscription
            await client.DeleteSubscriptionAsync(topicName, subscriptionName);

            #endregion
            Assert.That(
                async() =>
                await client.GetSubscriptionAsync(topicName, subscriptionName),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusFailureReason.MessagingEntityNotFound));

            #region Snippet:DeleteTopic
            await client.DeleteTopicAsync(topicName);

            #endregion
            Assert.That(
                async() =>
                await client.GetTopicAsync(topicName),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusFailureReason.MessagingEntityNotFound));
        }
        public async Task CreateTopicAndSubscription()
        {
            string topicName        = Guid.NewGuid().ToString("D").Substring(0, 8);
            string subscriptionName = Guid.NewGuid().ToString("D").Substring(0, 8);
            string connectionString = TestEnvironment.ServiceBusConnectionString;
            var    client           = new ServiceBusAdministrationClient(connectionString);

            try
            {
                #region Snippet:CreateTopicAndSubscription
                //@@ string connectionString = "<connection_string>";
                //@@ string topicName = "<topic_name>";
                //@@ var client = new ServiceBusManagementClient(connectionString);
                var topicOptions = new CreateTopicOptions(topicName)
                {
                    AutoDeleteOnIdle                    = TimeSpan.FromDays(7),
                    DefaultMessageTimeToLive            = TimeSpan.FromDays(2),
                    DuplicateDetectionHistoryTimeWindow = TimeSpan.FromMinutes(1),
                    EnableBatchedOperations             = true,
                    EnablePartitioning                  = false,
                    MaxSizeInMegabytes                  = 2048,
                    RequiresDuplicateDetection          = true,
                    UserMetadata = "some metadata"
                };

                topicOptions.AuthorizationRules.Add(new SharedAccessAuthorizationRule(
                                                        "allClaims",
                                                        new[] { AccessRights.Manage, AccessRights.Send, AccessRights.Listen }));

                TopicProperties createdTopic = await client.CreateTopicAsync(topicOptions);

                //@@ string subscriptionName = "<subscription_name>";
                var subscriptionOptions = new CreateSubscriptionOptions(topicName, subscriptionName)
                {
                    AutoDeleteOnIdle         = TimeSpan.FromDays(7),
                    DefaultMessageTimeToLive = TimeSpan.FromDays(2),
                    EnableBatchedOperations  = true,
                    UserMetadata             = "some metadata"
                };
                SubscriptionProperties createdSubscription = await client.CreateSubscriptionAsync(subscriptionOptions);

                #endregion
                Assert.AreEqual(topicOptions, new CreateTopicOptions(createdTopic));
                Assert.AreEqual(subscriptionOptions, new CreateSubscriptionOptions(createdSubscription));
            }
            finally
            {
                await client.DeleteTopicAsync(topicName);
            }
        }
        /// <summary>
        /// Delete a Topic.
        /// </summary>
        /// <param name="topic">The name of the Topic.</param>
        public async Task DeleteTopicAsync(string topic)
        {
            s_logger.LogInformation("Deleting topic {Topic}...", topic);
            try
            {
                await _administrationClient.DeleteTopicAsync(topic);

                s_logger.LogInformation("Topic {Topic} successfully deleted", topic);
            }
            catch (Exception e)
            {
                s_logger.LogError(e, "Failed to delete Topic {Topic}", topic);
            }
        }
示例#7
0
        private void DeleteServiceBusTopic(string topic)
        {
            var busAdmin = new ServiceBusAdministrationClient(Connections.ServiceBusConnectionString);

            busAdmin.DeleteTopicAsync(topic).Wait();
        }
示例#8
0
        private static async Task RunAsync(string fullyQualifiedNamespace, string connection)
        {
            if (!string.IsNullOrEmpty(connection))
            {
                s_client      = new ServiceBusClient(Environment.GetEnvironmentVariable(connection));
                s_adminClient = new ServiceBusAdministrationClient(Environment.GetEnvironmentVariable(connection));
            }
            else if (!string.IsNullOrEmpty(fullyQualifiedNamespace))
            {
                var defaultAzureCredential = new DefaultAzureCredential();
                s_client      = new ServiceBusClient(fullyQualifiedNamespace, defaultAzureCredential);
                s_adminClient = new ServiceBusAdministrationClient(fullyQualifiedNamespace, defaultAzureCredential);
            }
            else
            {
                throw new ArgumentException(
                          "Either a fully qualified namespace or a connection string environment variable must be specified.");
            }

            Console.WriteLine($"Creating topic {TopicName}");
            await s_adminClient.CreateTopicAsync(TopicName);

            s_sender = s_client.CreateSender(TopicName);

            // First Subscription is already created with default rule. Leave as is.
            Console.WriteLine($"Creating subscription {NoFilterSubscriptionName}");
            await s_adminClient.CreateSubscriptionAsync(TopicName, NoFilterSubscriptionName);

            Console.WriteLine($"SubscriptionName: {NoFilterSubscriptionName}, Removing and re-adding Default Rule");
            await s_adminClient.DeleteRuleAsync(TopicName, NoFilterSubscriptionName, RuleProperties.DefaultRuleName);

            await s_adminClient.CreateRuleAsync(TopicName, NoFilterSubscriptionName,
                                                new CreateRuleOptions(RuleProperties.DefaultRuleName, new TrueRuleFilter()));

            // 2nd Subscription: Add SqlFilter on Subscription 2
            // In this scenario, rather than deleting the default rule after creating the subscription,
            // we will create the subscription along with our desired rule in a single operation.
            // See https://docs.microsoft.com/en-us/azure/service-bus-messaging/topic-filters to learn more about topic filters.
            Console.WriteLine($"Creating subscription {SqlFilterOnlySubscriptionName}");
            await s_adminClient.CreateSubscriptionAsync(
                new CreateSubscriptionOptions(TopicName, SqlFilterOnlySubscriptionName),
                new CreateRuleOptions { Name = "RedSqlRule", Filter = new SqlRuleFilter("Color = 'Red'") });

            // 3rd Subscription: Add the SqlFilter Rule and Action
            // See https://docs.microsoft.com/en-us/azure/service-bus-messaging/topic-filters#actions to learn more about actions.
            Console.WriteLine($"Creating subscription {SqlFilterWithActionSubscriptionName}");
            await s_adminClient.CreateSubscriptionAsync(
                new CreateSubscriptionOptions(TopicName, SqlFilterWithActionSubscriptionName),
                new CreateRuleOptions
            {
                Name   = "BlueSqlRule",
                Filter = new SqlRuleFilter("Color = 'Blue'"),
                Action = new SqlRuleAction("SET Color = 'BlueProcessed'")
            });

            // 4th Subscription: Add Correlation Filter on Subscription 4
            Console.WriteLine($"Creating subscription {CorrelationFilterSubscriptionName}");
            await s_adminClient.CreateSubscriptionAsync(
                new CreateSubscriptionOptions(TopicName, CorrelationFilterSubscriptionName),
                new CreateRuleOptions
            {
                Name   = "ImportantCorrelationRule",
                Filter = new CorrelationRuleFilter {
                    Subject = "Red", CorrelationId = "important"
                }
            });

            // Get Rules on Subscription, called here only for one subscription as example
            var rules = s_adminClient.GetRulesAsync(TopicName, CorrelationFilterSubscriptionName);

            await foreach (var rule in rules)
            {
                Console.WriteLine(
                    $"GetRules:: SubscriptionName: {CorrelationFilterSubscriptionName}, CorrelationFilter Name: {rule.Name}, Rule: {rule.Filter}");
            }

            // Send messages to Topic
            await SendMessagesAsync();

            // Receive messages from 'NoFilterSubscription'. Should receive all 9 messages
            await ReceiveMessagesAsync(NoFilterSubscriptionName);

            // Receive messages from 'SqlFilterOnlySubscription'. Should receive all messages with Color = 'Red' i.e 3 messages
            await ReceiveMessagesAsync(SqlFilterOnlySubscriptionName);

            // Receive messages from 'SqlFilterWithActionSubscription'. Should receive all messages with Color = 'Blue'
            // i.e 3 messages AND all messages should have color set to 'BlueProcessed'
            await ReceiveMessagesAsync(SqlFilterWithActionSubscriptionName);

            // Receive messages from 'CorrelationFilterSubscription'. Should receive all messages  with Color = 'Red' and CorrelationId = "important"
            // i.e 1 message
            await ReceiveMessagesAsync(CorrelationFilterSubscriptionName);

            Console.ResetColor();

            Console.WriteLine("=======================================================================");
            Console.WriteLine("Completed Receiving all messages. Disposing clients and deleting topic.");
            Console.WriteLine("=======================================================================");

            Console.WriteLine("Disposing sender");
            await s_sender.CloseAsync();

            Console.WriteLine("Disposing client");
            await s_client.DisposeAsync();

            Console.WriteLine("Deleting topic");

            // Deleting the topic will handle deleting all the subscriptions as well.
            await s_adminClient.DeleteTopicAsync(TopicName);
        }
示例#9
0
        public static async Task <Azure.Response> DeleteTopicAsync(string topicName, string connectionString)
        {
            var client = new ServiceBusAdministrationClient(connectionString);

            return(await client.DeleteTopicAsync(topicName));
        }
示例#10
0
        public static async Task DeleteTopic(string connectionString, string topic)
        {
            var sbClient = new ServiceBusAdministrationClient(connectionString);

            _ = await sbClient.DeleteTopicAsync(topic);
        }