Beispiel #1
0
        protected override void OnOpen(TimeSpan timeout)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            base.OnOpen(timeoutHelper.RemainingTime());
            _innerOutputChannel.Open(timeoutHelper.RemainingTime());
        }
Beispiel #2
0
 //入口方法
 static void Main(string[] args)
 {
     try
     {
         //建立自定义的通道栈
         BindingElement[] bindingElements = new BindingElement[3];
         bindingElements[0] = new TextMessageEncodingBindingElement();
         //OneWayBindingElement可以使得传输通道支持数据报模式
         bindingElements[1] = new OneWayBindingElement();
         bindingElements[2] = new HttpTransportBindingElement();
         CustomBinding binding = new CustomBinding(bindingElements);
         using (Message message = Message.CreateMessage(binding.MessageVersion, "sendMessage", "Message Body"))
         {
             //创建ChannelFactory
             IChannelFactory <IOutputChannel> factory = binding.BuildChannelFactory <IOutputChannel>(new BindingParameterCollection());
             factory.Open();
             //这里创建IOutputChannel
             IOutputChannel outputChannel = factory.CreateChannel(new EndpointAddress("http://localhost/InputService"));
             outputChannel.Open();
             //发送消息
             outputChannel.Send(message);
             Console.WriteLine("已经成功发送消息!");
             outputChannel.Close();
             factory.Close();
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
     finally
     {
         Console.Read();
     }
 }
Beispiel #3
0
        IOutputChannel GetOutputChannel(Uri to, TimeoutHelper timeoutHelper)
        {
            IOutputChannel channel = this.innerChannelFactory.CreateChannel(new EndpointAddress(to));

            channel.Open(timeoutHelper.RemainingTime());
            return(channel);
        }
Beispiel #4
0
        public Publisher(IOutputChannel outputChannel, MessageVersion messageVersion, IKnownContractCollector contractCollector, string busId)
        {
            _outputChannel     = outputChannel;
            _messageVersion    = messageVersion;
            _busId             = busId;
            _contractCollector = contractCollector;

            _outputChannel.Open();
        }
Beispiel #5
0
        private void SendMessage(object objectToSend)
        {
            IChannelFactory <IOutputChannel> channelFactory =
                Util.GetBinding().BuildChannelFactory <IOutputChannel>();

            channelFactory.Open();
            IOutputChannel proxy = channelFactory.CreateChannel(new EndpointAddress(Queue));

            proxy.Open();
            Message toSend = Message.CreateMessage(MessageVersion.Default, string.Empty, objectToSend);

            proxy.Send(toSend);
            toSend.Close();
            channelFactory.Close();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            try
            {
                Options options = new Options(args);

                AmqpBinaryBinding binding = new AmqpBinaryBinding();
                binding.BrokerHost   = options.Broker;
                binding.BrokerPort   = options.Port;
                binding.TransferMode = TransferMode.Streamed;

                IChannelFactory <IOutputChannel> factory = binding.BuildChannelFactory <IOutputChannel>();

                factory.Open();
                try
                {
                    System.ServiceModel.EndpointAddress addr = options.Address;
                    IOutputChannel sender = factory.CreateChannel(addr);
                    sender.Open();

                    MyRawBodyWriter.Initialize(options.Content);
                    DateTime end = DateTime.Now.Add(options.Timeout);
                    System.ServiceModel.Channels.Message message;

                    for (int count = 0; ((count < options.Count) || (options.Count == 0)) &&
                         ((options.Timeout == TimeSpan.Zero) || (end.CompareTo(DateTime.Now) > 0)); count++)
                    {
                        message = Message.CreateMessage(MessageVersion.None, "", new MyRawBodyWriter());
                        AmqpProperties props = new AmqpProperties();
                        props.ContentType = "text/plain";

                        string id = Guid.NewGuid().ToString() + ":" + count;
                        props.PropertyMap.Add("spout-id", new AmqpString(id));

                        message.Properties["AmqpProperties"] = props;
                        sender.Send(message);
                    }
                }
                finally
                {
                    factory.Close();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Spout: " + e);
            }
        }
Beispiel #7
0
        private void SendMessage(object objectToSend, AmqpProperties propertiesToSend)
        {
            ChannelFactory <IOutputChannel> channelFactory =
                new ChannelFactory <IOutputChannel>(Util.GetBinding(), SendToUri);
            IOutputChannel proxy = channelFactory.CreateChannel();

            proxy.Open();

            Message toSend = Message.CreateMessage(MessageVersion.Default, string.Empty, objectToSend);

            toSend.Properties["AmqpProperties"] = propertiesToSend;
            proxy.Send(toSend);

            toSend.Close();
            proxy.Close();
            channelFactory.Close();
        }
Beispiel #8
0
        public override void Dispatch(MessageDelivery messageDelivery)
        {
            IOutputChannel channel = CreateChannelFactory().CreateChannel();
            bool           closed  = false;

            try
            {
                channel.Open();
                channel.Send(_converter.ToMessage(messageDelivery));
            }
            finally
            {
                channel.Close();
                closed = true;
            }
            if (!closed)
            {
                channel.Abort();
            }
        }
Beispiel #9
0
 static void Main(string[] args)
 {
     try
     {
         //建立自定义通道栈
         BindingElement[] bindingElements = new BindingElement[3];
         bindingElements[0] = new TextMessageEncodingBindingElement();//文本编码
         //OneWayBindingElement 可以使得传输通道支持数据报模式
         bindingElements[1] = new OneWayBindingElement();
         //http传输
         //  bindingElements[2] = new HttpTransportBindingElement();
         //命名管道传输
         bindingElements[2] = new NamedPipeTransportBindingElement();
         CustomBinding binding = new CustomBinding(bindingElements);
         using (Message message = Message.CreateMessage(binding.MessageVersion, "sendMessage", "Message Body"))
         //创建消息
         {
             //创建ChannelFactory
             IChannelFactory <IOutputChannel> factory = binding.BuildChannelFactory <IOutputChannel>(new BindingParameterCollection());
             factory.Open();//打开ChannelFactory
             //创建IoutputChannel
             IOutputChannel outputChannel = factory.CreateChannel(new System.ServiceModel.EndpointAddress("net.pipe://localhost/InputService"));
             outputChannel.Open();        //打开通道
             outputChannel.Send(message); //发送消息
             Console.WriteLine("已经成功发送消息!");
             outputChannel.Close();       //关闭通道
             factory.Close();             //关闭工厂
         }
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
     finally
     {
         Console.Read();
     }
 }
Beispiel #10
0
        public static IOutputChannel CreateWriterChannel(string exchangeName, string routingKey)
        {
            lock (typeof(QueueChannelFactory))
            {
                if (brokerBinding == null)
                {
                    InitializeBinding();
                }

                if (writerFactory == null)
                {
                    writerFactory = brokerBinding.BuildChannelFactory <IOutputChannel>(bindingParameters);
                    writerFactory.Open();
                }

                IOutputChannel channel = writerFactory.CreateChannel(new EndpointAddress(
                                                                         "amqp:" + exchangeName +
                                                                         "?routingkey=" + routingKey));
                channel.Open();

                return(channel);
            }
        }
Beispiel #11
0
        static void Main(string[] args)
        {
            try
            {
                ParseArgs(args);
                Console.Title = "Message Sender";

                // Get credentials as Endpoint behavior
                TransportClientEndpointBehavior securityBehavior = new TransportClientEndpointBehavior();
                securityBehavior.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(serviceBusKeyName, serviceBusKey);

                // Create factory and channel using NetMessagingBinding
                NetMessagingBinding messagingBinding = new NetMessagingBinding("messagingBinding");
                EndpointAddress     address          = SampleManager.GetEndpointAddress(SampleManager.SessionlessQueueName, serviceBusNamespace);

                IChannelFactory <IOutputChannel> messagingChannelFactory = null;
                IOutputChannel messagingOutputChannel = null;
                try
                {
                    messagingChannelFactory = messagingBinding.BuildChannelFactory <IOutputChannel>(securityBehavior);
                    messagingChannelFactory.Open();
                    messagingOutputChannel = messagingChannelFactory.CreateChannel(address);
                    messagingOutputChannel.Open();

                    // Send messages to queue which does not require session
                    Console.WriteLine("Preparing to send messages to {0}...", SampleManager.SessionlessQueueName);
                    Thread.Sleep(3000);

                    SendMessages(messagingOutputChannel, messagingBinding);
                    messagingOutputChannel.Close();
                    messagingChannelFactory.Close();
                }
                catch (Exception)
                {
                    if (messagingOutputChannel != null)
                    {
                        messagingOutputChannel.Abort();
                    }
                    if (messagingChannelFactory != null)
                    {
                        messagingChannelFactory.Abort();
                    }
                    throw;
                }

                // Wait for all receivers to receive message
                Thread.Sleep(TimeSpan.FromSeconds(5.0d));
                Console.Clear();

                // Create factory and channel using custom binding
                CustomBinding customBinding = new CustomBinding("customBinding");
                address = SampleManager.GetEndpointAddress(SampleManager.SessionQueueName, serviceBusNamespace);

                IChannelFactory <IOutputChannel> customChannelFactory = null;
                IOutputChannel customOutputChannel = null;
                try
                {
                    customChannelFactory = customBinding.BuildChannelFactory <IOutputChannel>(securityBehavior);
                    customChannelFactory.Open();
                    customOutputChannel = customChannelFactory.CreateChannel(address);
                    customOutputChannel.Open();

                    // Send messages to queue which requires session
                    Console.Title = "Session MessageSender";
                    Console.WriteLine("Preparing to send messages to {0}...", SampleManager.SessionQueueName);
                    Thread.Sleep(3000);

                    SendMessages(customOutputChannel, customBinding);
                    customOutputChannel.Close();
                    customChannelFactory.Close();
                }
                catch (Exception)
                {
                    if (customOutputChannel != null)
                    {
                        customOutputChannel.Abort();
                    }
                    if (customChannelFactory != null)
                    {
                        customChannelFactory.Abort();
                    }
                    throw;
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception Occurred: {0}", exception);
                SampleManager.ExceptionOccurred = true;
            }

            // All messages sent
            Console.WriteLine("\nSender complete. Press [Enter] to exit.");
            Console.ReadLine();
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            // Creating the Channel
            string channelName = "EMailHelloWorld";

            string serverAddress          = "*****@*****.**";
            string serverPWD              = "MyPassword";
            string clientAddress          = "*****@*****.**";
            string exchangeServerLocation = "http://example.com";

            ExchangeWebServiceMailBinding binding =
                new ExchangeWebServiceMailBinding(
                    new Uri(exchangeServerLocation),
                    new System.Net.NetworkCredential(serverAddress,
                                                     serverPWD));
            BindingParameterCollection parameters =
                new BindingParameterCollection();

            IChannelListener <IInputChannel> listener =
                binding.BuildChannelListener <IInputChannel>
                    (MailUriHelper.CreateUri(channelName, ""), parameters);

            listener.Open();

            // Opening the Channel
            IInputChannel inputChannel = listener.AcceptChannel();

            inputChannel.Open(TimeSpan.MaxValue);
            Console.WriteLine("Channel Open");

            // Waiting and receiving the Message
            Message reply = inputChannel.Receive(TimeSpan.MaxValue);

            Console.WriteLine("Message Recieved");
            XmlSerializerWrapper wrapper = new XmlSerializerWrapper(
                typeof(TransmittedObject));
            TransmittedObject to = reply.GetBody <TransmittedObject>(wrapper);

            // Processing the Message
            to.str = to.str + " World";
            to.i   = to.i + 1;

            Console.WriteLine("Response: " + to.str + " " + to.i.ToString());

            // Creating and returning the Message
            Message m = Message.CreateMessage(binding.MessageVersion,
                                              "urn:test", to, wrapper);

            IChannelFactory <IOutputChannel> channelFactory =
                binding.BuildChannelFactory <IOutputChannel>(parameters);

            channelFactory.Open();

            IOutputChannel outChannel = channelFactory.CreateChannel(
                new EndpointAddress(
                    MailUriHelper.CreateUri(channelName, clientAddress)));

            outChannel.Open();

            Console.WriteLine("Out Channel Open");

            // Sending the Message over the OutChannel
            outChannel.Send(m);

            Console.WriteLine("Message Sent");

            // Cleaning up
            outChannel.Close();
            channelFactory.Close();

            listener.Close();
            inputChannel.Close();
            binding.Close();
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            String broker = "localhost";
            int    port   = 5672;
            String target = "amq.topic";
            String source = "my_topic_node";

            if (args.Length > 0)
            {
                broker = args[0];
            }

            if (args.Length > 1)
            {
                port = int.Parse(args[1]);
            }

            if (args.Length > 2)
            {
                target = args[2];
            }

            if (args.Length > 3)
            {
                source = args[3];
            }

            AmqpBinaryBinding binding = new AmqpBinaryBinding();

            binding.BrokerHost = broker;
            binding.BrokerPort = port;

            IChannelFactory <IInputChannel> receiverFactory = binding.BuildChannelFactory <IInputChannel>();

            receiverFactory.Open();
            IInputChannel receiver = receiverFactory.CreateChannel(new EndpointAddress("amqp:" + source));

            receiver.Open();

            IChannelFactory <IOutputChannel> senderFactory = binding.BuildChannelFactory <IOutputChannel>();

            senderFactory.Open();
            IOutputChannel sender = senderFactory.CreateChannel(new EndpointAddress("amqp:" + target));

            sender.Open();

            sender.Send(Message.CreateMessage(MessageVersion.None, "", new HelloWorldBinaryBodyWriter()));

            Message             message = receiver.Receive();
            XmlDictionaryReader reader  = message.GetReaderAtBodyContents();

            while (!reader.HasValue)
            {
                reader.Read();
            }

            byte[] binaryContent = reader.ReadContentAsBase64();
            string text          = Encoding.UTF8.GetString(binaryContent);

            Console.WriteLine(text);

            senderFactory.Close();
            receiverFactory.Close();
        }
Beispiel #14
0
        private void SendMessages(IOutputChannel channel)
        {
            channel.Open();

            AutoResetEvent doneSending      = new AutoResetEvent(false);
            int            threadsCompleted = 0;

            if (this.Parameters.NumberOfMessages > 0)
            {
                this.SendMessage(channel, "FirstMessage", true);
            }

            if (this.Parameters.NumberOfThreads == 1)
            {
                for (int j = 0; j < this.Parameters.NumberOfMessages; ++j)
                {
                    if (this.Parameters.SenderShouldAbort)
                    {
                        this.SendMessage(channel, "Message " + (j + 1), false);
                    }

                    this.SendMessage(channel, "Message " + (j + 1), true);
                }

                doneSending.Set();
            }
            else
            {
                for (int i = 0; i < this.Parameters.NumberOfThreads; ++i)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object unused)
                    {
                        for (int j = 0; j < this.Parameters.NumberOfMessages / this.Parameters.NumberOfThreads; ++j)
                        {
                            if (this.Parameters.SenderShouldAbort)
                            {
                                this.SendMessage(channel, "Message", false);
                            }

                            this.SendMessage(channel, "Message", true);
                        }
                        if (Interlocked.Increment(ref threadsCompleted) == this.Parameters.NumberOfThreads)
                        {
                            doneSending.Set();
                        }
                    }));
                }
            }

            TimeSpan threadTimeout = TimeSpan.FromMinutes(2.0);

            if (!doneSending.WaitOne(threadTimeout, false))
            {
                lock (this.Results)
                {
                    this.Results.Add(String.Format("Threads did not complete within {0}.", threadTimeout));
                }
            }

            doneSending.Close();
            channel.Close();
        }
Beispiel #15
0
        private void sendButton_Click(object sender, EventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            // Creating the Message
            TransmittedObject to = new TransmittedObject();

            to.str = "Hello";
            to.i   = 5;

            XmlSerializerWrapper wrapper =
                new XmlSerializerWrapper(typeof(TransmittedObject));

            Message m = Message.CreateMessage
                            (MessageVersion.Default, "urn:test", to, wrapper);

            // Creating the Channel
            string channelName = "EMailHelloWorld";

            string serverAddress = "*****@*****.**";
            string clientAddress = "*****@*****.**";

            WindowsMobileMailBinding   binding    = new WindowsMobileMailBinding();
            BindingParameterCollection parameters =
                new BindingParameterCollection();

            IChannelFactory <IOutputChannel> channelFactory =
                binding.BuildChannelFactory <IOutputChannel>(parameters);

            channelFactory.Open();

            // Opening the Channel
            IOutputChannel outChannel = channelFactory.CreateChannel(
                new EndpointAddress(MailUriHelper.Create
                                        (channelName, serverAddress)));

            outChannel.Open();

            // Sending the Message
            outChannel.Send(m);

            // Listening for the response
            IChannelListener <IInputChannel> listner =
                binding.BuildChannelListener <IInputChannel>
                    (MailUriHelper.CreateUri(channelName, clientAddress),
                    parameters);

            listner.Open();

            IInputChannel inputChannel = listner.AcceptChannel();

            inputChannel.Open();

            Message reply = inputChannel.Receive();

            // Deserializing and using the Message
            TransmittedObject to1 =
                reply.GetBody <TransmittedObject>
                    (new XmlSerializerWrapper(typeof(TransmittedObject)));

            MessageBox.Show(to1.str + " " + to1.i.ToString());

            // Cleaning up
            outChannel.Close();
            channelFactory.Close();

            listner.Close();
            inputChannel.Close();
            binding.Close();

            Cursor.Current = Cursors.Default;
        }