【发布时间】:2011-04-24 07:56:10
【问题描述】:
我创建了一个托管在 IIS 和 wcf 客户端中的简单 wcf 服务,并发现当你从 wcf 服务捕获一个 FaultException 然后调用 client.Abort() 来释放会话时(正如微软示例所说)它不会'不释放会话并在第 11 个通话时挂断。
示例如下:
Wcf 服务:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
public class Service1 : IService1
{
public string GetData(int value)
{
throw new FaultException("Exception is here");
return string.Format("You entered: {0}", value);
}
}
客户:
class Program
{
static void Main(string[] args)
{
Service1Client client = null;
for(int i = 0; i < 15; i++)
{
try
{
client = new Service1Client();
client.GetData(100);
}
catch (TimeoutException timeoutEx)
{
Console.WriteLine(timeoutEx);
client.Abort();
}
catch (FaultException faultEx)
{
Console.WriteLine(faultEx);
client.Abort();
}
catch (CommunicationException commEx)
{
Console.WriteLine(commEx);
client.Abort();
}
}
}
}
但是,如果您将 client.Abort() 替换为 client.Close() 以获得 catch(FaultException ),那么一切都会像魅力一样工作,并且在第 11 次调用 wcf-service 方法后没有锁定。
为什么会这样?为什么捕获到 FaultException 后 Abort() 方法不清理会话?
【问题讨论】:
-
您是从这里复制粘贴的吗? social.msdn.microsoft.com/Forums/en-US/wcf/thread/…
标签: c# wcf faultexception