Example #1
0
        static void SendMessages(IOutputChannel clientChannel, Binding binding)
        {
            // Send messages to queue:
            Console.WriteLine("Started sending messages...");
            Random rand = new Random();

            for (int i = 0; i < SampleManager.NumMessages; ++i)
            {
                string sessionName = rand.Next(SampleManager.NumSessions).ToString();

                // Creating BrokeredMessageProperty
                BrokeredMessageProperty property = new BrokeredMessageProperty();
                property.SessionId = sessionName;
                string soapBody = "Order_" + Guid.NewGuid().ToString().Substring(0, 5);
                property.Label = soapBody;

                // Creating message and adding BrokeredMessageProperty to the properties bag
                Message message = Message.CreateMessage(binding.MessageVersion, "SoapAction", soapBody);
                message.Properties.Add(BrokeredMessageProperty.Name, property);

                // Sending message
                clientChannel.Send(message);
                SampleManager.OutputMessageInfo("Send", message);
                Thread.Sleep(senderDelay);
            }

            Console.WriteLine("Finished sending messages");
        }
        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!");
            }
        }
Example #3
0
 public static void OutputMessageInfo(string action, Message message, string additionalText = "")
 {
     lock (typeof(SampleManager))
     {
         BrokeredMessageProperty property = (BrokeredMessageProperty)message.Properties[BrokeredMessageProperty.Name];
         Console.ForegroundColor = colors[int.Parse(property.SessionId)];
         Console.WriteLine("{0}: {1} - Group {2}. {3}", action, property.Label, property.SessionId, additionalText);
         Console.ResetColor();
     }
 }
        protected override void PreInvoke(ref Message request)
        {
            BrokeredMessageProperty property = GetMessageProperty(ref request);

            Debug.Assert(property != null);

            property.SessionId = SessionId;

            base.PreInvoke(ref request);
        }
Example #5
0
        public void DeserializeRequest(Message message, object[] parameters)
        {
            if (!message.Properties.ContainsKey(BrokeredMessageProperty.Name))
            {
                throw new InvalidOperationException("BrokeredMessageFormatter_BrokeredMessagePropertyMissing");
            }

            BrokeredMessageProperty property = (BrokeredMessageProperty)message.Properties[BrokeredMessageProperty.Name];

            parameters[0] = property.Message;
        }
Example #6
0
        private void sendQueue_Click(object sender, EventArgs e)
        {
            var sendingClient = QueueClient.Create(queueNameTxt.Text);

            object msgBody            = sendQueueTxt.Text;
            var    msg                = new BrokeredMessage(msgBody);
            var    teamCustomProperty = new BrokeredMessageProperty();

            sendingClient.Send(new BrokeredMessage(msgBody));

            status.Text = "Message sent to " + queueNameTxt.Text + " queue.";
        }
