Ejemplo n.º 1
0
        private static void SetupChat(ServiceBusConfig serviceBusConfig)
        {
            ManagementClient manager = new ManagementClient(serviceBusConfig.ConnectionString);

            //Create a Topic if it does not exists
            if (!manager.TopicExistsAsync(serviceBusConfig.TopicName).Result)
            {
                manager.CreateTopicAsync(serviceBusConfig.TopicName).Wait();
            }

            //Create new subscription for each user. Message will be delivered to each subscription.
            string userName = GetUserName();

            while (manager.SubscriptionExistsAsync(serviceBusConfig.TopicName, userName).Result)
            {
                Console.WriteLine("Please choose different user name");
                userName = GetUserName();
            }
            SubscriptionDescription subDesc = new SubscriptionDescription(serviceBusConfig.TopicName, userName)
            {
                AutoDeleteOnIdle = TimeSpan.FromMinutes(5)
            };

            manager.CreateSubscriptionAsync(subDesc).Wait();

            //Create clients
            TopicClient        senderClient   = new TopicClient(serviceBusConfig.ConnectionString, serviceBusConfig.TopicName);
            SubscriptionClient receiverClient = new SubscriptionClient(serviceBusConfig.ConnectionString, serviceBusConfig.TopicName, userName);

            //Create a message pump for receiving & processing messages
            receiverClient.RegisterMessageHandler(ProcessMessagesAsync, ExceptionHandlerAsync);

            //Send a message
            var helloMessage = new Message(Encoding.UTF8.GetBytes("Has entered the room..."))
            {
                Label = userName
            };

            senderClient.SendAsync(helloMessage).Wait();

            while (true)
            {
                string text = Console.ReadLine();
                if (text.Equals("exit"))
                {
                    break;
                }

                var chatMessage = new Message(Encoding.UTF8.GetBytes(text))
                {
                    Label = userName
                };
                senderClient.SendAsync(chatMessage).Wait();
            }

            var byeMessage = new Message(Encoding.UTF8.GetBytes("Has left the building..."))
            {
                Label = userName
            };

            senderClient.SendAsync(byeMessage).Wait();

            senderClient.CloseAsync().Wait();
            receiverClient.CloseAsync().Wait();
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            ServiceBusConfig serviceBusConfig = InitServiceBusConfig();

            SetupChat(serviceBusConfig);
        }