static void SendMessages(QueueClient requestClient, QueueClient responseClient)
        {
            // Send request messages to queue:
            Console.WriteLine("Sending request messages to queue {0}", requestClient.Path);
            Console.WriteLine("Receiving response messages on queue {0}", responseClient.Path);

            MessageSession receiver = responseClient.AcceptMessageSession(ReplyToSessionId);

            for (int i = 0; i < SampleManager.NumMessages; ++i)
            {
                // send request message
                BrokeredMessage message = CreateRequestMessage(i);
                requestClient.Send(message);
                SampleManager.OutputMessageInfo("REQUEST: ", message);

                // receive response message
                BrokeredMessage receivedMessage = receiver.Receive(TimeSpan.FromSeconds(ResponseMessageTimeout));
                if (receivedMessage != null)
                {
                    SampleManager.OutputMessageInfo("RESPONSE: ", message);
                    receivedMessage.Complete();
                }
                else
                {
                    Console.WriteLine("ERROR: Response timed out.");
                }
            }
            receiver.Close();

            Console.WriteLine();
        }
Exemple #2
0
        static void ReceiveSessionMessages(MessageSession receiver)
        {
            // Read messages from queue until queue is empty:
            Console.WriteLine("Reading messages from queue {0}", receiver.Path);
            Console.WriteLine("Receiver Type:" + receiver.GetType().Name);
            Console.WriteLine("Receiver.SessionId = " + receiver.SessionId);

            BrokeredMessage receivedMessage;

            while ((receivedMessage = receiver.Receive(TimeSpan.FromSeconds(receiveSessionMessageTimeout))) != null)
            {
                string sessionId = receiver.SessionId;
                ProcessMessage(receivedMessage, receiver);
            }

            receiver.Close();
        }
        /// <summary>
        /// Starts listening for session messages on the configured Service Bus Queue.
        /// </summary>
        protected override void ListenForSessionMessages()
        {
            ThreadStart ts = async() =>
            {
                WriteLog($"Service Bus Communication Listnener now listening for session messages.");

                while (!StopProcessingMessageToken.IsCancellationRequested)
                {
                    MessageSession session = null;
                    try
                    {
                        session = ServiceBusClient.AcceptMessageSession(ServerTimeout);

                        do
                        {
                            var messages = session.ReceiveBatch(MessageBatchSize, ServerTimeout).ToArray();
                            if (messages.Length == 0)
                            {
                                break;
                            }

                            var tokens = BatchReceiver.AutoComplete ? messages.Select(m => m.LockToken).ToArray() : null;

                            await ProcessMessagesAsync(messages, session);

                            if (BatchReceiver.AutoComplete)
                            {
                                await session.CompleteBatchAsync(tokens);
                            }
                        }while (!StopProcessingMessageToken.IsCancellationRequested);
                    }
                    catch (TimeoutException)
                    { }
                    finally
                    {
                        session?.Close();
                    }
                }
            };

            StartBackgroundThread(ts);
        }
Exemple #4
0
        /// <summary>
        ///     Closes both queues and closes the session.
        /// </summary>
        public void Dispose()
        {
            if (_session != null)
            {
                _session.Close();
                _session = null;
            }

            if (_sendQueueClient != null)
            {
                _sendQueueClient.Close();
                _sendQueueClient = null;
            }

            if (_readQueueClient != null)
            {
                _readQueueClient.Close();
                _readQueueClient = null;
            }
        }