Code extracted from https://web.archive.org/web/20141027055124/http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication.
Original article has been lost to bit rot, but the Wayback Machine was linked to via this StackOverflow answer
Code extracted from https://web.archive.org/web/20141027055124/http://tech.pro/tutorial/855/wcf-tutorial-basic-interprocess-communication.
Original article has been lost to bit rot, but the Wayback Machine was linked to via this StackOverflow answer
| using System; | |
| using System.ServiceModel; | |
| using System.ServiceModel.Channels; | |
| namespace WCFClient | |
| { | |
| [ServiceContract] | |
| public interface IStringReverser | |
| { | |
| [OperationContract] | |
| string ReverseString(string value); | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| ChannelFactory<IStringReverser> httpFactory = | |
| new ChannelFactory<IStringReverser>( | |
| new BasicHttpBinding(), | |
| new EndpointAddress( | |
| "http://localhost:8000/Reverse")); | |
| ChannelFactory<IStringReverser> pipeFactory = | |
| new ChannelFactory<IStringReverser>( | |
| new NetNamedPipeBinding(), | |
| new EndpointAddress( | |
| "net.pipe://localhost/PipeReverse")); | |
| IStringReverser httpProxy = | |
| httpFactory.CreateChannel(); | |
| IStringReverser pipeProxy = | |
| pipeFactory.CreateChannel(); | |
| while (true) | |
| { | |
| string str = Console.ReadLine(); | |
| Console.WriteLine("http: " + | |
| httpProxy.ReverseString(str)); | |
| Console.WriteLine("pipe: " + | |
| pipeProxy.ReverseString(str)); | |
| } | |
| } | |
| } | |
| } |
| using System; | |
| using System.ServiceModel; | |
| namespace WCFServer | |
| { | |
| [ServiceContract] | |
| public interface IStringReverser | |
| { | |
| [OperationContract] | |
| string ReverseString(string value); | |
| } | |
| public class StringReverser : IStringReverser | |
| { | |
| public string ReverseString(string value) | |
| { | |
| char[] retVal = value.ToCharArray(); | |
| int idx = 0; | |
| for (int i = value.Length - 1; i >= 0; i--) | |
| retVal[idx++] = value[i]; | |
| return new string(retVal); | |
| } | |
| } | |
| class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| using (ServiceHost host = new ServiceHost( | |
| typeof(StringReverser), | |
| new Uri[]{ | |
| new Uri("http://localhost:8000"), | |
| new Uri("net.pipe://localhost") | |
| })) | |
| { | |
| host.AddServiceEndpoint(typeof(IStringReverser), | |
| new BasicHttpBinding(), | |
| "Reverse"); | |
| host.AddServiceEndpoint(typeof(IStringReverser), | |
| new NetNamedPipeBinding(), | |
| "PipeReverse"); | |
| host.Open(); | |
| Console.WriteLine("Service is available. " + | |
| "Press <ENTER> to exit."); | |
| Console.ReadLine(); | |
| host.Close(); | |
| } | |
| } | |
| } | |
| } |