コード例 #1
0
        /// <summary>
        /// Scorches the entire namespace that the <see cref="NamespaceManager"/> is connected to.
        /// Currently this wipes out all queues and topics. This is used mostly by integration tests, to guarantee that
        /// both queue and topic creation processes are in place and working as intended.
        /// </summary>
        /// <param name="nsm">The <see cref="NamespaceManager"/> we are using to scorch the namespace.</param>
        /// <returns>The async <see cref="Task"/> wrapper.</returns>
        public static async Task ScorchNamespace(this NamespaceManager nsm)
        {
            foreach (var queue in await nsm.GetQueuesAsync())
            {
                await nsm.DeleteQueueAsync(queue.Path);
            }

            foreach (var topic in await nsm.GetTopicsAsync())
            {
                await nsm.DeleteTopicAsync(topic.Path);
            }
        }
        static async Task CreateTopic(NamespaceManager namespaceManager, string name)
        {
            if (name.Equals("bundle-3"))
            {
                await namespaceManager.DeleteTopicAsync(name);
            }

            if (!await namespaceManager.TopicExistsAsync(name))
            {
                await namespaceManager.CreateTopicAsync(new TopicDescription(name));
            }
        }
コード例 #3
0
        /// <summary>
        /// Delete a Topic, if it exists, and indicate if the attempt was successful.
        /// </summary>
        public bool DeleteTopic()
        {
            string topicName   = EnvironmentType.AzureTopicName();
            bool   topicExists = TopicExists;

            if (topicExists)
            {
                _namespaceManager.DeleteTopicAsync(topicName).Wait();
                topicExists = TopicExists;
            }

            return(!topicExists);
        }
コード例 #4
0
 public Task DeleteQueueAsync(QueueName name)
 {
     if (name.IsSimpleQueue)
     {
         return(_namespaceManager.DeleteQueueAsync(name.TopicName));
     }
     else
     {
         if (name.IsTopic)
         {
             return(_namespaceManager.DeleteTopicAsync(name.TopicName));
         }
         else
         {
             return(_namespaceManager.DeleteSubscriptionAsync(name.TopicName,
                                                              name.SubscriptionName));
         }
     }
 }
コード例 #5
0
        public async Task Setup()
        {
            namespaceConnectionString = Environment.GetEnvironmentVariable("NServiceBus.AzureServiceBusForwarder.ConnectionString", EnvironmentVariableTarget.Process);
            destinationQueue = GetType().Name;
            messageForwarder = A.Fake<IMessageForwarder>();
            messageReceiver = new QueueBatchMessageReceiver(QueueClient.CreateFromConnectionString(namespaceConnectionString, destinationQueue));

            await MessageEntityHelper.CreateQueue(destinationQueue);
            
            namespaceManager = NamespaceManager.CreateFromConnectionString(namespaceConnectionString);

            if (await namespaceManager.TopicExistsAsync(TopicName))
            {
                await namespaceManager.DeleteTopicAsync(TopicName);
            }

            await namespaceManager.CreateTopicAsync(TopicName);

            forwarder = new Forwarder(
                new ForwarderConfiguration(
                    new ForwarderSourceConfiguration(500, () => messageReceiver),
                    new ForwarderDestinationConfiguration(destinationQueue, () => messageForwarder)));
        }
コード例 #6
0
 public async Task DeleteAsync()
 {
     await RetryPolicy.ExecuteAsync(() => NamespaceManager.DeleteTopicAsync(Client.Path));
 }
コード例 #7
0
        public async Task DeleteTopicAsync <T>()
        {
            var topic = await _topicRepository.Get <T>();

            await _namespaceManager.DeleteTopicAsync(topic.Path, null);
        }
