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();
          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。

学会WCF之试错法——客户端调用基础的更多相关文章

  1. 学会WCF之试错法——安全配置报错分析

    安全配置报错分析 服务端配置 <system.serviceModel> <bindings> <wsHttpBinding> <binding name = ...

  2. 学会WCF之试错法——数据传输

    数据传输 服务契约 [ServiceContract] public interface IService { [OperationContract] string GetData(int value ...

  3. 学会WCF之试错法——超时

    服务契约 [ServiceContract] public interface IService { [OperationContract] string GetData(int value); [O ...

  4. WCF初探-10:WCF客户端调用服务

    创建WCF 服务客户端应用程序需要执行下列步骤: 获取服务终结点的服务协定.绑定以及地址信息 使用该信息创建 WCF 客户端 调用操作 关闭该 WCF 客户端对象 WCF客户端调用服务存在以下特点: ...

  5. Java与WCF交互(一):Java客户端调用WCF服务

    最近开始了解WCF,写了个最简单的Helloworld,想通过java客户端实现通信.没想到以我的基础,居然花了整整两天(当然是工作以外的时间,呵呵),整个过程大费周折,特写下此文,以供有需要的朋友参 ...

  6. Java与WCF交互(一):Java客户端调用WCF服务 【转】

    原文:http://www.cnblogs.com/downmoon/archive/2010/08/24/1807161.html 最近开始了解WCF,写了个最简单的Helloworld,想通过ja ...

  7. WCF系列教程之WCF客户端调用服务

    1.创建WCF客户端应用程序需要执行下列步骤 (1).获取服务终结点的服务协定.绑定以及地址信息 (2).使用该信息创建WCF客户端 (3).调用操作 (4).关闭WCF客户端对象 二.操作实例 1. ...

  8. 转载——Java与WCF交互(一):Java客户端调用WCF服务

    最近开始了解WCF,写了个最简单的Helloworld,想通过java客户端实现通信.没想到以我的基础,居然花了整整两天(当然是工作以外的时间,呵呵),整个过程大费周折,特写下此文,以供有需要的朋友参 ...

  9. Silverlight客户端调用WCF服务难题解疑

    一:解决办法 Silverlight客户端调用WCF服务在实际使用中经常会出现的问题就是无法直接应用类文件和配置文件.微软针对这一情况已经给出了解决办法.WCF开发框架可以帮助我们实现可靠性较高的跨平 ...

随机推荐

  1. Spring 基础入门(一)

    本文代码部分来自于<spring in action>,本文讲的是使用!! Spring 是为了解决什么 一个框架的存在是为了解决某个问题的,那么Spring这个框架是为了解决什么问题呢? ...

  2. 流畅的python和cookbook学习笔记(四)

    1.数字的四舍五入 对于简单的舍入运算,使用内置的 round(value, ndigits) 函数即可. round 函数返回离它最近的偶数.也就是说,对 1.5 或者 2.5 的舍入运算都会得到 ...

  3. MVC设计模式实现权限管理登录,超详细

    功能实现:在页面输入给定的用户名之一,可以显示当前用户的权限,也可以在页面更改该用户的权限,更新之后保存.像下面这样. 填写用户名提交: 显示用户AAA的权限: 修改权限(增加article3): 点 ...

  4. swoole安装

    转自:http://blog.csdn.net/u014207604/article/details/49926207 Windows 下安装 swoole 具体步骤: Swoole,原本不支持在Wi ...

  5. springMVC @Component-@Resource-@Repository-@Service-@Controller的区别和理解

    作用: @Component------------------------泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注.(Component-------成分; 组分; 零件) @R ...

  6. js两个字符串明明一样却判断显示不相等

    一.问题 两个字符串看起来一样.类型一样,判断str1==str2时返回false: 二.原因 字符串可能含有其他特殊字符:换行符(%D).空格(%20)...一般不显示. 三.如何判断 encode ...

  7. CSS基础-CSS三大特性

    继承性 层叠性 优先级 优先级权重 !important

  8. 您需要售后返修管理软件的N个理由

    一.减少人工成本. 二.提高工作效率. 三.其他管理成本降低. ▲传统采用人工登记或电子表格Excel管理 售后人员,他们日常的工作就是接件.维修.送修.记录.统计.跟件.寄送件.电话客户沟通.电话厂 ...

  9. Ubuntu / Raspberry 下切换GCC版本

    目前Ubuntu 自带的GCC版本为4.6,遗憾的是在实际使用时,反而版本越高越好问题越多,所以,一旦遇到编译问题时最好先检查你下载的工程里的readme,默认的编译器版本是否为当前的安装版本,如果不 ...

  10. 基于以太坊的Token开发步骤

    Token开发步骤 一.准备工具1.安装以太坊brew tap ethereum/ethereumbrew install ethereum2.node:brew install nodejs3.安装 ...