1.和服务端一样的契约接口ITest
namespace WcfService.Client { [ServiceContract] public interface ITest { [OperationContract] string GetHello(); } }
必须和服务端的契约一致
2.配置客服端配置文件
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <client> <endpoint address="http://localhost:8080/wcf/http" binding="basicHttpBinding" contract="WcfService.Client.ITest" name="testHttp"/> <endpoint address="net.tcp://localhost:8081/wcf/nettcp" binding="netTcpBinding" contract="WcfService.Client.ITest" name="testNetTcp"/> </client> </system.serviceModel> </configuration>
此处会使用client配置节在endpoint中的address填写服务中配置的绝对地址+相对地址,协议和契约要一致;
3.实例化出服务传输的对象实例
namespace WcfService.Client { class Program { static void Main(string[] args) { var porxy = new ChannelFactory<ITest>("testHttp"); ITest testHttp = porxy.CreateChannel(); Console.WriteLine(testHttp.GetHello()); porxy.Close(); porxy = new ChannelFactory<ITest>("testNetTcp"); ITest testNetTcp = porxy.CreateChannel(); Console.WriteLine(testNetTcp.GetHello()); porxy.Close(); Console.ReadLine(); } } }