// 建立自定義綁定 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements);
// 使用自定義綁定建立通道偵聽器 IChannelListener<IReplyChannel> listener = binding.BuildChannelListener<IReplyChannel>( new Uri("http://localhost:8887/StringService"), new BindingParameterCollection()); // 監聽消息 listener.Open(); Console.WriteLine("Listening for incoming channel connections");
// 建立Reply通道 IReplyChannel channel = listener.AcceptChannel(); Console.WriteLine("Channel accepted. Listening for messages"); channel.Open(); RequestContext request = channel.ReceiveRequest();
// 讀取請求的消息 Message message = request.RequestMessage; Console.WriteLine("Message Received"); Console.WriteLine("Message Action: {0}", message.Headers.Action); string body = message.GetBody<string>(); Console.WriteLine("Message Content: {0}", body); // 發送響應消息 Message replymessage = Message.CreateMessage( binding.MessageVersion, "http://www.cnblogs.com/TerryLee/Encode", "Hello : " + body); request.Reply(replymessage);
// 釋放對象
message.Close();
request.Close();
channel.Close();
listener.Close();
/// <summary> /// Author:TerryLee /// Url:[url]http://www.cnblogs.com/terrylee[/url] /// </summary> static void Main() { // 建立自定義綁定 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements); // 使用自定義綁定建立通道偵聽器 IChannelListener<IReplyChannel> listener = binding.BuildChannelListener<IReplyChannel>( new Uri("http://localhost:8887/StringService"), new BindingParameterCollection()); // 監聽消息 listener.Open(); Console.WriteLine("Listening for incoming channel connections"); // 建立Reply通道 IReplyChannel channel = listener.AcceptChannel(); Console.WriteLine("Channel accepted. Listening for messages"); channel.Open(); RequestContext request = channel.ReceiveRequest(); // 讀取請求的消息 Message message = request.RequestMessage; Console.WriteLine("Message Received"); Console.WriteLine("Message Action: {0}", message.Headers.Action); string body = message.GetBody<string>(); Console.WriteLine("Message Content: {0}", body); // 發送響應消息 Message replymessage = Message.CreateMessage( binding.MessageVersion, "http://www.cnblogs.com/TerryLee/Encode", "Hello : " + body); request.Reply(replymessage); // 釋放對象 message.Close(); request.Close(); channel.Close(); listener.Close(); Console.WriteLine("Press Enter to exit"); Console.ReadLine(); }
// 建立綁定 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements);
// 使用綁定建立通道工廠 IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>( new BindingParameterCollection()); // 打開通道工廠 factory.Open(); Console.WriteLine("Channel factory opened");
// 建立Request通道 IRequestChannel channel = factory.CreateChannel( new EndpointAddress("http://localhost:8887/StringService")); channel.Open(); Console.WriteLine("Request channel opened");
// 建立請求消息 Message requestmessage = Message.CreateMessage( binding.MessageVersion, "http://www.cnblogs.com/TerryLee/Encode", "TerryLee"); // 發送請求消息並接收響應消息 Message replymessage = channel.Request(requestmessage); Console.WriteLine("Reply message received"); Console.WriteLine("Reply action: {0}", replymessage.Headers.Action); string data = replymessage.GetBody<string>(); Console.WriteLine("Reply content: {0}", data);
replymessage.Close(); channel.Close(); factory.Close();
/// <summary> /// Author:TerryLee /// Url:[url]http://www.cnblogs.com/terrylee[/url] /// </summary> public static void Main() { // 建立綁定 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements); // 使用綁定建立通道工廠 IChannelFactory<IRequestChannel> factory = binding.BuildChannelFactory<IRequestChannel>( new BindingParameterCollection()); // 打開通道工廠 factory.Open(); Console.WriteLine("Channel factory opened"); // 建立Request通道 IRequestChannel channel = factory.CreateChannel( new EndpointAddress("http://localhost:8887/StringService")); channel.Open(); Console.WriteLine("Request channel opened"); // 建立請求消息 Message requestmessage = Message.CreateMessage( binding.MessageVersion, "http://www.cnblogs.com/TerryLee/Encode", "TerryLee"); // 發送請求消息並接收響應消息 Message replymessage = channel.Request(requestmessage); Console.WriteLine("Reply message received"); Console.WriteLine("Reply action: {0}", replymessage.Headers.Action); string data = replymessage.GetBody<string>(); Console.WriteLine("Reply content: {0}", data); replymessage.Close(); channel.Close(); factory.Close(); Console.WriteLine("Press Enter to exit"); Console.ReadLine(); }
0javascript
收藏css
Ctrl+Enter 發佈html
發佈java
取消jquery