コード例 #1
0
        async Task ReceiveAllMessageFromSubscription(string connectionString, string subsName)
        {
            var receivedMessages = 0;

            // Create subscription client.
            var subscriptionClient = new Microsoft.Azure.ServiceBus.Core.MessageReceiver(connectionString, EntityNameHelper.FormatSubscriptionPath(TopicName, subsName), ReceiveMode.ReceiveAndDelete);

            // Create a receiver from the subscription client and receive all messages.
            Console.WriteLine("\nReceiving messages from subscription {0}.", subsName);


            while (true)
            {
                var receivedMessage = await subscriptionClient.ReceiveAsync(TimeSpan.FromSeconds(10));

                if (receivedMessage != null)
                {
                    foreach (var prop in receivedMessage.UserProperties)
                    {
                        Console.Write("{0}={1},", prop.Key, prop.Value);
                    }
                    Console.WriteLine("CorrelationId={0}", receivedMessage.CorrelationId);
                    receivedMessages++;
                }
                else
                {
                    // No more messages to receive.
                    break;
                }
            }
            Console.WriteLine("Received {0} messages from subscription {1}.", receivedMessages, subsName);
        }
コード例 #2
0
        static async Task Receiver()
        {
            var queueName = "myfirstqueue";
            var receiver  = new Microsoft.Azure.ServiceBus.Core.MessageReceiver(NamespaceConnectionString, queueName)
            {
            };

            receiver.RegisterMessageHandler(async(message, cancelationToken) => {
                Console.WriteLine(System.Text.Encoding.UTF8.GetString(message.Body));
                await receiver.CompleteAsync(message.SystemProperties.LockToken);
            }, new MessageHandlerOptions(async args => {})
            {
                AutoComplete = false
            });

            Console.ReadKey();

            await receiver.CloseAsync();
        }