static void Main(string[] args) { try { //建立和发送端相同的通道栈 BindingElement[] bindingElements = new BindingElement[3]; bindingElements[0] = new TextMessageEncodingBindingElement();//文本编码 //oneWayBindingElement() 传输通道支持数据报模式 bindingElements[1] = new OneWayBindingElement(); bindingElements[2] = new NamedPipeTransportBindingElement();//命名管道 CustomBinding binding = new CustomBinding(bindingElements); //建立ChannelListner倾听者,接收数据 IChannelListener <IInputChannel> listener = binding.BuildChannelListener <IInputChannel>(new Uri("net.pipe://localhost/InputService"), new BindingParameterCollection()); listener.Open();//打开ChannelListner IInputChannel inputChannel = listener.AcceptChannel(); //创建IInputChannel inputChannel.Open(); //打开IInputChannel Console.WriteLine("开始接受消息.."); Message message = inputChannel.Receive(); //接受并打印 Console.WriteLine($"接收一条消息,action为{message.Headers.Action},Body为{message.GetBody<string>()}"); message.Close(); //关闭消息 inputChannel.Close(); //关闭通道 listener.Close(); //关闭监听器 Console.Read(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.Read(); } }
private TObjectType ReceiveMessage <TObjectType>() { Uri endpoint = new Uri("amqp:message_queue"); IChannelListener <IInputChannel> listener = Util.GetBinding().BuildChannelListener <IInputChannel>(endpoint, new BindingParameterCollection()); listener.Open(); IInputChannel service = listener.AcceptChannel(TimeSpan.FromSeconds(10)); service.Open(); Message receivedMessage = service.Receive(TimeSpan.FromSeconds(10)); Assert.NotNull(receivedMessage, "Message was not received"); try { TObjectType receivedObject = receivedMessage.GetBody <TObjectType>(); return(receivedObject); } catch (SerializationException) { Assert.Fail("Deserialized object not of correct type"); } finally { receivedMessage.Close(); service.Close(); listener.Close(); } return(default(TObjectType)); }
private void ReceiveNonTryMessages(TimeSpan channelTimeout, TimeSpan messageTimeout) { IChannelListener inputChannelParentListener; IInputChannel inputChannel = this.RetrieveAsyncChannel(this.endpoint, channelTimeout, out inputChannelParentListener); inputChannel.Open(); IAsyncResult[] resultArray = new IAsyncResult[MessageCount]; try { for (int i = 0; i < MessageCount; i++) { resultArray[i] = inputChannel.BeginReceive(messageTimeout, null, null); } for (int j = 0; j < MessageCount; j++) { inputChannel.EndReceive(resultArray[j]); } } finally { IAsyncResult channelCloseResult = inputChannel.BeginClose(channelTimeout, null, null); Thread.Sleep(TimeSpan.FromMilliseconds(50.0)); // Dummy work inputChannel.EndClose(channelCloseResult); // Asynchronous listener close has not been implemented. ////IAsyncResult listenerCloseResult = inputChannelParentListener.BeginClose(channelTimeout, null, null); ////Thread.Sleep(TimeSpan.FromMilliseconds(50.0)); // Dummy work ////inputChannelParentListener.EndClose(listenerCloseResult); inputChannelParentListener.Close(); } }
static void Main(string[] args) { try { //建立和发送端相同的通道栈 BindingElement[] bindingElements = new BindingElement[3]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new OneWayBindingElement(); bindingElements[2] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements); //建立ChannelListner IChannelListener <IInputChannel> listener = binding.BuildChannelListener <IInputChannel>(new Uri("http://localhost/InputService"), new BindingParameterCollection()); listener.Open(); //创建IInputChannel IInputChannel inputChannel = listener.AcceptChannel(); inputChannel.Open(); Console.WriteLine("开始接受消息。。。"); //接受并打印消息 Message message = inputChannel.Receive(); Console.WriteLine("接受到一条消息,action为:{0},body为:{1}", message.Headers.Action, message.GetBody <String>()); message.Close(); inputChannel.Close(); listener.Close(); Console.Read(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.Read(); } }
private static void ReceiveMessages() { // Read messages from queue until queue is empty Console.WriteLine("Reading messages from queue {0}...", SampleManager.SessionlessQueueName); Console.WriteLine("Receiver Type: Receive and Delete"); // Create channel listener and channel using NetMessagingBinding NetMessagingBinding messagingBinding = new NetMessagingBinding("messagingBinding"); EndpointAddress address = SampleManager.GetEndpointAddress(SampleManager.SessionlessQueueName, serviceBusNamespace); TransportClientEndpointBehavior securityBehavior = new TransportClientEndpointBehavior(); securityBehavior.TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(serviceBusKeyName, serviceBusKey); IChannelListener <IInputChannel> inputChannelListener = null; IInputChannel inputChannel = null; try { inputChannelListener = messagingBinding.BuildChannelListener <IInputChannel>(address.Uri, securityBehavior); inputChannelListener.Open(); inputChannel = inputChannelListener.AcceptChannel(); inputChannel.Open(); while (true) { try { // Receive message from queue. If no more messages available, the operation throws a TimeoutException. Message receivedMessage = inputChannel.Receive(receiveMessageTimeout); SampleManager.OutputMessageInfo("Receive", receivedMessage); // Since the message body contains a serialized string one can access it like this: //string soapBody = receivedMessage.GetBody<string>(); } catch (TimeoutException) { break; } } // Close inputChannel.Close(); inputChannelListener.Close(); } catch (Exception) { if (inputChannel != null) { inputChannel.Abort(); } if (inputChannelListener != null) { inputChannelListener.Abort(); } throw; } }
public virtual void Open() { if (_stared) { return; } _inputChannel.Open(); _stared = true; ApplyFilters(); }
public static void Snippet6() { MsmqIntegrationBindingElement msmqBindingElement = new MsmqIntegrationBindingElement(); CustomBinding binding = new CustomBinding(msmqBindingElement); BindingParameterCollection bindingParameterCollection = new BindingParameterCollection(); BindingContext bindingContext = new BindingContext(binding, bindingParameterCollection); IChannelListener <IInputChannel> listener = msmqBindingElement.BuildChannelListener <IInputChannel>(bindingContext); listener.Open(); IInputChannel channel = listener.AcceptChannel(); channel.Open(); }
private void ReceiveMessage <TObjectType>(TObjectType objectToMatch, AmqpProperties expectedProperties) { Uri receiveFromUri = new Uri("amqp:message_queue"); IChannelListener <IInputChannel> listener = Util.GetBinding().BuildChannelListener <IInputChannel>(receiveFromUri, new BindingParameterCollection()); listener.Open(); IInputChannel service = listener.AcceptChannel(TimeSpan.FromSeconds(10)); service.Open(); Message receivedMessage = service.Receive(TimeSpan.FromSeconds(10)); try { TObjectType receivedObject = receivedMessage.GetBody <TObjectType>(); Assert.True(receivedObject.Equals(objectToMatch), "Original and deserialized objects do not match"); AmqpProperties receivedProperties = (AmqpProperties)receivedMessage.Properties["AmqpProperties"]; PropertyInfo[] propInfo = typeof(AmqpProperties).GetProperties(); for (int i = 0; i < propInfo.Length; i++) { string propertyName = propInfo[i].Name; if (propertyName.Equals("RoutingKey", StringComparison.InvariantCultureIgnoreCase)) { Assert.AreEqual(RoutingKey, Convert.ToString(propInfo[i].GetValue(receivedProperties, null))); } else { Assert.AreEqual(Convert.ToString(propInfo[i].GetValue(expectedProperties, null)), Convert.ToString(propInfo[i].GetValue(receivedProperties, null))); } } } catch (NullReferenceException) { Assert.Fail("Message not received"); } catch (SerializationException) { Assert.Fail("Deserialized object not of correct type"); } finally { receivedMessage.Close(); service.Close(); listener.Close(); } }
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 <IInputChannel> factory = binding.BuildChannelFactory <IInputChannel>(); factory.Open(); try { System.ServiceModel.EndpointAddress addr = options.Address; IInputChannel receiver = factory.CreateChannel(addr); receiver.Open(); TimeSpan timeout = options.Timeout; System.ServiceModel.Channels.Message message; while (receiver.TryReceive(timeout, out message)) { AmqpProperties props = (AmqpProperties)message.Properties["AmqpProperties"]; Console.WriteLine("Message(properties=" + MessagePropertiesAsString(props) + ", content='" + MessageContentAsString(message, props) + "')"); } } finally { factory.Close(); } } catch (Exception e) { Console.WriteLine("Drain: " + e); } }
public static IInputChannel CreateReaderChannel(string queueName) { lock (typeof(QueueChannelFactory)) { if (brokerBinding == null) { InitializeBinding(); } if (readerFactory == null) { readerFactory = brokerBinding.BuildChannelFactory <IInputChannel>(bindingParameters); readerFactory.Open(); } IInputChannel channel = readerFactory.CreateChannel(new EndpointAddress( new Uri("amqp:" + queueName))); channel.Open(); return(channel); } }
public static int GetMessageCountFromQueue(string listenUri) { Message receivedMessage = null; int messageCount = 0; IChannelListener <IInputChannel> listener = Util.GetBinding().BuildChannelListener <IInputChannel>(new Uri(listenUri), new BindingParameterCollection()); listener.Open(); IInputChannel proxy = listener.AcceptChannel(TimeSpan.FromSeconds(10)); proxy.Open(); while (true) { try { receivedMessage = proxy.Receive(TimeSpan.FromSeconds(3)); } catch (Exception e) { if (e.GetType() == typeof(TimeoutException)) { break; } else { throw; } } messageCount++; } listener.Close(); return(messageCount); }
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(); }
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(); }
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; }