public void Order(OrderItem orderItem)
        {
            // Get the BrokeredMessageProperty from OperationContext
            var incomingProperties           = OperationContext.Current.IncomingMessageProperties;
            BrokeredMessageProperty property = (BrokeredMessageProperty)incomingProperties[BrokeredMessageProperty.Name];

            // Get the current ServiceBus SessionId
            if (this.sessionId == string.Empty)
            {
                this.sessionId = property.SessionId;
            }

            // Print message
            if (this.messageCounter == 0)
            {
                SampleManager.OutputMessageInfo("Process Order", "Started processing order.", this.sessionId);
            }

            //Complete the Message
            ReceiveContext receiveContext;

            if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
            {
                receiveContext.Complete(TimeSpan.FromSeconds(10.0d));
                this.orderItems.Add(orderItem);
                this.messageCounter++;
            }
            else
            {
                throw new InvalidOperationException("Receiver is in peek lock mode but receive context is not available!");
            }
        }
 static void SendMessages(IPingServiceContract clientChannel)
 {
     // Send messages to queue which requires session:
     for (int i = 0; i < numberOfMessages; i++)
     {
         // Send message
         PingData message = CreatePingData();
         clientChannel.Ping(message);
         SampleManager.OutputMessageInfo("Send", message);
         Thread.Sleep(200);
     }
 }
        public void Ping(PingData pingData)
        {
            // Get the message properties
            var incomingProperties           = OperationContext.Current.IncomingMessageProperties;
            BrokeredMessageProperty property = (BrokeredMessageProperty)incomingProperties[BrokeredMessageProperty.Name];

            // Print message
            SampleManager.OutputMessageInfo("Receive", pingData);

            //Complete the Message
            ReceiveContext receiveContext;

            if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
            {
                receiveContext.Complete(TimeSpan.FromSeconds(10.0d));
            }
            else
            {
                throw new InvalidOperationException("Receiver is in peek lock mode but receive context is not available!");
            }
        }
        static void PlaceOrder(IOrderServiceContract clientChannel)
        {
            // Send messages to queue which requires session:
            for (int i = 0; i < orderQuantity; i++)
            {
                using (OperationContextScope scope = new OperationContextScope((IContextChannel)clientChannel))
                {
                    OrderItem orderItem = RandomizeOrder();

                    // Assigning the session name
                    BrokeredMessageProperty property = new BrokeredMessageProperty();

                    // Correlating ServiceBus SessionId to CustomerId
                    property.SessionId = customerId;

                    // Add BrokeredMessageProperty to the OutgoingMessageProperties bag to pass on the session information
                    OperationContext.Current.OutgoingMessageProperties.Add(BrokeredMessageProperty.Name, property);
                    clientChannel.Order(orderItem);
                    SampleManager.OutputMessageInfo("Order", string.Format("{0} [{1}]", orderItem.ProductId, orderItem.Quantity), customerId);
                    Thread.Sleep(200);
                }
            }
        }
 public void Dispose()
 {
     SampleManager.OutputMessageInfo("Process Order", string.Format("Finished processing order. Total {0} items", orderItems.Count), this.sessionId);
 }