コード例 #8
0
        public async Task Run(string namespaceAddress, string manageToken)
        {
            // This sample demonstrates how to use advanced filters with ServiceBus topics and subscriptions.
            // The sample creates a topic and 3 subscriptions with different filter definitions.
            // Each receiver will receive matching messages depending on the filter associated with a subscription.

            // NOTE:
            // This is primarily an example illustrating the management features related to setting up
            // Service Bus subscriptions. It is DISCOURAGED for applications to routinely set up and
            // tear down topics and subscriptions as a part of regular message processing. Managing
            // topics and subscriptions is a system configuration operation.

            // Create messaging factory and ServiceBus namespace client.
            var sharedAccessSignatureTokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken);
            var namespaceManager = new NamespaceManager(namespaceAddress, sharedAccessSignatureTokenProvider);

            Console.WriteLine("\nCreating a topic and 3 subscriptions.");

            // Create a topic and several subscriptions; clean house ahead of time
            if (await namespaceManager.TopicExistsAsync(TopicName))
            {
                await namespaceManager.DeleteTopicAsync(TopicName);
            }

            var topicDescription = await namespaceManager.CreateTopicAsync(TopicName);

            Console.WriteLine("Topic created.");

            // Create a subscription for all messages sent to topic.
            await namespaceManager.CreateSubscriptionAsync(topicDescription.Path, SubscriptionAllMessages, new TrueFilter());

            Console.WriteLine("Subscription {0} added with filter definition set to TrueFilter.", SubscriptionAllMessages);


            // Create a subscription that'll receive all orders which have color "blue" and quantity 10.

            await namespaceManager.CreateSubscriptionAsync(
                topicDescription.Path,
                SubscriptionColorBlueSize10Orders,
                new SqlFilter("color = 'blue' AND quantity = 10"));

            Console.WriteLine(
                "Subscription {0} added with filter definition \"color = 'blue' AND quantity = 10\".",
                SubscriptionColorBlueSize10Orders);

            // Create a subscription that'll receive all orders which have color "red"
            await namespaceManager.CreateSubscriptionAsync(
                topicDescription.Path,
                SubscriptionColorRed,
                new RuleDescription
            {
                Name   = "RedRule",
                Filter = new SqlFilter("color = 'red'"),
                Action = new SqlRuleAction(
                    "SET quantity = quantity / 2;" +
                    "REMOVE priority;" +
                    "SET sys.CorrelationId = 'low';")
            });

            Console.WriteLine("Subscription {0} added with filter definition \"color = 'red'\" and action definition.", SubscriptionColorRed);

            // Create a subscription that'll receive all high priority orders.
            namespaceManager.CreateSubscription(topicDescription.Path, SubscriptionHighPriorityOrders,
                                                new CorrelationFilter {
                Label = "red", CorrelationId = "high"
            });
            Console.WriteLine("Subscription {0} added with correlation filter definition \"high\".", SubscriptionHighPriorityOrders);

            Console.WriteLine("Create completed.");


            await this.SendAndReceiveTestsAsync(namespaceAddress, sharedAccessSignatureTokenProvider);


            Console.WriteLine("Press [Enter] to quit...");
            Console.ReadLine();

            Console.WriteLine("\nDeleting topic and subscriptions from previous run if any.");

            try
            {
                namespaceManager.DeleteTopic(TopicName);
            }
            catch (MessagingEntityNotFoundException)
            {
                Console.WriteLine("No topic found to delete.");
            }

            Console.WriteLine("Delete completed.");
        }
コード例 #9
0
        public async Task Run(string namespaceAddress, string manageToken)
        {
            // This sample demonstrates how to use advanced filters with ServiceBus topics and subscriptions.
            // The sample creates a topic and 3 subscriptions with different filter definitions.
            // Each receiver will receive matching messages depending on the filter associated with a subscription.

            // NOTE:
            // This is primarily an example illustrating the management features related to setting up 
            // Service Bus subscriptions. It is DISCOURAGED for applications to routinely set up and 
            // tear down topics and subscriptions as a part of regular message processing. Managing 
            // topics and subscriptions is a system configuration operation. 

            // Create messaging factory and ServiceBus namespace client.
            var sharedAccessSignatureTokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken);
            var namespaceManager = new NamespaceManager(namespaceAddress, sharedAccessSignatureTokenProvider);

            Console.WriteLine("\nCreating a topic and 3 subscriptions.");

            // Create a topic and several subscriptions; clean house ahead of time
            if (await namespaceManager.TopicExistsAsync(TopicName))
            {
                await namespaceManager.DeleteTopicAsync(TopicName);
            }

            var topicDescription = await namespaceManager.CreateTopicAsync(TopicName);
            Console.WriteLine("Topic created.");

            // Create a subscription for all messages sent to topic.
            await namespaceManager.CreateSubscriptionAsync(topicDescription.Path, SubscriptionAllMessages, new TrueFilter());
            Console.WriteLine("Subscription {0} added with filter definition set to TrueFilter.", SubscriptionAllMessages);
            

            // Create a subscription that'll receive all orders which have color "blue" and quantity 10.

            await namespaceManager.CreateSubscriptionAsync(
                topicDescription.Path,
                SubscriptionColorBlueSize10Orders,
                new SqlFilter("color = 'blue' AND quantity = 10"));
            Console.WriteLine(
                "Subscription {0} added with filter definition \"color = 'blue' AND quantity = 10\".",
                 SubscriptionColorBlueSize10Orders);

            // Create a subscription that'll receive all orders which have color "red"
            await namespaceManager.CreateSubscriptionAsync(
                topicDescription.Path,
                SubscriptionColorRed,
                new RuleDescription
                {
                    Name = "RedRule",
                    Filter = new SqlFilter("color = 'red'"),
                    Action = new SqlRuleAction(
                        "SET quantity = quantity / 2;" +
                        "REMOVE priority;" +
                        "SET sys.CorrelationId = 'low';")
                });
            Console.WriteLine("Subscription {0} added with filter definition \"color = 'red'\" and action definition.", SubscriptionColorRed);
     
            // Create a subscription that'll receive all high priority orders.
            namespaceManager.CreateSubscription(topicDescription.Path, SubscriptionHighPriorityOrders, 
                new CorrelationFilter { Label = "red", CorrelationId = "high"});
            Console.WriteLine("Subscription {0} added with correlation filter definition \"high\".", SubscriptionHighPriorityOrders);
     
            Console.WriteLine("Create completed.");


            await this.SendAndReceiveTestsAsync(namespaceAddress, sharedAccessSignatureTokenProvider);


            Console.WriteLine("Press [Enter] to quit...");
            Console.ReadLine();

            Console.WriteLine("\nDeleting topic and subscriptions from previous run if any.");

            try
            {
                namespaceManager.DeleteTopic(TopicName);
            }
            catch (MessagingEntityNotFoundException)
            {
                Console.WriteLine("No topic found to delete.");
            }

            Console.WriteLine("Delete completed.");
        }
