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")); }
public void FullRequest() { EndpointIdentity identity = new X509CertificateEndpointIdentity(new X509Certificate2("Test/Resources/test.pfx", "mono")); EndpointAddress address = new EndpointAddress(new Uri("stream:dummy"), identity); Message mreq = Message.CreateMessage(MessageVersion.Default, "myAction"); Message mreply = null; XmlWriterSettings settings = new XmlWriterSettings(); settings.Indent = true; // listener setup ReplyHandler replyHandler = delegate(Message rinput) { mreply = rinput; }; RequestReceiver receiver = delegate() { return(mreq); }; IChannelListener <IReplyChannel> listener = CreateListener(replyHandler, receiver); listener.Open(); IReplyChannel reply = listener.AcceptChannel(); reply.Open(); RequestSender reqHandler = delegate(Message input) { try { // sync version somehow causes an infinite loop (!?) RequestContext ctx = reply.EndReceiveRequest(reply.BeginReceiveRequest(TimeSpan.FromSeconds(5), null, null)); // RequestContext ctx = reply.ReceiveRequest (TimeSpan.FromSeconds (5)); Console.Error.WriteLine("Acquired RequestContext."); ctx.Reply(input); } catch (Exception ex) { Console.Error.WriteLine("ERROR during processing a request in FullRequest()"); Console.Error.WriteLine(ex); Console.Error.Flush(); throw; } return(mreply); }; CustomBinding b = CreateBinding(reqHandler); IRequestChannel ch = ChannelFactory <IRequestChannel> .CreateChannel(b, address); ch.Open(); Console.Error.WriteLine("**** starting a request ****"); IAsyncResult async = ch.BeginRequest(mreq, null, null); Console.Error.WriteLine("**** request started. ****"); Message res = ch.EndRequest(async); }
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> }
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> }
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()); } }
/// <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 void ReceiveRequest() { // Seems like this method is invoked to send a reply // with related to "already created" SOAP fault. // // It is still not understandable that this delegate // is invoked as an infinite loop ... ReplyHandler handler = delegate(Message input) { Console.Error.WriteLine("Processing a reply."); // a:InvalidSecurity // An error occurred when verifying security for the message. Assert.IsTrue(input.IsFault); throw new ApplicationException(); }; Message msg = Message.CreateMessage(MessageVersion.Default, "myAction"); RequestReceiver receiver = delegate() { return(msg); }; IChannelListener <IReplyChannel> listener = CreateListener(handler, receiver); listener.Open(); IReplyChannel reply = listener.AcceptChannel(); reply.Open(); RequestContext ctx = reply.EndReceiveRequest(reply.BeginReceiveRequest(null, null)); }
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)); } }
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); }); } }
private void ChannelAccepted(IAsyncResult result) { try { IReplyChannel replyChannel = _replyChannelListener.EndAcceptChannel(result); if (replyChannel != null) { try { replyChannel.Open(); replyChannel.BeginReceiveRequest(RequestAccepted, replyChannel); } catch (Exception e) { Console.WriteLine(e); replyChannel.Abort(); } if (_replyChannelListener.State == CommunicationState.Opened) { this._replyChannelListener.BeginAcceptChannel(ChannelAccepted, _replyChannelListener); } } } catch (Exception e) { Console.WriteLine(e); this._replyChannelListener.Abort(); this._replyChannelListener = null; } }
public void AcceptChannel(IAsyncResult ar) { IChannelListener <IReplyChannel> listener = ar.AsyncState as IChannelListener <IReplyChannel>; IReplyChannel channel = listener.EndAcceptChannel(ar); channel.Open(); channel.BeginReceiveRequest(AcceptRequest, channel); }
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(); }
public void Start(ServerBindingContext ctx) { if (1 == Interlocked.Increment(ref m_refcount)) { m_requestStop = false; m_replyChannel = m_binding.CreateServerChannel(ctx); m_replyChannel.Open(); m_thread = new Thread(new ThreadStart(this.Listen)); m_thread.Start(); } }
public IChannelListenerPage() { InitializeComponent(); ///监听者 IChannelListener <IReplyChannel> listener = binding.BuildChannelListener <IReplyChannel>(new Uri("http://localhost:9090/RequestReplyService"), new BindingParameterCollection()); listener.Open();//打开 //创建IReplyChannel IReplyChannel replyChannel = listener.AcceptChannel(); replyChannel.Open();//打开IReplyChannel应答通道 }
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 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")); }
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)); } }
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(); } }
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 }
public static void Main() { HttpTransportBindingElement el = new HttpTransportBindingElement(); IChannelListener <IReplyChannel> listener = el.BuildChannelListener <IReplyChannel> ( new BindingContext(new CustomBinding(), new BindingParameterCollection(), new Uri("http://localhost:37564"), String.Empty, ListenUriMode.Explicit)); IChannelFactory <IRequestChannel> factory = el.BuildChannelFactory <IRequestChannel> ( new BindingContext(new CustomBinding(), new BindingParameterCollection())); listener.Open(); factory.Open(); IRequestChannel request = factory.CreateChannel( new EndpointAddress("http://localhost:37564")); IReplyChannel reply = listener.AcceptChannel(); reply.Open(); request.Open(); new Thread(delegate() { try { RunListener(reply); } catch (Exception ex) { Console.WriteLine(ex); } }).Start(); Message msg = request.Request(Message.CreateMessage( MessageVersion.Default, "Echo"), TimeSpan.FromSeconds(15)); XmlWriterSettings settings = new XmlWriterSettings(); settings.OmitXmlDeclaration = true; StringWriter sw = new StringWriter(); using (XmlWriter w = XmlWriter.Create(sw, settings)) { msg.WriteMessage(w); } Console.WriteLine(sw); }
public void Open() { _innerChannel.Open(); }
protected override void OnOpen(TimeSpan timeout) { inner.Open(timeout); }