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通信方式

文章

 

 

 

转载于:https://www.cnblogs.com/llcdbk/p/6374625.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值