Example #7
0
        private Message ConvertToWcfMessage(BrokeredMessage brokeredMessage)
        {
            MessageEncoder encoder = this.ChannelListener.MessageEncoderFactory.Encoder;
            Message        uri     = encoder.ReadMessage(brokeredMessage.BodyStream, this.ChannelListener.MaxBufferSize);

            BrokeredMessageProperty.AddPropertyToWcfMessage(brokeredMessage, uri);
            if (this.ChannelListener.ReceiveContextEnabled)
            {
                ServiceBusInputChannelBase <TChannel> .AfmsReceiveContext.ApplyTo(uri, this.messageReceiver, brokeredMessage.LockToken);
            }
            uri.Properties.Via = this.LocalAddress.Uri;
            return(uri);
        }
        protected override void PreInvoke(ref Message request)
        {
            BrokeredMessageProperty property = GetMessageProperty(ref request);

            Debug.Assert(property != null);

            property.CorrelationId    = GenerateMethodId();
            property.ReplyTo          = ResponseAddress.AbsoluteUri;
            property.ReplyToSessionId = ReplyToSessionId;

            LastCorrolationId = property.CorrelationId;

            base.PreInvoke(ref request);
        }
        protected override void PreInvoke(ref Message request)
        {
            BrokeredMessageProperty property = GetMessageProperty(ref request);

            Debug.Assert(property != null);

            BrokeredMessageProperty contextProperty = OperationContext.Current.IncomingMessageProperties[BrokeredMessageProperty.Name] as BrokeredMessageProperty;

            SessionId = contextProperty.SessionId;

            property.CorrelationId    = contextProperty.CorrelationId;
            property.ReplyToSessionId = contextProperty.ReplyToSessionId;

            base.PreInvoke(ref request);
        }
        protected BrokeredMessageProperty GetMessageProperty(ref Message request)
        {
            BrokeredMessageProperty property;

            if (request.Properties.ContainsKey(BrokeredMessageProperty.Name) == false)
            {
                property = new BrokeredMessageProperty();
                request.Properties.Add(BrokeredMessageProperty.Name, property);
            }
            else
            {
                property = request.Properties[BrokeredMessageProperty.Name] as BrokeredMessageProperty;
            }
            return(property);
        }
        private BrokeredMessage ConvertToBrokerMessage(Message wcfMessage, out byte[] bufferToReturn)
        {
            BrokeredMessage         brokeredMessage;
            BrokeredMessageProperty brokeredMessageProperty;
            BrokeredMessage         brokeredMessage1;

            bufferToReturn = null;
            if (!BrokeredMessageProperty.TryGet(wcfMessage.Properties, out brokeredMessageProperty))
            {
                brokeredMessage = new BrokeredMessage();
                EndpointAddress replyTo = wcfMessage.Headers.ReplyTo;
                if (replyTo != null)
                {
                    brokeredMessage.ReplyTo = replyTo.Uri.AbsoluteUri;
                }
            }
            else
            {
                brokeredMessage = brokeredMessageProperty.Message;
            }
            byte[] array = null;
            try
            {
                ArraySegment <byte> nums = this.encoder.WriteMessage(wcfMessage, this.maxBufferSize, this.ChannelFactory.BufferManager);
                array = nums.Array;
                MemoryStream memoryStream = new MemoryStream(nums.Array, nums.Offset, nums.Count, true, true);
                brokeredMessage.BodyStream  = memoryStream;
                brokeredMessage.ContentType = this.encoder.ContentType;
                bufferToReturn   = array;
                array            = null;
                brokeredMessage1 = brokeredMessage;
            }
            catch (Exception exception)
            {
                if (array != null)
                {
                    this.ChannelFactory.BufferManager.ReturnBuffer(array);
                }
                throw;
            }
            return(brokeredMessage1);
        }
        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);
                }
            }
        }
Example #14
0
        private static void MessageWithContext()
        {
            var transportClientEndpointBehavior = new TransportClientEndpointBehavior
            {
                TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sendKeyName, sendKeyValue)
            };

            var serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, _queueName);

            var netMessagingBinding = new NetMessagingBinding
            {
                PrefetchCount = 10,
                TransportSettings = new NetMessagingTransportSettings
                {
                    // this is polling interval
                    BatchFlushInterval = TimeSpan.FromSeconds(0)
                }
            };

            var serviceEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IRequest)),
                                                        netMessagingBinding,
                                                        new EndpointAddress(serviceUri));
            serviceEndpoint.Behaviors.Add(transportClientEndpointBehavior);
            var channelFactory = new ChannelFactory<IRequest>(serviceEndpoint);
            var channel = channelFactory.CreateChannel();
            Console.WriteLine("Press any key to send message...");
            Console.ReadKey();

            using (new OperationContextScope((IContextChannel)channel))
            {
                var brokeredMessageProperty = new BrokeredMessageProperty
                {
                    Label = "MyLabel",
                    MessageId = Guid.NewGuid().ToString()
                };
                brokeredMessageProperty.Properties.Add("Author", "Paolo Salvatori");
                brokeredMessageProperty.Properties.Add("Email", "*****@*****.**");
                brokeredMessageProperty.Properties.Add("Country", "Italy");
                brokeredMessageProperty.Properties.Add("Priority", 1);
                OperationContext.Current.OutgoingMessageProperties.Add(BrokeredMessageProperty.Name, brokeredMessageProperty);
                channel.SendMessageWithContext("Hello from wcf client with context");

            }

            Console.WriteLine("Message sent");
            Console.ReadKey();
        }