服务契约

[ServiceContract]
public interface IService
{
[OperationContract]
string GetData(int value); [OperationContract]
string GetString(string value); [OperationContract]
void Upload(Request request);
} [MessageContract]
public class Request
{
[MessageHeader(MustUnderstand = true)]
public string FileName { get; set; } [MessageBodyMember(Order = )]
public Stream Content {get;set;}
}

服务

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class Service : IService
{
public string GetData(int value)
{
//Thread.Sleep(120000);
return string.Format("You entered: {0}", value);
} public string GetString(string value)
{
//Thread.Sleep(120000);
return string.Format("You entered: {0}", value);
} public void Upload(Request request)
{
try
{
StreamReader sr = new StreamReader(request.Content, Encoding.GetEncoding("GB2312"));
StreamWriter sw = new StreamWriter("E:\\" + request.FileName + ".txt", false, Encoding.GetEncoding("GB2312"));
while (!sr.EndOfStream)
{
sw.WriteLine(sr.ReadLine());
//Thread.Sleep(5000);
}
sr.Close();
sw.Close();
}
catch (Exception ex)
{ } }
}

服务配置

<system.serviceModel>
<services>
<service name="WCF_Find_Error_Lib.Service">
<endpoint address="" binding="basicHttpBinding" contract="WCF_Find_Error_Lib.IService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost/S" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

客户端代理

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;
} public string GetString(string value)
{
string ret = null;
ServiceClient client = null;
try
{
client = new ServiceClient();
ret = client.GetString(value);
client.Close();
}
catch
{
if (client != null)
{
client.Abort();
}
throw;
}
return ret;
}
public void Upload(Request request)
{
ServiceClient client = null;
try
{
client = new ServiceClient();
client.Upload(request);
client.Close();
}
catch
{
if (client != null)
{
client.Abort();
}
throw;
}
} } [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); [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/GetString", ReplyAction = "http://tempuri.org/IService/GetStringResponse")]
string GetString(string value); [System.ServiceModel.OperationContractAttribute(Action = "http://tempuri.org/IService/Upload", ReplyAction = "http://tempuri.org/IService/UploadResponse")]
void Upload(Request request);
}
[MessageContract]
public class Request
{
[MessageHeader(MustUnderstand = true)]
public string FileName { get; set; } [MessageBodyMember(Order = 1)]
public Stream Content { get; set; }
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class ServiceClient : System.ServiceModel.ClientBase<IService>, IService
{ public ServiceClient()
{
} public string GetData(int value)
{
return base.Channel.GetData(value);
} public string GetString(string value)
{
return base.Channel.GetString(value);
} public void Upload(Request request)
{
base.Channel.Upload(request);
}
}

客户端配置

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/S" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService" contract="IService"
name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>

1 客户端调用超时

 

运行客户端,执行调用

ServiceProxy proxy = new ServiceProxy();

string s = proxy.GetData(1);

 

通过配置sendTimeout参数设定超时时间,超时时间默认为1分钟,上述配置中采用了默认超时时间。

Message

请求通道在等待 00:00:59.9469970 以后答复时超时。增加传递给请求调用的超时值,或者增加绑定上的 SendTimeout 值。分配给此操作的时间可能已经是更长超时的一部分。

 

Stacktrace:

Server stack trace:

在 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

增大客户端调用超时时间,可解决超时问题

例如,超时时间设置为10分钟,满足此次调用需求。

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" sendTimeout="00:10:00"/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/S" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService" contract="IService"
name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>

2 非活动状态的最大时间间隔

通过配置receiveTimeout设定时间间隔,默认值为 10 分钟。

服务实例化模式改为为会话模式:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Reentrant)]

服务端配置

<netTcpBinding>
<binding name="NetTcpBinding_IService" maxBufferSize="270000" maxReceivedMessageSize="270000" transferMode="Buffered" receiveTimeout="00:00:10">
<readerQuotas maxStringContentLength="240000"/>
<reliableSession enabled="true" inactivityTimeout="00:00:10"/>
</binding>
</netTcpBinding>

客户端配置

<netTcpBinding>
<binding name="NetTcpBinding_IService" sendTimeout="00:00:10" maxBufferSize="2700000" maxReceivedMessageSize="2700000" transferMode="Buffered" >
<readerQuotas maxStringContentLength="240000"/>
</binding>
</netTcpBinding>

客户端调用

using (ServiceClient client = new ServiceClient())
{
StreamReader sr = new StreamReader("D:\\CSBMTEMP.txt", Encoding.Default);
string str = sr.ReadToEnd();
sr.Close();
client.GetString(str); Thread.Sleep();
client.GetString(str);
}

运行客户端程序,成功捕获异常

 

上述异常中给出的错误信息并未指出具体的异常原因,所以从中很难推测是由于超时时间设置问题。遇到此类问题只能根据经验逐项排查,当然这是很浪费时间的,尤其是对于复杂的程序,更是如此。

