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> }
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 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 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 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); }
// It is almost identical to http-low-level-binding public void LowLevelHttpConnection () { HttpTransportBindingElement lel = new HttpTransportBindingElement (); // Service BindingContext lbc = new BindingContext ( new CustomBinding (), new BindingParameterCollection (), new Uri ("http://localhost:37564"), String.Empty, ListenUriMode.Explicit); listener = lel.BuildChannelListener<IReplyChannel> (lbc); try { listener.Open (); svcret = ""; Thread svc = new Thread (delegate () { try { svcret = LowLevelHttpConnection_SetupService (); } catch (Exception ex) { svcret = ex.ToString (); } }); svc.Start (); // Client code goes here. HttpTransportBindingElement el = new HttpTransportBindingElement (); BindingContext ctx = new BindingContext ( new CustomBinding (), new BindingParameterCollection ()); IChannelFactory<IRequestChannel> factory = el.BuildChannelFactory<IRequestChannel> (ctx); factory.Open (); IRequestChannel request = factory.CreateChannel ( new EndpointAddress ("http://localhost:37564")); request.Open (); try { try { Message reqmsg = Message.CreateMessage ( MessageVersion.Default, "Echo"); // sync version does not work here. Message msg = request.Request (reqmsg, TimeSpan.FromSeconds (5)); using (XmlWriter w = XmlWriter.Create (TextWriter.Null)) { msg.WriteMessage (w); } if (svcret != null) Assert.Fail (svcret.Length > 0 ? svcret : "service code did not finish until this test expected."); } finally { if (request.State == CommunicationState.Opened) request.Close (); } } finally { if (factory.State == CommunicationState.Opened) factory.Close (); } } finally { if (listener.State == CommunicationState.Opened) listener.Close (); } }
// It is almost identical to http-low-level-binding public void LowLevelHttpConnection() { HttpTransportBindingElement lel = new HttpTransportBindingElement(); // Service BindingContext lbc = new BindingContext( new CustomBinding(), new BindingParameterCollection(), new Uri("http://localhost:" + NetworkHelpers.FindFreePort()), String.Empty, ListenUriMode.Explicit); listener = lel.BuildChannelListener <IReplyChannel> (lbc); try { listener.Open(); svcret = ""; Thread svc = new Thread(delegate() { try { svcret = LowLevelHttpConnection_SetupService(); } catch (Exception ex) { svcret = ex.ToString(); } }); svc.Start(); // Client code goes here. HttpTransportBindingElement el = new HttpTransportBindingElement(); BindingContext ctx = new BindingContext( new CustomBinding(), new BindingParameterCollection()); IChannelFactory <IRequestChannel> factory = el.BuildChannelFactory <IRequestChannel> (ctx); factory.Open(); IRequestChannel request = factory.CreateChannel( new EndpointAddress("http://localhost:" + NetworkHelpers.FindFreePort())); request.Open(); try { try { Message reqmsg = Message.CreateMessage( MessageVersion.Default, "Echo"); // sync version does not work here. Message msg = request.Request(reqmsg, TimeSpan.FromSeconds(5)); using (XmlWriter w = XmlWriter.Create(TextWriter.Null)) { msg.WriteMessage(w); } if (svcret != null) { Assert.Fail(svcret.Length > 0 ? svcret : "service code did not finish until this test expected."); } } finally { if (request.State == CommunicationState.Opened) { request.Close(); } } } finally { if (factory.State == CommunicationState.Opened) { factory.Close(); } } } finally { if (listener.State == CommunicationState.Opened) { listener.Close(); } } }