Have you properly disabled the WCF link?

Source: Internet
Author: User

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 >}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.