Пример #1
0
        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 ServiceBusManagementClient(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 ThrowsIfEntityDoesNotExist()
        {
            var client = new ServiceBusManagementClient(TestEnvironment.ServiceBusConnectionString);

            Assert.That(
                async() =>
                await client.GetQueueAsync("NonExistingPath"),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.GetQueueAsync("NonExistingTopic"),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.GetSubscriptionAsync("NonExistingTopic", "NonExistingPath"),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.UpdateQueueAsync(new QueueProperties("NonExistingPath")),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.UpdateTopicAsync(new TopicProperties("NonExistingPath")),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.UpdateSubscriptionAsync(new SubscriptionProperties("NonExistingTopic", "NonExistingPath")),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.DeleteQueueAsync("NonExistingPath"),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.DeleteTopicAsync("NonExistingPath"),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.DeleteSubscriptionAsync("NonExistingTopic", "NonExistingPath"),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));


            var queueName = Guid.NewGuid().ToString("D").Substring(0, 8);
            var topicName = Guid.NewGuid().ToString("D").Substring(0, 8);

            await client.CreateQueueAsync(queueName);

            await client.CreateTopicAsync(topicName);

            Assert.That(
                async() =>
                await client.GetQueueAsync(topicName),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            Assert.That(
                async() =>
                await client.GetTopicAsync(queueName),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            await client.DeleteQueueAsync(queueName);

            await client.DeleteTopicAsync(topicName);
        }
        public async Task BasicSubscriptionCrudOperations()
        {
            var topicName        = nameof(BasicSubscriptionCrudOperations).ToLower() + Guid.NewGuid().ToString("D").Substring(0, 8);
            var subscriptionName = Guid.NewGuid().ToString("D").Substring(0, 8);

            var client = new ServiceBusManagementClient(TestEnvironment.ServiceBusConnectionString);

            await client.CreateTopicAsync(topicName);

            var options = new CreateSubscriptionOptions(topicName, subscriptionName)
            {
                AutoDeleteOnIdle                 = TimeSpan.FromHours(1),
                DefaultMessageTimeToLive         = TimeSpan.FromDays(2),
                DeadLetteringOnMessageExpiration = true,
                EnableBatchedOperations          = false,
                ForwardDeadLetteredMessagesTo    = null,
                ForwardTo        = null,
                LockDuration     = TimeSpan.FromSeconds(45),
                MaxDeliveryCount = 8,
                RequiresSession  = true,
                UserMetadata     = nameof(BasicSubscriptionCrudOperations)
            };

            SubscriptionProperties createdSubscription = await client.CreateSubscriptionAsync(options);

            Assert.AreEqual(options, new CreateSubscriptionOptions(createdSubscription));

            SubscriptionProperties getSubscription = await client.GetSubscriptionAsync(options.TopicName, options.SubscriptionName);

            Assert.AreEqual(options, new CreateSubscriptionOptions(getSubscription));

            getSubscription.DefaultMessageTimeToLive = TimeSpan.FromDays(3);
            getSubscription.MaxDeliveryCount         = 9;

            SubscriptionProperties updatedSubscription = await client.UpdateSubscriptionAsync(getSubscription);

            Assert.AreEqual(getSubscription, updatedSubscription);

            bool exists = await client.SubscriptionExistsAsync(topicName, subscriptionName);

            Assert.True(exists);

            List <SubscriptionProperties> subscriptionList = new List <SubscriptionProperties>();

            await foreach (SubscriptionProperties subscription in client.GetSubscriptionsAsync(topicName))
            {
                subscriptionList.Add(subscription);
            }
            subscriptionList = subscriptionList.Where(e => e.TopicName.StartsWith(nameof(BasicSubscriptionCrudOperations).ToLower())).ToList();
            Assert.True(subscriptionList.Count == 1, $"Expected 1 subscription but {subscriptionList.Count} subscriptions returned");
            Assert.AreEqual(subscriptionList.First().TopicName, topicName);
            Assert.AreEqual(subscriptionList.First().SubscriptionName, subscriptionName);

            await client.DeleteSubscriptionAsync(options.TopicName, options.SubscriptionName);

            Assert.That(
                async() =>
                await client.GetSubscriptionAsync(options.TopicName, options.SubscriptionName),
                Throws.InstanceOf <ServiceBusException>().And.Property(nameof(ServiceBusException.Reason)).EqualTo(ServiceBusException.FailureReason.MessagingEntityNotFound));

            await client.DeleteTopicAsync(options.TopicName);

            exists = await client.SubscriptionExistsAsync(topicName, subscriptionName);

            Assert.False(exists);
        }