예제 #1
0
        static void Main(string[] args)
        {
            GetUserCredentials();
            TokenProvider credentials =
                TokenProvider.CreateSharedSecretTokenProvider(IssuerName, IssuerKey);
            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty);

            //*****************************************************************************************************
            //                                   Runtime Operations
            //*****************************************************************************************************
            MessagingFactory factory = MessagingFactory.Create(serviceUri, credentials);

            try
            {
                QueueClient myQueueClient = factory.CreateQueueClient(queueName, ReceiveMode.PeekLock);

                //*****************************************************************************************************
                //                                   Receiving messages from a Queue
                //*****************************************************************************************************

                Console.WriteLine("\nReceiving messages from queue...");
                BrokeredMessage message;
                while ((message = myQueueClient.Receive(new TimeSpan(hours: 0, minutes: 0, seconds: 5))) != null)
                {
                    Console.WriteLine(
                        string.Format(
                            "Message received: Id = {0}, Body = {1}", message.MessageId, message.GetBody <string>()));
                    // Further custom message processing could go here...
                    message.Complete();
                }

                Console.WriteLine("\nEnd of scenario, press ENTER to exit.");
                Console.ReadLine();

                // Closing factory close all entities created from the factory.
                factory.Close();
            }
            catch (Exception)
            {
                factory.Abort();
                throw;
            }
        }
        static void Main(string[] args)
        {
            GetUserCredentials();
            TokenProvider credentials =
                TokenProvider.CreateSharedSecretTokenProvider(IssuerName, IssuerKey);
            Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty);

            //*****************************************************************************************************
            //                                   Management Operations
            //*****************************************************************************************************
            NamespaceManager namespaceClient = new NamespaceManager(serviceUri, credentials);

            Console.WriteLine("\nCreating Queue 'IssueTrackingQueue'...");

            // Delete if exists
            if (namespaceClient.QueueExists(queueName))
            {
                namespaceClient.DeleteQueue(queueName);
            }

            namespaceClient.CreateQueue(queueName);

            //*****************************************************************************************************
            //                                   Runtime Operations
            //*****************************************************************************************************
            MessagingFactory factory = MessagingFactory.Create(serviceUri, credentials);

            try
            {
                QueueClient myQueueClient = factory.CreateQueueClient(queueName);

                //*****************************************************************************************************
                //                                   Sending messages to a Queue
                //*****************************************************************************************************
                List <BrokeredMessage> messageList = new List <BrokeredMessage>();
                messageList.Add(CreateIssueMessage("1", "Package lost"));
                messageList.Add(CreateIssueMessage("2", "Package damaged"));
                messageList.Add(CreateIssueMessage("3", "Package defective"));

                Console.WriteLine("\nSending messages to queue...");

                foreach (BrokeredMessage message in messageList)
                {
                    myQueueClient.Send(message);
                    Console.WriteLine(
                        string.Format("Message sent: Id = {0}, Body = {1}", message.MessageId, message.GetBody <string>()));
                }

                Console.WriteLine("\nFinished sending messages, press ENTER to clean up and exit.");
                Console.ReadLine();

                // Closing factory close all entities created from the factory.
                factory.Close();
            }
            catch (Exception)
            {
                factory.Abort();
                throw;
            }

            namespaceClient.DeleteQueue(queueName);
        }