TcpTrace路由解析,参考页面-http://www.cnblogs.com/artech/archive/2008/09/19/1294227.html。
TcpTrace工具下载地址:http://www.piaodown.com/soft/43538.htm
原理:模拟路由的方式进行客户端的消息转发,回复。如图:
代码结构如下:
分别为ICalculator:
using System.Linq; using System.Text; using System.ServiceModel; namespace Artech.TcpTraceDemo.Contracts { [ServiceContract(Namespace = "http://www.artech.com/")] public interface ICalculator { [OperationContract] double Add(double x, double y); } }
Service:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Artech.TcpTraceDemo.Contracts; namespace Artech.TcpTraceDemo.Services { public class CalculatorService:ICalculator { public double Add(double x, double y) { return x + y; } } }
Hosting
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using Artech.TcpTraceDemo.Services; namespace Artech.TcpTraceDemo.Hosting { class Program { static void Main(string[] args) { using (ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService))) { serviceHost.Open(); Console.Read(); } } } }
Hosting,Appconfig
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <customBinding> <binding name="SimpleBinding"> <textMessageEncoding /> <httpTransport /> </binding> </customBinding> </bindings> <services> <service name="Artech.TcpTraceDemo.Services.CalculatorService"> <endpoint address="http://127.0.0.1:9999/CalculatorService" binding="customBinding" bindingConfiguration="SimpleBinding" contract="Artech.TcpTraceDemo.Contracts.ICalculator" listenUri="http://127.0.0.1:8888/CalculatorService"></endpoint> </service> </services> </system.serviceModel> </configuration>
Clients:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Artech.TcpTraceDemo.Contracts; using System.ServiceModel; namespace Artech.TcpTraceDemo.Clients { class Program { static void Main(string[] args) { using (ChannelFactory<ICalculator> ChannelFactory = new ChannelFactory<ICalculator>("CalculatorService")) { ICalculator calculator = ChannelFactory.CreateChannel(); using (calculator as IDisposable) { Console.WriteLine("x+y={2} where x={0} and y={1}", 1, 2, calculator.Add(1,2)); } } Console.ReadLine(); } } }
Clients,Appconfig
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <customBinding> <binding name="SimpleBinding"> <textMessageEncoding /> <httpTransport /> </binding> </customBinding> </bindings> <client> <endpoint address="http://127.0.0.1:9999/CalculatorService" binding="customBinding" bindingConfiguration="SimpleBinding" contract="Artech.TcpTraceDemo.Contracts.ICalculator" name="CalculatorService"></endpoint> </client> </system.serviceModel> </configuration>