コード例 #10
0
        public async Task Run(string namespaceAddress, string manageToken)
        {
            Console.WriteLine("\nCreating topology\n");
            this.sharedAccessRuleKey = SharedAccessAuthorizationRule.GenerateRandomKey();
            var namespaceManageTokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken);

            // Create namespace manager and create destination queue with a SAS rule that allows sending to that queue.
            var namespaceManager = new NamespaceManager(namespaceAddress, namespaceManageTokenProvider);

            var targetQueue = new QueueDescription("TargetQueue")
            {
                Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) },
            };

            targetQueue = (await namespaceManager.QueueExistsAsync(targetQueue.Path))
                ? await namespaceManager.UpdateQueueAsync(targetQueue)
                : await namespaceManager.CreateQueueAsync(targetQueue);

            var topic = new TopicDescription("SourceTopic")
            {
                Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) }
            };

            topic = (await namespaceManager.TopicExistsAsync(topic.Path))
                ? await namespaceManager.UpdateTopicAsync(topic)
                : await namespaceManager.CreateTopicAsync(topic);

            var forwardingSubscription = namespaceManager.CreateSubscription(
                new SubscriptionDescription(topic.Path, "Sub1")
            {
                ForwardTo = targetQueue.Path
            });

            var forwardingQueue = new QueueDescription("SourceQueue")
            {
                ForwardTo     = targetQueue.Path,
                Authorization =
                {
                    new SharedAccessAuthorizationRule(
                        "SendKey",
                        this.sharedAccessRuleKey,
                        new[] { AccessRights.Send })
                }
            };

            forwardingQueue = (await namespaceManager.QueueExistsAsync(forwardingQueue.Path))
                ? await namespaceManager.UpdateQueueAsync(forwardingQueue)
                : await namespaceManager.CreateQueueAsync(forwardingQueue);


            Console.WriteLine("\nSending messages\n");

            var topicFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey));
            var topicSender  = await topicFactory.CreateMessageSenderAsync(topic.Path);

            await topicSender.SendAsync(CreateMessage("M1"));

            var queueFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey));
            var queueSender  = await queueFactory.CreateMessageSenderAsync(forwardingQueue.Path);

            await queueSender.SendAsync(CreateMessage("M1"));


            var messagingFactory    = MessagingFactory.Create(namespaceAddress, namespaceManageTokenProvider);
            var targetQueueReceiver = messagingFactory.CreateQueueClient(targetQueue.Path);

            while (true)
            {
                var message = await targetQueueReceiver.ReceiveAsync(TimeSpan.FromSeconds(10));

                if (message != null)
                {
                    await this.PrintReceivedMessage(message);

                    await message.CompleteAsync();
                }
                else
                {
                    break;
                }
            }
            await targetQueueReceiver.CloseAsync();

            Console.WriteLine("\nPress ENTER to delete topics and exit\n");
            Console.ReadLine();
            messagingFactory.Close();
            Task.WaitAll(
                namespaceManager.DeleteQueueAsync(targetQueue.Path),
                namespaceManager.DeleteQueueAsync(forwardingQueue.Path),
                namespaceManager.DeleteTopicAsync(topic.Path));
        }
