WCF使用地址去调用服务端的方法
前面的章节已经讲过了WCF的代码和SVC页面的分离,这里是分离后,客户端调用代码如下:
try { var myBinding = new BasicHttpBinding(); var myEndpoint = new EndpointAddress( new Uri("http://localhost:12857/UserService.svc")); var myChannelFactory = new ChannelFactory<IUserBussiness>(myBinding, myEndpoint); IUserBussiness client = myChannelFactory.CreateChannel(); var res = client.DoWork("1111"); } catch (Exception ex) { //do something proper here }
其中,
BasicHttpBinding使用的是明文,使用的是http基础通信协议,WsHttpBinding使用的是加密的SOPA协议,
对于
BasicHttpBinding可以有如下设定:
public BasicHttpBinding Binging() { _cmdServiceBinding = new BasicHttpBinding(); _cmdServiceBinding.OpenTimeout = new TimeSpan(0, 1, 30); _cmdServiceBinding.CloseTimeout = new TimeSpan(0, 1, 30); _cmdServiceBinding.SendTimeout = new TimeSpan(1, 30, 0); _cmdServiceBinding.ReceiveTimeout = new TimeSpan(0, 3, 0); _cmdServiceBinding.MaxBufferSize = int.MaxValue; _cmdServiceBinding.MaxBufferPoolSize = int.MaxValue; _cmdServiceBinding.MaxReceivedMessageSize = int.MaxValue; _cmdServiceBinding.MaxBufferPoolSize = int.MaxValue; _cmdServiceBinding.ReaderQuotas.MaxBytesPerRead = int.MaxValue; _cmdServiceBinding.ReaderQuotas.MaxStringContentLength = int.MaxValue; _cmdServiceBinding.ReaderQuotas.MaxNameTableCharCount = int.MaxValue; _cmdServiceBinding.ReaderQuotas.MaxArrayLength = int.MaxValue; return _cmdServiceBinding; }
WCF的配置文件不变,但是
WsHttpBinding如果想要使用的话,需要进行一些设置,不然会爆出
Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.。类似这样的错误,WSHTTPBINDING已经专门有写过详情请查看
编写WsHttpBinding的WCF通信方式
文章
积累小的知识,才能成就大的智慧,希望网上少一些复制多一些原创有用的答案