In general, when we close a WCF link, we simply write the icommunicationobject. close () method, but a problem with this method is that when an exception occurs in the call, close () will cause a secondary exception, resulting in the link cannot be closed normally. If there are many such exceptions, the system stability will inevitably be greatly affected. Therefore, we must consider how to close the link after the exception occurs.
We can write an extension to specifically close the WCF link, instead of using the original close
Public Static Void Closeconnection ( This Icommunicationobject myserviceclient)
{
If (Myserviceclient. State ! = Communicationstate. Opened)
{
Return ;
}
Try
{
Myserviceclient. Close ();
}
Catch (Communicationexception ex)
{
Debug. Print (ex. tostring ());
Myserviceclient. Abort ();
}
Catch (Timeoutexception ex)
{
Debug. Print (ex. tostring ());
Myserviceclient. Abort ();
}
Catch (Exception ex)
{
Debug. Print (ex. tostring ());
Myserviceclient. Abort ();
Throw ;
}
}
Then you can use this extension:
Protected Void Close (T client)
{
If (Client ! = Null )
{
Ichannel = Client As Ichannel;
If (Ichannel ! = Null )
Ichannel. closeconnection ();
Else
{
Idisposable = Client As Idisposable;
If (Idisposable ! = Null ) idisposable. Dispose ();
}< BR >}