コード例 #11
0
		public async Task Delete()
		{
			var namespaceManager = new NamespaceManager(_address, _settings.TokenProvider);
			await namespaceManager.DeleteSubscriptionAsync(TopicName, SubscriptionName);
			await namespaceManager.DeleteTopicAsync(TopicName);
		}
コード例 #12
0
ファイル: Program.cs プロジェクト: pravinrest/azure
        public async Task Run(string namespaceAddress, string manageToken)
        {
            // Create the Topic / Subscription entities
            var tokenProvider    = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken);
            var namespaceManager = new NamespaceManager(namespaceAddress, tokenProvider);
            var topicDescription = new TopicDescription(TopicName);

            // Delete the topic if it already exists before creation.
            if (await namespaceManager.TopicExistsAsync(topicDescription.Path))
            {
                await namespaceManager.DeleteTopicAsync(topicDescription.Path);
            }
            await namespaceManager.CreateTopicAsync(topicDescription);

            await Task.WhenAll(
                // this sub receives messages for Priority = 1
                namespaceManager.CreateSubscriptionAsync(
                    new SubscriptionDescription(TopicName, "Priority1Subscription"),
                    new RuleDescription(new SqlFilter("Priority = 1"))),
                // this sub receives messages for Priority = 2
                namespaceManager.CreateSubscriptionAsync(
                    new SubscriptionDescription(TopicName, "Priority2Subscription"),
                    new RuleDescription(new SqlFilter("Priority = 2"))),
                // this sub receives messages for Priority Less than 2
                namespaceManager.CreateSubscriptionAsync(
                    new SubscriptionDescription(TopicName, "PriorityLessThan2Subscription"),
                    new RuleDescription(new SqlFilter("Priority > 2")))
                );


            // Start senders and receivers:
            Console.WriteLine("\nLaunching senders and receivers...");

            //send messages to topic
            var messagingFactory = MessagingFactory.Create(namespaceAddress, tokenProvider);

            var topicClient = messagingFactory.CreateTopicClient(TopicName);

            Console.WriteLine("Preparing to send messages to {0}...", topicClient.Path);

            // Send messages to queue:
            Console.WriteLine("Sending messages to topic {0}", topicClient.Path);

            var rand = new Random();

            for (var i = 0; i < 100; ++i)
            {
                var msg = new BrokeredMessage()
                {
                    TimeToLive = TimeSpan.FromMinutes(2),
                    Properties =
                    {
                        { "Priority", rand.Next(1, 4) }
                    }
                };

                await topicClient.SendAsync(msg);

                this.OutputMessageInfo("Sent: ", msg);
            }

            Console.WriteLine();


            // All messages sent
            Console.WriteLine("\nSender complete. Press ENTER");
            Console.ReadLine();

            // start receive
            Console.WriteLine("Receiving messages by priority ...");
            var subClient1 = messagingFactory.CreateSubscriptionClient(
                TopicName,
                new SubscriptionDescription(TopicName, "Priority1Subscription").Name,
                ReceiveMode.ReceiveAndDelete);
            var subClient2 = messagingFactory.CreateSubscriptionClient(
                TopicName,
                new SubscriptionDescription(TopicName, "Priority2Subscription").Name,
                ReceiveMode.ReceiveAndDelete);
            var subClient3 = messagingFactory.CreateSubscriptionClient(
                TopicName,
                new SubscriptionDescription(TopicName, "PriorityLessThan2Subscription").Name,
                ReceiveMode.ReceiveAndDelete);

            while (true)
            {
                try
                {
                    // Please see the README.md file regarding this loop and
                    // the handling strategy below.
                    var message = await subClient1.ReceiveAsync(TimeSpan.Zero) ??
                                  (await subClient2.ReceiveAsync(TimeSpan.Zero) ??
                                   await subClient3.ReceiveAsync(TimeSpan.Zero));

                    if (message != null)
                    {
                        this.OutputMessageInfo("Received: ", message);
                    }
                    else
                    {
                        break;
                    }
                }
                catch (MessageNotFoundException)
                {
                    Console.WriteLine("Got MessageNotFoundException, waiting for messages to be available");
                }
                catch (MessagingException e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }

            Console.WriteLine("\nReceiver complete. press ENTER");
            Console.ReadLine();

            // Cleanup:
            namespaceManager.DeleteTopic(TopicName);
        }
