public async Task SubmitSequenceItemAsync(SequenceItem sequenceItem)
        {
            // Get the BrokeredMessageProperty from OperationContext
            var incomingProperties = OperationContext.Current.IncomingMessageProperties;
            var 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)
            {
                Console.WriteLine("{0}: {1} - ContextId {2}.", "Process Sequence", "Started processing sequence.", this.sessionId);
            }

            //Complete the Message
            ReceiveContext receiveContext;
            if (ReceiveContext.TryGet(incomingProperties, out receiveContext))
            {
                receiveContext.Complete(TimeSpan.FromSeconds(10.0d));
                this.sequenceItems.Add(sequenceItem);
                this.messageCounter++;
            }
            else
            {
                throw new InvalidOperationException("Receiver is in peek lock mode but receive context is not available!");
            }
        }
Ejemplo n.º 2
0
        public async Task Run(string namespaceAddress, string queueName, string sendToken)
        {
            try
            {
                // Create sender to Sequence Service
                using (var sendChannelFactory = new ChannelFactory <ISequenceServiceChannel>("sequenceSendClient"))
                {
                    sendChannelFactory.Endpoint.Address = new EndpointAddress(
                        new Uri(new Uri(namespaceAddress), queueName));
                    sendChannelFactory.Endpoint.EndpointBehaviors.Add(
                        new TransportClientEndpointBehavior(TokenProvider.CreateSharedAccessSignatureTokenProvider(sendToken)));

                    using (var clientChannel = sendChannelFactory.CreateChannel())
                    {
                        for (int j = 0; j < 3; j++)
                        {
                            var contextId = Guid.NewGuid().ToString();


                            // Send messages
                            var sequenceLength = new Random().Next(5, 10);
                            for (var i = 0; i < sequenceLength; i++)
                            {
                                // Generating a random sequence item
                                var sequenceItem = new SequenceItem(
                                    string.Format("{0:00000}", new Random().Next(0, 10000)),
                                    new Random().Next(1, 100));

                                // set the operation context for the subsequent call, this MUST be a new context
                                OperationContext.Current = new OperationContext(clientChannel)
                                {
                                    OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty {
                                                                        SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5)
                                                                    } } }
                                };
                                // Correlating ServiceBus SessionId to ContextId
                                await clientChannel.SubmitSequenceItemAsync(sequenceItem);

                                Console.WriteLine("Sequence: {0} [{1}] - ContextId {2}.", sequenceItem.ItemId, sequenceItem.Quantity, contextId);
                            }

                            // set the operation context for the subsequent call, this MUST be a new context
                            OperationContext.Current = new OperationContext(clientChannel)
                            {
                                OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty {
                                                                    SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5)
                                                                } } }
                            };
                            await clientChannel.TerminateSequenceAsync();
                        }
                        clientChannel.Close();
                    }
                    // Close sender
                    sendChannelFactory.Close();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred: {0}", exception);
            }

            Console.WriteLine("\nSender complete.");
            Console.WriteLine("\nPress [Enter] to exit.");
            Console.ReadLine();
        }
        public async Task Run(string namespaceAddress, string queueName, string sendToken)
        {
            try
            {

                // Create sender to Sequence Service
                using (var sendChannelFactory = new ChannelFactory<ISequenceServiceChannel>("sequenceSendClient"))
                {
                    sendChannelFactory.Endpoint.Address = new EndpointAddress(
                        new Uri(new Uri(namespaceAddress), queueName));
                    sendChannelFactory.Endpoint.EndpointBehaviors.Add(
                        new TransportClientEndpointBehavior(TokenProvider.CreateSharedAccessSignatureTokenProvider(sendToken)));

                    using (var clientChannel = sendChannelFactory.CreateChannel())
                    {

                        for (int j = 0; j < 3; j++)
                        {
                            var contextId = Guid.NewGuid().ToString();


                            // Send messages
                            var sequenceLength = new Random().Next(5, 10);
                            for (var i = 0; i < sequenceLength; i++)
                            {

                                // Generating a random sequence item
                                var sequenceItem = new SequenceItem(
                                    string.Format("{0:00000}", new Random().Next(0, 10000)),
                                    new Random().Next(1, 100));

                                // set the operation context for the subsequent call, this MUST be a new context
                                OperationContext.Current = new OperationContext(clientChannel)
                                {
                                    OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty { SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5) } } }
                                };
                                // Correlating ServiceBus SessionId to ContextId 
                                await clientChannel.SubmitSequenceItemAsync(sequenceItem);

                                Console.WriteLine("Sequence: {0} [{1}] - ContextId {2}.", sequenceItem.ItemId, sequenceItem.Quantity, contextId);
                            }

                            // set the operation context for the subsequent call, this MUST be a new context
                            OperationContext.Current = new OperationContext(clientChannel)
                            {
                                OutgoingMessageProperties = { { BrokeredMessageProperty.Name, new BrokeredMessageProperty { SessionId = contextId, TimeToLive = TimeSpan.FromMinutes(5) } } }
                            };
                            await clientChannel.TerminateSequenceAsync();
                        }
                        clientChannel.Close();
                    }
                    // Close sender
                    sendChannelFactory.Close();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception occurred: {0}", exception);
            }

            Console.WriteLine("\nSender complete.");
            Console.WriteLine("\nPress [Enter] to exit.");
            Console.ReadLine();
        }