public static void Snippet17() { // <Snippet17> CustomBinding binding = new CustomBinding(); HttpTransportBindingElement element = new HttpTransportBindingElement(); BindingParameterCollection parameters = new BindingParameterCollection(); Uri baseAddress = new Uri("http://localhost:8000/ChannelApp"); String relAddress = "http://localhost:8000/ChannelApp/service"; BindingContext context = new BindingContext(binding, parameters, baseAddress, relAddress, ListenUriMode.Explicit); IChannelListener <IReplyChannel> listener = element.BuildChannelListener <IReplyChannel>(context); listener.Open(); IReplyChannel channel = listener.AcceptChannel(); channel.Open(); RequestContext request = channel.ReceiveRequest(); Message msg = request.RequestMessage; Console.WriteLine("Message Received"); Console.WriteLine("Message Action: {0}", msg.Headers.Action); if (msg.Headers.Action == "hello") { Message reply = Message.CreateMessage(MessageVersion.Default, "wcf"); request.Reply(reply); } msg.Close(); channel.Close(); listener.Close(); // </Snippet17> }
/// <summary> /// 网络监听任务ChannelListener管道创建的消息处理管道最终实现了对请求的接收和响应的发送 /// </summary> /// <param name="args"></param> public static void Main(string[] args) { //1、httpselfhost openAsync开启之后逻辑 Uri listenUri = new Uri("http://127.0.0.1:3721"); Binding binding = new HttpBinding(); //创建 开启 信道监听器 IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(listenUri); //可以使用异步的方式开启 //channelListener.BeginOpen(); channelListener.Open(); //创建 开启 回复信道 IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.FromDays(24)); channel.Open(); while (true) { RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue); PrintRequestMessage(requestContext.RequestMessage); //消息回复 requestContext.Reply(CreateResponseMessage()); } }
public static void Snippet1() { // <Snippet1> CustomBinding binding = new CustomBinding(); binding.Elements.Add(new HttpTransportBindingElement()); BindingParameterCollection paramCollection = new BindingParameterCollection(); IChannelListener <IReplyChannel> listener = binding.BuildChannelListener <IReplyChannel> (new Uri("http://localhost:8000/ChannelApp"), paramCollection); listener.Open(); IReplyChannel channel = listener.AcceptChannel(); Console.WriteLine("Listening for messages"); channel.Open(); RequestContext request = channel.ReceiveRequest(); Message msg = request.RequestMessage; Console.WriteLine("Message Received"); Console.WriteLine("Message Action: {0}", msg.Headers.Action); if (msg.Headers.Action == "hello") { Message reply = Message.CreateMessage(MessageVersion.Default, "wcf"); request.Reply(reply); } msg.Close(); channel.Close(); listener.Close(); // </Snippet1> }
static void Main(string[] args) { Uri listenUri = new Uri("http://127.0.0.1:3721"); Binding binding = new HttpBinding(); //创建、开启信道监听器 IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(listenUri); channelListener.Open(); //创建、开启回复信道 IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue); channel.Open(); //开始监听 while (true) { //接收输出请求消息 RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue); PrintRequestMessage(requestContext.RequestMessage); //消息回复 requestContext.Reply(CreateResponseMessage()); } }
static void Main(string[] args) { //创建监听地址和绑定 Uri listenUri = new Uri("http://localhost:9999/listener"); Binding binding = new BasicHttpBinding(); //通过监听地址创建信道监听器 IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(listenUri); //开启信道监听器 channelListener.Open(); //通过信道监听器对象创建回复信道对象 IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue); //开启回复信道 channel.Open(); Console.WriteLine("开始监听..."); while (true) { //通过回复信道创建请求文本对象 RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue); //输出请求文本对象里存储的从请求端发送的请求信息 Console.WriteLine("接收到请求消息:\n{0}", requestContext.RequestMessage); //通过请求文本对象的Reply()方法向请求端发送回复信息 requestContext.Reply(CreateReplyMessage(binding)); } }
public static void Main() { HttpTransportBindingElement el = new HttpTransportBindingElement(); BindingContext bc = new BindingContext( new CustomBinding(), new BindingParameterCollection(), new Uri("http://localhost:37564"), String.Empty, ListenUriMode.Explicit); IChannelListener <IReplyChannel> listener = el.BuildChannelListener <IReplyChannel> (bc); listener.Open(); IReplyChannel reply = listener.AcceptChannel(); reply.Open(); if (!reply.WaitForRequest(TimeSpan.FromSeconds(10))) { Console.WriteLine("No request reached here."); return; } Console.WriteLine("Receiving request ..."); RequestContext ctx = reply.ReceiveRequest(); if (ctx == null) { return; } Console.WriteLine("Starting reply ..."); ctx.Reply(Message.CreateMessage(MessageVersion.Default, "Ack")); }
static void Main(string[] args) { Uri listenUri = new Uri("http://127.0.0.1:1357/listener"); Binding binding = new BasicHttpBinding(); //创建,开启信道监听器 IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(listenUri); channelListener.Open(); //创建,开启回复信道 IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue); channel.Open(); //监听 while (true) { //接收请求消息 RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue); Console.WriteLine(requestContext.RequestMessage); //消息回复 requestContext.Reply(CreateReplyMessage(binding)); } }
public void Open() { HttpBinding binding = new HttpBinding(); this.ChannelListener = binding.BuildChannelListener <IReplyChannel>(this.BaseAddress); this.ChannelListener.Open(); IReplyChannel channnel = this.ChannelListener.AcceptChannel(); channnel.Open(); while (true) { RequestContext requestContext = channnel.ReceiveRequest(TimeSpan.MaxValue); Message message = requestContext.RequestMessage; MethodInfo method = message.GetType().GetMethod("GetHttpRequestMessage"); HttpRequestMessage request = (HttpRequestMessage)method.Invoke(message, new object[] { true }); Task <HttpResponseMessage> processResponse = base.SendAsync(request, new CancellationTokenSource().Token); processResponse.ContinueWith(task => { string httpMessageTypeName = "System.Web.Http.SelfHost.Channels.HttpMessage, System.Web.Http.SelfHost"; Type httpMessageType = Type.GetType(httpMessageTypeName); Message reply = (Message)Activator.CreateInstance(httpMessageType, new object[] { task.Result }); requestContext.Reply(reply); }); } }
public RequestContext ReceiveRequest(TimeSpan timeout) { RequestContext requestContext = innerReplyChannel.ReceiveRequest(timeout); // Read the context id from the incoming message. ReadContextId(requestContext.RequestMessage); return(requestContext); }
public void ReceiveRequestWithoutOpenChannel() { IChannelListener <IReplyChannel> listener = CreateListener(null, null); listener.Open(); IReplyChannel reply = listener.AcceptChannel(); reply.ReceiveRequest(); }
/// <summary> /// Listens for Udp request on 239.255.255.250:3702 /// </summary> /// <remarks>On initialization it sends a Discovery Hello message and listens on the Ws-Discovery /// endpoint for a request. When a request arrives it starts a UdpProcess thread that processes the message. /// The number of UdpProcessing threads are limited by the Device.MaxUdpRequestThreads property. /// </remarks> private void Listen() { // Create a duplicate message tester. WsMessageCheck messageCheck = new WsMessageCheck(40); while (!m_requestStop) { try { // If threads ara availble receive next message. If we are waiting on threads let the socket // buffer request until we get a thread. This will work until the reveice buffer is depleted // at which time request will be dropped if (m_threadManager.ThreadsAvailable == true) { RequestContext req = m_replyChannel.ReceiveRequest(); if (req != null) { WsWsaHeader header = req.Message.Header; if (header.MessageID != null && messageCheck.IsDuplicate(header.MessageID, header.From != null ? header.From.Address.AbsoluteUri : "")) { continue; } // Try to get a processing thread and process the request m_threadManager.StartNewThread(new WsUdpMessageProcessor(m_serviceEndpoints, req)); } else { System.Ext.Console.Write("UDP Receive returned 0 bytes"); } } else { System.Ext.Console.Write("Udp service host waiting for a thread..."); m_threadManager.ThreadEvent.WaitOne(); } } catch (SocketException se) { // Since the MF Socket does not have IOControl that would be used to turn off ICMP notifications // for UDP, catch 10054 and try to continue if ((SocketError)se.ErrorCode == SocketError.ConnectionReset) { Thread.Sleep(100); } } catch (Exception e) { System.Ext.Console.Write(e.Message + " " + e.InnerException); } } }
static void RunService() { //Step1: Create a custom binding with just TCP. BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements); //Step2: Use the binding to build the channel listener. IChannelListener <IReplyChannel> listener = binding.BuildChannelListener <IReplyChannel>( new Uri("http://localhost:8080/channelapp"), new BindingParameterCollection()); //Step3: Listening for messages. listener.Open(); Console.WriteLine( "Listening for incoming channel connections"); //Wait for and accept incoming connections. IReplyChannel channel = listener.AcceptChannel(); Console.WriteLine("Channel accepted. Listening for messages"); //Open the accepted channel. channel.Open(); //Wait for and receive a message from the channel. RequestContext request = channel.ReceiveRequest(); //Step4: Reading the request message. Message message = request.RequestMessage; Console.WriteLine("Message received"); Console.WriteLine("Message action: {0}", message.Headers.Action); string data = message.GetBody <string>(); Console.WriteLine("Message content: {0}", data); //Send a reply. Message replymessage = Message.CreateMessage( binding.MessageVersion, "http://contoso.com/someotheraction", data); request.Reply(replymessage); //Step5: Closing objects. //Do not forget to close the message. message.Close(); //Do not forget to close RequestContext. request.Close(); //Do not forget to close channels. channel.Close(); //Do not forget to close listeners. listener.Close(); }
static void RunListener (IReplyChannel reply) { if (!reply.WaitForRequest (TimeSpan.FromSeconds (10))) { Console.WriteLine ("No request reached here."); return; } Console.WriteLine ("Receiving request ..."); RequestContext ctx = reply.ReceiveRequest (); if (ctx == null) return; Console.WriteLine ("Starting reply ..."); ctx.Reply (Message.CreateMessage (MessageVersion.Default, "Ack")); }
static void Main(string[] args) { try { //建立和发送端相同的通道栈 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); bindingElements[1] = new HttpTransportBindingElement(); CustomBinding binding = new CustomBinding(bindingElements); //建立ChannelListener IChannelListener <IReplyChannel> listener = binding.BuildChannelListener <IReplyChannel>( new Uri("http://localhost:9090/RequestReplyService"), new BindingParameterCollection()); listener.Open(); //打开监听 //创建IReplyChannel IReplyChannel replyChannel = listener.AcceptChannel(); replyChannel.Open(); //打开通道 Console.WriteLine("开始接收消息"); //接收并打印消息 RequestContext requestContext = replyChannel.ReceiveRequest(); //会堵塞在这里 Console.WriteLine("接收到一条消息, action为{0}, body为{1}", requestContext.RequestMessage.Headers.Action, requestContext.RequestMessage.GetBody <string>()); //创建返回消息 Message response = Message.CreateMessage(binding.MessageVersion, "response", "response body"); //发送返回消息 requestContext.Reply(response); requestContext.Close(); replyChannel.Close(); listener.Close(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.WriteLine("Press any key to exit..."); Console.ReadKey(); } }
static void RunListener(IReplyChannel reply) { if (!reply.WaitForRequest(TimeSpan.FromSeconds(10))) { Console.WriteLine("No request reached here."); return; } Console.WriteLine("Receiving request ..."); RequestContext ctx = reply.ReceiveRequest(); if (ctx == null) { return; } Console.WriteLine("Starting reply ..."); ctx.Reply(Message.CreateMessage(MessageVersion.Default, "Ack")); }
static void Main(string[] args) { Uri address = new Uri("http://127.0.0.1:9999/messagingviabinding"); BasicHttpBinding binding = new BasicHttpBinding(); IChannelListener<IReplyChannel> channelListener = binding.BuildChannelListener<IReplyChannel>(address); channelListener.Open(); IReplyChannel channel = channelListener.AcceptChannel(); channel.Open(); Console.WriteLine("Begin to listen..."); while (true) { RequestContext context = channel.ReceiveRequest(new TimeSpan(1, 0, 0)); Console.WriteLine("Receive a request message:\n{0}", context.RequestMessage); Message replyMessage = Message.CreateMessage(MessageVersion.Soap11, "http://artech.messagingviabinding", "This is a mannualy created reply message for the purpose of testing"); context.Reply(replyMessage); } }
static void Main(string[] args) { Uri listenUri = new Uri("https://127.0.0.1:3721/listener"); BasicHttpBinding binding = new BasicHttpBinding(); binding.MessageEncoding = WSMessageEncoding.Mtom; binding.ListAllBindingElements(); binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport); Console.WriteLine("Transport"); binding.MessageEncoding = WSMessageEncoding.Mtom; binding.ListAllBindingElements(); binding = new BasicHttpBinding(BasicHttpSecurityMode.Message); binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.Certificate; Console.WriteLine("Message"); binding.MessageEncoding = WSMessageEncoding.Mtom; binding.ListAllBindingElements(); binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential); Console.WriteLine("TransportWithMessageCredential"); binding.MessageEncoding = WSMessageEncoding.Mtom; binding.ListAllBindingElements(); bool flag = binding.CanBuildChannelFactory <IDuplexChannel>(listenUri); //创建,开启信道监听器 IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(listenUri); channelListener.Open(); //创建开启回复信道 IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue); channel.Open(); //开始监听 while (true) { //接受输出请求消息 RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue); Console.WriteLine(requestContext.RequestMessage); //消息回复 requestContext.Reply(CreateReplyMessage(binding)); } }
public void CustomTransportDoesNotRequireMessageEncoding() { ReplyHandler replier = delegate(Message msg) { resmsg = msg; }; RequestReceiver receiver = delegate() { return(reqmsg); }; RequestSender sender = delegate(Message msg) { reqmsg = msg; CustomBinding br = new CustomBinding( new HandlerTransportBindingElement(replier, receiver)); IChannelListener <IReplyChannel> l = br.BuildChannelListener <IReplyChannel> ( new BindingParameterCollection()); l.Open(); IReplyChannel rch = l.AcceptChannel(); rch.Open(); Message res = Message.CreateMessage(MessageVersion.Default, "urn:succeeded"); rch.ReceiveRequest().Reply(res); rch.Close(); l.Close(); return(resmsg); }; CustomBinding bs = new CustomBinding( new HandlerTransportBindingElement(sender)); IChannelFactory <IRequestChannel> f = bs.BuildChannelFactory <IRequestChannel> ( new BindingParameterCollection()); f.Open(); IRequestChannel ch = f.CreateChannel(new EndpointAddress("urn:dummy")); ch.Open(); Message result = ch.Request(Message.CreateMessage(MessageVersion.Default, "urn:request")); }
private void BasicChannelTestA(bool customBinding) { IChannelListener <IReplyChannel> listener = null; IReplyChannel channel = null; ManualResetEvent done = new ManualResetEvent(false); Thread t = null; try { channel = this.OpenChannel(customBinding, out listener); Assert.IsNotNull(channel); Assert.IsNotNull(listener); t = new Thread(BasicChannelTests.SubmitRequests); t.Start(done); for (var cnt = 0; cnt < TestServiceCommon.Iterations; cnt++) { using (var context = channel.ReceiveRequest()) { this.SendResponse(context); } } channel.Close(TestServiceCommon.DefaultHostTimeout); listener.Close(TestServiceCommon.DefaultHostTimeout); } catch (Exception e) { channel.Abort(); listener.Abort(); Assert.Fail("Unexpected exception: " + e); } finally { if (t != null && !done.WaitOne(TestServiceCommon.DefaultHostTimeout)) { t.Abort(); } done.Dispose(); } }
static void Main(string[] args) { // Creating the Binding BasicHttpBinding binding = new BasicHttpBinding(); BindingParameterCollection parameters = new BindingParameterCollection(); IChannelListener <IReplyChannel> listener = binding.BuildChannelListener <IReplyChannel> (new Uri("http://<<ServerAddress>>:<<PortNumber>>"), parameters); listener.Open(); XmlSerializerWrapper wrapper = new XmlSerializerWrapper(typeof(TransmittedObject)); // Opening the Channel IReplyChannel channel = listener.AcceptChannel(); channel.Open(TimeSpan.MaxValue); // Waiting for and receiving the Message RequestContext r = channel.ReceiveRequest(TimeSpan.MaxValue); TransmittedObject to = r.RequestMessage.GetBody <TransmittedObject>(wrapper); // Processing the Message to.str = to.str + " World"; to.i = to.i + 1; // Creating the return Message Message m = Message.CreateMessage (MessageVersion.Soap11, "urn:test", to, wrapper); r.Reply(m, TimeSpan.MaxValue); // Cleaning up channel.Close(); }
static void Main(string[] args) { MyBinding binding = new MyBinding(); IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(new Uri("http://127.0.0.1:8888/messagingviabinding")); channelListener.Open(); while (true) { IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue); channel.Open(); RequestContext context = channel.ReceiveRequest(TimeSpan.MaxValue); Console.WriteLine("Receive a request message:\n{0}", context.RequestMessage); Message replyMessage = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "http://artech.messagingviabinding", "This is a mannualy created reply message for the purpose of testing"); context.Reply(replyMessage); channel.Close(); } }
static void Main(string[] args) { Uri listenUri = new Uri(@"http://127.0.0.1:9999/listener"); Binding binding = new BasicHttpBinding(); IChannelListener <IReplyChannel> channelListener = binding.BuildChannelListener <IReplyChannel>(listenUri); channelListener.Open(); IReplyChannel channel = channelListener.AcceptChannel(TimeSpan.MaxValue); channel.Open(); Console.WriteLine("开始监听..."); while (true) { RequestContext requestContext = channel.ReceiveRequest(TimeSpan.MaxValue); Console.WriteLine("接收到的请求消息: \n{0}", requestContext.RequestMessage); requestContext.Reply(CreateReplyMessage(binding)); } }
string LowLevelHttpConnection_SetupService() { IReplyChannel reply = listener.AcceptChannel(); reply.Open(); if (!reply.WaitForRequest(TimeSpan.FromSeconds(10))) { return("No request reached here."); } svcret = "Receiving request ..."; RequestContext ctx = reply.ReceiveRequest(); if (ctx == null) { return("No request context returned."); } svcret = "Starting reply ..."; ctx.Reply(Message.CreateMessage(MessageVersion.Default, "Ack")); return(null); // OK }
static void Main(string[] args) { try { //使用说明先启动input,然后在启动启动output。 //记得使用管理员启动VS,我在程序清单中添加了管理员权限。 //建立和发送端相同的通道栈 BindingElement[] bindingElements = new BindingElement[2]; bindingElements[0] = new TextMessageEncodingBindingElement(); //文本编码 bindingElements[1] = new HttpTransportBindingElement(); //HTTP传输 CustomBinding binding = new CustomBinding(bindingElements); //建立ChannelListner IChannelListener <IReplyChannel> listener = binding.BuildChannelListener <IReplyChannel>(new Uri("http://localhost:9090/RequestReplyService"), new BindingParameterCollection()); listener.Open();//打开监听 //创建IRepllyChannel IReplyChannel replyChannel = listener.AcceptChannel(); replyChannel.Open();//打开通道 Console.WriteLine("开始接受消息.."); //接收打印 RequestContext requestContext = replyChannel.ReceiveRequest(); Console.WriteLine($"接收到一条消息,action为:{requestContext.RequestMessage.Headers.Action},body为{requestContext.RequestMessage.GetBody<string>()}"); //创建返回消息 Message response = Message.CreateMessage(binding.MessageVersion, "response", "reponse body"); //发送返回消息 requestContext.Reply(response); //关闭 requestContext.Close(); //关闭通道 listener.Close(); //关闭监听 Console.Read(); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } finally { Console.Read(); } }
public RequestContext ReceiveRequest() => WrapRequestContext(_innerChannel.ReceiveRequest());
public RequestContext ReceiveRequest() { return(_innerChannel.ReceiveRequest()); }
public virtual RequestContext ReceiveRequest(TimeSpan timeout) { return(inner.ReceiveRequest(timeout)); }
public RequestContext ReceiveRequest(TimeSpan timeout) { var context = _innerChannel.ReceiveRequest(timeout); return(ProcessContext(context, timeout)); }
public override RequestContext ReceiveRequest() { return(Inspect(inner.ReceiveRequest())); }