コード例 #13
0
        public async Task Run(string namespaceAddress, string manageToken)
        {
            Console.WriteLine("\nCreating topology\n");
            this.sharedAccessRuleKey = SharedAccessAuthorizationRule.GenerateRandomKey();
            var namespaceManageTokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken);

            // Create namespace manager and create destination queue with a SAS rule that allows sending to that queue.
            var namespaceManager = new NamespaceManager(namespaceAddress, namespaceManageTokenProvider);

            var targetQueue = new QueueDescription("TargetQueue")
            {
                Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) },
            };

            targetQueue = (await namespaceManager.QueueExistsAsync(targetQueue.Path))
                ? await namespaceManager.UpdateQueueAsync(targetQueue)
                : await namespaceManager.CreateQueueAsync(targetQueue);

            var topic = new TopicDescription("SourceTopic")
            {
                Authorization = { new SharedAccessAuthorizationRule("SendKey", this.sharedAccessRuleKey, new[] { AccessRights.Send }) }
            };
            topic = (await namespaceManager.TopicExistsAsync(topic.Path))
                ? await namespaceManager.UpdateTopicAsync(topic)
                : await namespaceManager.CreateTopicAsync(topic);
            var forwardingSubscription = namespaceManager.CreateSubscription(
                new SubscriptionDescription(topic.Path, "Sub1")
                {
                    ForwardTo = targetQueue.Path
                });

            var forwardingQueue = new QueueDescription("SourceQueue")
            {
                ForwardTo = targetQueue.Path,
                Authorization =
                {
                    new SharedAccessAuthorizationRule(
                        "SendKey",
                        this.sharedAccessRuleKey,
                        new[] {AccessRights.Send})
                }
            };
            forwardingQueue = (await namespaceManager.QueueExistsAsync(forwardingQueue.Path))
                ? await namespaceManager.UpdateQueueAsync(forwardingQueue)
                : await namespaceManager.CreateQueueAsync(forwardingQueue);


            Console.WriteLine("\nSending messages\n");

            var topicFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey));
            var topicSender = await topicFactory.CreateMessageSenderAsync(topic.Path);
            await topicSender.SendAsync(CreateMessage("M1"));

            var queueFactory = MessagingFactory.Create(namespaceAddress, TokenProvider.CreateSharedAccessSignatureTokenProvider("SendKey", this.sharedAccessRuleKey));
            var queueSender = await queueFactory.CreateMessageSenderAsync(forwardingQueue.Path);
            await queueSender.SendAsync(CreateMessage("M1"));


            var messagingFactory = MessagingFactory.Create(namespaceAddress, namespaceManageTokenProvider);
            var targetQueueReceiver = messagingFactory.CreateQueueClient(targetQueue.Path);
            while (true)
            {
                var message = await targetQueueReceiver.ReceiveAsync(TimeSpan.FromSeconds(10));
                if (message != null)
                {
                    await this.PrintReceivedMessage(message);
                    await message.CompleteAsync();
                }
                else
                {
                    break;
                }
            }
            await targetQueueReceiver.CloseAsync();

            Console.WriteLine("\nPress ENTER to delete topics and exit\n");
            Console.ReadLine();
            messagingFactory.Close();
            Task.WaitAll(
                namespaceManager.DeleteQueueAsync(targetQueue.Path),
                namespaceManager.DeleteQueueAsync(forwardingQueue.Path),
                namespaceManager.DeleteTopicAsync(topic.Path));
        }
コード例 #14
0
        static async Task DeleteEntities(NamespaceManager namespaceManager)
        {
            var queryQueues = namespaceManager.GetQueuesAsync();
            var queryTopics = namespaceManager.GetTopicsAsync();
            await Task.WhenAll(queryQueues, queryTopics)
            .ConfigureAwait(false);

            const int maxRetryAttempts = 5;
            var       deleteQueues     = (await queryQueues).Select(queueDescription => TryWithRetries(queueDescription.Path, namespaceManager.DeleteQueueAsync(queueDescription.Path), maxRetryAttempts));

            await Task.WhenAll(deleteQueues)
            .ConfigureAwait(false);

            var deleteTopics = (await queryTopics).Select(topicDescription => TryWithRetries(topicDescription.Path, namespaceManager.DeleteTopicAsync(topicDescription.Path), maxRetryAttempts));
            await Task.WhenAll(deleteTopics)
            .ConfigureAwait(false);
        }
