【问题标题】:WCF client throws System.TimeoutException after 20minsWCF 客户端在 20 分钟后抛出 System.TimeoutException
【发布时间】:2013-06-06 09:58:32
【问题描述】:

我将 WCF 用于 IPC。只有一个服务器和一个客户端,.Net3.5 和在同一台机器上运行,没有提升,没有任何系统设置更改(在正常使用下)。

这在所有客户端(从 WinXp 到 Win8)中都可以正常运行。但只有在一台机器上,经过大约 20 分钟的通信后,客户端应用程序中才会出现 TimeoutException。此行为无法在其他地方复制,但每次都以相同的方式在客户端中复制。在应用程序生命周期中,有几个调用被打了,我会说至少有几百个。堆栈位于问题的底部。

服务合同

[ServiceContract]
interface IExampleService
{
    [OperationContract]
    string TestSample();
}

服务合同实施

[ServiceBehavior(IncludeExceptionDetailInFaults = true,
                 InstanceContextMode = InstanceContextMode.Single)]
internal class ExampleService : IExampleService
{
    // IExampleService implementation
}

服务器初始化(总是在客户端之前启动)

// Server initialization
Uri baseAddress = new Uri("net.pipe://127.0.0.1/Example");
NetNamedPipeBinding binding = new NetNamedPipeBinding();
var service = new ExampleService();
ServiceHost serviceHost = new ServiceHost(service, baseAddress);
serviceHost.AddServiceEndpoint(typeof(IExampleService), binding, baseAddress);
serviceHost.Open();

客户端初始化

// Client initialization
EndpointAddress address = new EndpointAddress(new Uri("net.pipe://127.0.0.1/Example"));
NetNamedPipeBinding binding = new NetNamedPipeBinding();
ChannelFactory<IExampleService> factory = new ChannelFactory<IExampleService>(binding, address);
IExampleService exampleService = factory.CreateChannel();

这是客户端上的堆栈(手头唯一的线索):

serverResponseWorker_DoWork System.TimeoutException: The open operation did not complete within the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout. 
at System.ServiceModel.Channels.PipeConnection.WaitForSyncRead(TimeSpan timeout, Boolean traceExceptionsAsErrors)
at System.ServiceModel.Channels.PipeConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
at System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout)
at System.ServiceModel.Channels.ConnectionUpgradeHelper.InitiateUpgrade(StreamUpgradeInitiator upgradeInitiator, IConnection& connection, ClientFramingDecoder decoder, IDefaultCommunicationTimeouts defaultTimeouts, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.SendPreamble(IConnection connection, ArraySegment`1 preamble, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.DuplexConnectionPoolHelper.AcceptPooledConnection(IConnection connection, TimeoutHelper& timeoutHelper)
at System.ServiceModel.Channels.ConnectionPoolHelper.EstablishConnection(TimeSpan timeout)
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
--- End of inner exception stack trace ---

Server stack trace: 
at System.ServiceModel.Channels.ClientFramingDuplexSessionChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.CallOpenOnce.System.ServiceModel.Channels.ServiceChannel.ICallOnce.Call(ServiceChannel channel, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.CallOnceManager.CallOnce(TimeSpan timeout, CallOnceManager cascade)
at System.ServiceModel.Channels.ServiceChannel.EnsureOpened(TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs)
at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

服务器应用程序在客户端发生异常后继续运行,服务器似乎没有抛出任何异常。有几个细节被省略了,请问还有什么需要的。

这可能是什么原因造成的?我很确定它在特定的机器上,否则我们真的很幸运能在数百个客户端上运行。

【问题讨论】:

  • // IExampleService 实现 --- 你在这个实现中做什么?这是一个 20 分钟的过程?
  • 不,当然!但是既然你指出了这一点,我会在响应过程中检查循环依赖。
  • @VikramBose 我浏览了代码,该服务仅使用其类中提供的数据进行响应,因此不可能出现死锁。它只响应作为它的字段的字符串或数字。所以这不可能是问题的根源。有什么建议吗?
  • 您能否在特定机器中查找 machine.config 文件以了解任何特殊情况。

标签: c# wcf timeoutexception


【解决方案1】:

客户端没有收到服务器的响应,所以会超时

  • 检查服务器端是否发生异常

【讨论】:

  • 你能详细说明一下no2吗?
  • 我不知道您的实现是什么,实现将是从数据库中检索数据意味着当时不获取全部数据,使用分页(例如:您的记录大小是 200000 每个请求需要 1000 条记录)。
【解决方案2】:
       NetNamedPipeBinding binding = new NetNamedPipeBinding();
            CustomBinding pipeBinding = new CustomBinding(binding);
pipeBinding.Elements.Find<NamedPipeTransportBindingElement>().ConnectionPoolSettings.IdleTimeout = TimeSpan.MaxValue;
            binding.ReceiveTimeout = TimeSpan.MaxValue;

只需要添加超时时间

【讨论】:

  • 添加到服务器客户端双方
  • 为什么要使用最大值来超时?如果出现问题,将需要很长时间才能出现。这方面的最佳做法是什么?
  • 你说得对,我刚刚举了例子,会根据用户需求来的
猜你喜欢
  • 2015-06-11
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
相关资源
最近更新 更多