服务契约

[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);
}

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

 

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

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

转载与引用请注明出处。

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

最新文章

  1. 解决 PLSQL Developer无法连接数据库
  2. 使用 CoordinatorLayout 实现复杂联动效果
  3. HTML相关问题
  4. Mecanim的Avater
  5. StringUtils cannot be resolved
  6. linux操作文本文件
  7. 熬之滴水穿石:Spring--精简的J2EE(5)
  8. 开涛spring3(9.4) - Spring的事务 之 9.4 声明式事务
  9. Hi3516EV300专业型HD IP Camera SoC
  10. 新更新,又是一年了。这次记录下关于android版的WeiboDemo的问题
  11. docker 安装入门
  12. django-枚举类型扩展方法
  13. 物联网架构成长之路(16)-SpringCloud从入门到吹水
  14. mysql无法生成log文件
  15. org.springframework.web.bind.annotation重定向的问题
  16. 如何解析读取excel数据
  17. 897. Increasing Order Search Tree
  18. 如何获取app的activity
  19. C++的惨痛教训(未完待续)
  20. 【bzoj2330】[SCOI2011]糖果 差分约束系统

热门文章

  1. Linux学习笔记之(2)~linux目录树概览和说明
  2. docker 安装ElasticSearch head
  3. 简单的CRUD(一)
  4. js分离html代码的body内外部分
  5. C中的私有成员
  6. axios拦截器请求头携带token
  7. 理解JS表达式
  8. visual studio 2013的C++开发环境不错--vs2013安装试用手记
  9. JS高级程序设计第三版——变量、作用域和内存问题
  10. react-native-echarts