-----------------------------------------------------------------------------------------

转载与引用请注明出处。

时间仓促,水平有限,如有不当之处,欢迎指正。

学会WCF之试错法——超时的更多相关文章

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

    1当客户端调用未返回结果时,服务不可用(网络连接中断,服务关闭,服务崩溃等) 客户端抛出异常 异常类型:CommunicationException InnerException: Message: ...

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

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

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

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

  4. WCF服务调用超时错误:套接字连接已中止。这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的。本地套接字超时是“00:05:30”(已解决)

    问题: 线上正式环境调用WCF服务正常,但是每次使用本地测试环境调用WCF服务时长就是出现:套接字连接已中止.这可能是由于处理消息时出错或远程主机超过接收超时或者潜在的网络资源问题导致的.本地套接字超 ...

  5. WCF返回null超时

    Message.CreateMessage(msg.Version, msg.Headers.Action + "Response", DealObject("错误信息& ...

  6. 从Web Service和Remoting Service引出WCF服务

    本篇先通过Web Service和Remoting Service创建服务,抛砖引玉,再体验WCF服务.首先一些基本面: 什么是WCF? Windows Communication Foundatio ...

  7. 关于webapi调用wcf并发假死的分析

    原来IDFA(IOS推广获取到用户IOS手机的唯一标识,如果不刷机的话跟安卓的IMEI一样)在公司正常的页面是公用用一个网站和数据库的. 起初怀疑并发数太多,把数据库连接池的数量从一百设置到三百,确实 ...

  8. 中小研发团队架构实践之生产环境诊断工具WinDbg 三分钟学会.NET微服务之Polly 使用.Net Core+IView+Vue集成上传图片功能 Fiddler原理~知多少? ABP框架(asp.net core 2.X+Vue)模板项目学习之路(一) C#程序中设置全局代理(Global Proxy) WCF 4.0 使用说明 如何在IIS上发布,并能正常访问

    中小研发团队架构实践之生产环境诊断工具WinDbg 生产环境偶尔会出现一些异常问题,WinDbg或GDB是解决此类问题的利器.调试工具WinDbg如同医生的听诊器,是系统生病时做问题诊断的逆向分析工具 ...

  9. WCF 超时情形

    在做WCF开发时,会经常碰到超时的情况,总结了一下,主要是由一下原因引起: 1.客户端没有正确地Close. 确保每次客户端调用完毕之后,就要调用Close,保证连接数. 另外,服务端配置最大连接数: ...

随机推荐

  1. Hibernate学习2--对象的三种状态以及映射关系的简单配置

    上篇hibernate的博客总体简单梳理了对象持久化的一些思想以及hibernate中对象持久化化的方法,下面说说对象持久化过程的三种状态. 一.hibernate缓存的概念 1.session与缓存 ...

  2. Service的启动流程源码跟踪

    前言: 当我们在一个Activity里面startService的时候,具体的执行逻辑是怎么样的?需要我们一步步根据源码阅读. 在阅读源码的时候,要关注思路,不要陷在具体的实现细节中,一步步整理代码的 ...

  3. Java 8 读取文件

    以前的Java版本中读取文件非常繁琐,现在比较简单.使用Java8的Files以及Lambda,几句代码就可以搞定. public static String getXml() { StringBuf ...

  4. Effective C++ .14 智能指针的拷贝与deleter函数

    #include <iostream> #include <cstdlib> #include <memory> using namespace std; clas ...

  5. Goodbye Bingshen

    在uoj上打的第二场比赛......还凑合(卧槽C题80分没了QAQ 第一次接触交互题还挺好玩的哈哈 可能是人比较多吧.....rating涨了不少...... 现在我rating正好比lrd高1哈哈 ...

  6. 关于background定位

    直到刚刚我才发现我小瞧了background定位 因为项目里需要显示隐藏的按钮上有两个图标 开始想了几种方法都不行,然后突然就想到了background定位 果断试了一下 <input type ...

  7. 2-5 Sass 的 @ 规则

    @import Sass 支持所有 CSS3 的 @ 规则, 以及一些 Sass 专属的规则,也被称为"指令(directives)". 这些规则在 Sass 中具有不同的功效,详 ...

  8. 04_zookeeper的watcher机制

    [watcher简述] * zk针对每个节点的操作,都会有一个监督者:watcher * 当监控的某个对象(znode)发生了变化,则出发watcher * zk中的watcher是一次性的,出发后立 ...

  9. 139.00.009提高Github Clone速度

    @(139 - Environment Settings | 环境配置) Method 1 :SS+系统内置代理 用 git 内置代理,直接走系统中运行的代理工具中转,比如,你的 SS 本地端口是 1 ...

  10. 谈谈CSS性能

    CSS性能优化 1.衡量属性和布局的消耗代价: 2.探索W3C的性能优化新规范: 3.用测试数据判断优化策略. 慎重选择高消耗的样式 1.box-shadows; 2.border-radius; 3 ...