Jes*_*erg 8 .net wcf web-services exception fault
我有一个WCF服务,有时必须返回一个错误.由于某种原因,对我的服务的调用开始超时,并出现以下错误:"在00:00之后等待回复时请求通道超时:59.8906201.增加传递给Request的调用的超时值或增加SendTimeout绑定的值.分配给此操作的时间可能是较长超时的一部分."
检查问题后,出现了一种模式:当服务10次返回故障时,超时开始.所以我创建了一个由以下实现的测试服务:
public string GetData(int value)
{
throw new FaultException("A testerror occured");
}
Run Code Online (Sandbox Code Playgroud)
还有一个测试客户:
protected void RunTestGetData()
{
using (TestServiceReference.Service1Client client
= new WSPerformanceTester.TestServiceReference.Service1Client())
{
try
{
client.GetData(1);
client.Close();
outputWriter.WriteLine(string.Format("Call run in thread {0}: GetData()", Thread.CurrentThread.ManagedThreadId));
outputWriter.Flush();
}
catch (Exception e)
{
client.Abort();
client.Close();
outputWriter.WriteLine(string.Format("Error occured in thread {0}: GetData(): {1}", Thread.CurrentThread.ManagedThreadId, e.Message));
outputWriter.Flush();
}
}
}
Run Code Online (Sandbox Code Playgroud)
只有在服务返回FaultException时才会发生这种情况.如果我抛出正常异常,服务可以在第10次调用后继续运行.显然,我想很好地包装我的异常,所以抛出正常异常并不是一个真正的选择.
为什么我会遇到这些超时异常?在此先感谢任何帮助..
显然,客户端代码应该如下:
protected void RunTestGetData()
{
TestServiceReference.Service1Client client
= new WSPerformanceTester.TestServiceReference.Service1Client()
try
{
client.GetData(1);
}
catch (FaultException e)
{
//Handle fault
}
try
{
if (client.State != System.ServiceModel.CommunicationState.Faulted)
{
client.Close();
}
}
catch(Exception e)
{
outputWriter.WriteLine("Error occured in Client.Close()");
outputWriter.Flush();
client.Abort();
}
}
Run Code Online (Sandbox Code Playgroud)
调用 client.Abort() 应该始终是最后的手段。