コード例 #15
0
        public async Task Run(string namespaceAddress, string manageToken)
        {
            // Create the Topic / Subscription entities 
            var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(manageToken);
            var namespaceManager = new NamespaceManager(namespaceAddress, tokenProvider);
            var topicDescription = new TopicDescription(TopicName);

            // Delete the topic if it already exists before creation. 
            if (await namespaceManager.TopicExistsAsync(topicDescription.Path))
            {
                await namespaceManager.DeleteTopicAsync(topicDescription.Path);
            }
            await namespaceManager.CreateTopicAsync(topicDescription);
            await Task.WhenAll(
                // this sub receives messages for Priority = 1
                namespaceManager.CreateSubscriptionAsync(
                    new SubscriptionDescription(TopicName, "Priority1Subscription"),
                    new RuleDescription(new SqlFilter("Priority = 1"))),
                // this sub receives messages for Priority = 2
                namespaceManager.CreateSubscriptionAsync(
                    new SubscriptionDescription(TopicName, "Priority2Subscription"),
                    new RuleDescription(new SqlFilter("Priority = 2"))),
                // this sub receives messages for Priority Less than 2
                namespaceManager.CreateSubscriptionAsync(
                    new SubscriptionDescription(TopicName, "PriorityLessThan2Subscription"),
                    new RuleDescription(new SqlFilter("Priority > 2")))
                );


            // Start senders and receivers:
            Console.WriteLine("\nLaunching senders and receivers...");

            //send messages to topic            
            var messagingFactory = MessagingFactory.Create(namespaceAddress, tokenProvider);

            var topicClient = messagingFactory.CreateTopicClient(TopicName);

            Console.WriteLine("Preparing to send messages to {0}...", topicClient.Path);

            // Send messages to queue:
            Console.WriteLine("Sending messages to topic {0}", topicClient.Path);

            var rand = new Random();
            for (var i = 0; i < 100; ++i)
            {
                var msg = new BrokeredMessage()
                {
                    TimeToLive = TimeSpan.FromMinutes(2),
                    Properties =
                    {
                        { "Priority", rand.Next(1, 4) }
                    }
                };

                await topicClient.SendAsync(msg);

                this.OutputMessageInfo("Sent: ", msg);
            }

            Console.WriteLine();


            // All messages sent
            Console.WriteLine("\nSender complete. Press ENTER");
            Console.ReadLine();

            // start receive
            Console.WriteLine("Receiving messages by priority ...");
            var subClient1 = messagingFactory.CreateSubscriptionClient(
                TopicName,
                new SubscriptionDescription(TopicName, "Priority1Subscription").Name,
                ReceiveMode.ReceiveAndDelete);
            var subClient2 = messagingFactory.CreateSubscriptionClient(
                TopicName,
                new SubscriptionDescription(TopicName, "Priority2Subscription").Name,
                ReceiveMode.ReceiveAndDelete);
            var subClient3 = messagingFactory.CreateSubscriptionClient(
                TopicName,
                new SubscriptionDescription(TopicName, "PriorityLessThan2Subscription").Name,
                ReceiveMode.ReceiveAndDelete);

            while (true)
            {
                try
                {
                    // Please see the README.md file regarding this loop and 
                    // the handling strategy below. 
                    var message = await subClient1.ReceiveAsync(TimeSpan.Zero) ??
                                  (await subClient2.ReceiveAsync(TimeSpan.Zero) ?? 
                                   await subClient3.ReceiveAsync(TimeSpan.Zero));

                    if (message != null)
                    {
                        this.OutputMessageInfo("Received: ", message);
                    }
                    else
                    {
                        break;
                    }
                }
                catch (MessageNotFoundException)
                {
                    Console.WriteLine("Got MessageNotFoundException, waiting for messages to be available");
                }
                catch (MessagingException e)
                {
                    Console.WriteLine("Error: " + e.Message);
                }
            }

            Console.WriteLine("\nReceiver complete. press ENTER");
            Console.ReadLine();

            // Cleanup:
            namespaceManager.DeleteTopic(TopicName);
        }
コード例 #16
0
 public async Task DeleteTopicAsync(string name)
 {
     var topicName = CreateTopicName(name);
     await _manager.DeleteTopicAsync(topicName);
 }
 public Task DeleteTopic(string path)
 {
     return(manager.DeleteTopicAsync(path));
 }