wcf客户端代理的Abort()方法在捕获FaultException后不会释放会话
我创建了一个托管在 IIS 和 wcf 客户端中的简单 wcf 服务,并发现当您从 wcf 服务捕获FaultException,然后调用 client.Abort() 来释放会话(如微软示例所述)时,它不会释放会话并在第 11 个电话时挂断。
这是示例:
Wcf Service:
[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);
}
}
Client:
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();
}
}
}
}
但是,如果您将 catch(FaultException ) 的 client.Abort() 替换为 client.Close() ,那么一切都会像魅力一样工作,并且在第 11 次调用 wcf-service 后不会出现锁定方法。
为什么会这样?为什么 Abort() 方法在捕获FaultException 后不清理会话?
I have created a simple wcf service hosted in IIS and wcf client and figured out that when u catch a FaultException from the wcf service and then call client.Abort() to release the session (as the microsoft samples said) it doesn't release the session and hangs up on the 11th call.
Here is example:
Wcf Service:
[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);
}
}
Client:
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();
}
}
}
}
But if you replace the client.Abort() with client.Close() for catch(FaultException ) then everything works like a charm and there is no locking after 11th call of the wcf-service method.
Why could it be? Why Abort() method doesn't clean up the session after FaultException was catched?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两件事:
Abort()
。使用Close()
使客户端尝试与服务通信,告诉它关闭服务实例,如果您愿意的话,可以优雅地关闭服务实例。如果通信通道处于故障状态,则意味着无法从客户端到服务进行通信。在这种情况下,您应该调用Abort()
,以便至少关闭客户端。服务实例/会话在服务器上仍然处于活动状态(因为两者之间没有通信),并且将保持这种状态直到实例超时发生。如果您在故障通道上调用Close()
,它会引发更多错误。FaultException
。这并不意味着通信通道将进入故障状态。即您仍然可以使用同一客户端拨打电话。因此,在您的示例中,您不应调用Abort()
。tl;dr
Abort()
仅关闭客户端。服务实例/会话仍然存在。您可以使用以下方法检查通信通道的状态:
Two things:
Abort()
should be used when the Communication Channel is in a Faulted state. UsingClose()
makes the client try to communicate with the service, telling it to close the service instance, graceful-like, if you will. If the the Communication Channel is in the Faulted state, it means no communication can be done from client to service. In that situation you should callAbort()
so that at least the client is closed. The service instance/session will still be alive on the server (since there's no communication between the two), and will remain so until the instance timeout occurs. If you had calledClose()
on a faulted channel, it would have thrown more errors.FaultException
. This does not mean that the Communication Channel will be put into the faulted state. i.e. you can still make calls using the same client. And as such, in your example you should not be callingAbort()
.tl;dr
Abort()
only closes the client. The service instance/session is still alive.You can check for the Communication Channel's state by using:
您是否尝试过我用于调用 WCF 的这种方式?
Have you tried this way, which I use for calling WCF?