前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >学会WCF之试错法——客户端调用基础

学会WCF之试错法——客户端调用基础

作者头像
甜橙很酸
发布2018-03-08 11:15:00
1.4K0
发布2018-03-08 11:15:00
举报
文章被收录于专栏:DOTNETDOTNET

1当客户端调用未返回结果时,服务不可用(网络连接中断,服务关闭,服务崩溃等)

客户端抛出异常

异常类型:CommunicationException

InnerException:

Message:

接收对 http://localhost/S 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参见服务器日志。

Stacktrace:

Server stack trace:

在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)

在 System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

在 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)

在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)

在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)

在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)

在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]:

在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)

在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)

在 Client.IService.GetData(Int32 value)

在 Client.ServiceClient.GetData(Int32 value) 位置 e:\projgxz_myself\WCF_Find_Error\Client\ServiceProxy.cs:行号 52

在 Client.ServiceProxy.GetData(Int32 value) 位置 e:\projgxz_myself\WCF_Find_Error\Client\ServiceProxy.cs:行号 19

在 Client.Program.Main(String[] args) 位置 e:\projgxz_myself\WCF_Find_Error\Client\Program.cs:行号 17

2 服务地址与元数据访问地址

服务器A(192.168.107.13)上部署服务,服务端终结点配置为:http://localhost/S,元数据检索URI配置为http://localhost/S

在客户端(192.168.20.104)上访问A的服务,查看元数据。客户端浏览器输入网址:http://192.168.107.13/S

输出页面为:

点击页面链接:无法访问到A机器服务的元素据,这是合理的因为localhost代表本机的ip,此刻操作是在客户端的机器上,而不在服务器上;客户端的机器上并没有这个服务,所以服务端终结点配置为:http://localhost/S,元数据检索URI配置为http://192.168.107.13/S

当服务端终结点和元数据访问地址不统一时,服务端通信对象无法打开。

3对比无法获得异常真实原因的两种用法

服务端方法:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]
    public class Service : IService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
}

客户端代理:

public class ServiceProxy
    {
        public string GetData(int value)
        {
            string ret = null;
            ServiceClient client = null;
            try
            {
                client = new ServiceClient();
                ret = client.GetData(value);
                client.Close();
            }
            catch
            {
                if (client != null)
                {
                    client.Abort();
                }
                throw;
            }
            return ret;
        }
}

[ServiceContractAttribute(ConfigurationName = "IService")]
    public interface IService
    {

        [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/GetData", ReplyAction = "http://tempuri.org/IService/GetDataResponse")]
        string GetData(int value);
}

public class ServiceClient : System.ServiceModel.ClientBase<IService>, IService
    {

        public ServiceClient()
        {
        }

        public string GetData(int value)
        {
            return base.Channel.GetData(value);
        }
}

客户端调用:

方式一

直接调用ServiceClient,调用数据返回后关闭客户端。

static void Main(string[] args)
{
            try
            {
          ServiceClient clients = new ServiceClient();
                clients.GetData(1);
          clients.Close();
            }
            catch (Exception ex)
            {
                clients.Abort();
            }
}

方式二:

在Main方法中将下面的代码用try...catch包起来。

                ServiceProxy proxy = new ServiceProxy();
                proxy.GetData(1);

方式三:

在Main方法中将下面的代码用try...catch包起来。

 using (ServiceClient client = new ServiceClient())
        {
                    client.GetData(1);
                }

方法一和方法二可以返回真实的原因,而方法三不能,他们的区别在于,方法三在客户端捕获异常之前关闭了客户端对象,而其他两种方式则是在获得异常信息后才关闭客户端对象的,所以由上面的测试又可得出WCF客户端程序中慎用using。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-11-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档