c# - Exception in "using" statement with WCF not closing connections properly. How does one close faulted WCF client connections or those with exceptions? -


there several questions on stackoverflow regarding closing wcf connections, highest ranking answers refers blog:

http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html

i have problem technique when set breakpoint @ server , let client hang more 1 minute. (i'm intentionally creating timeout exception)

the issue client appears "hang" until server done processing. guess being cleaned post-exception.

in regard timeoutexception appears retry() logic of client continue resubmit query server on , on again, can see server-side debugger queue requests and execute each queued request concurrently. code wan't expecting wcf act way , may cause of data corruption issues i'm seeing.

something doesn't totally add solution.

what all-encompassing modern way of dealing faults , exceptions in wcf proxy?

update

admittedly, bit of mundane code write. i prefer linked answer, , don't see "hacks" in code may cause issues down road.


this microsoft's recommended way handle wcf client calls:

for more detail see: expected exceptions

try {     ...     double result = client.add(value1, value2);     ...     client.close(); } catch (timeoutexception exception) {     console.writeline("got {0}", exception.gettype());     client.abort(); } catch (communicationexception exception) {     console.writeline("got {0}", exception.gettype());     client.abort(); } 

additional information many people seem asking question on wcf microsoft created dedicated sample demonstrate how handle exceptions:

c:\wf_wcf_samples\wcf\basic\client\expectedexceptions\cs\client

download sample: c# or vb

considering there many issues involving using statement, (heated?) internal discussions , threads on issue, i'm not going waste time trying become code cowboy , find cleaner way. i'll suck up, , implement wcf clients verbose (yet trusted) way server applications.

optional additional failures catch

many exceptions derive communicationexception , don't think of exceptions should retried. drudged through each exception on msdn , found short list of retry-able exceptions (in addition timeoutexception above). let me know if missed exception should retried.

exception   mostrecentex = null; for(int i=0; i<5; i++)  // attempt maximum of 5 times  {     try     {        ...        double result = client.add(value1, value2);        ...        client.close();     }      // following typically thrown on client when channel terminated due server closing connection.     catch (channelterminatedexception cte)     {        mostrecentex = cte;       securesecretservice.abort();         //  delay (backoff) , retry          thread.sleep(1000 * (i + 1));      }      // following thrown when remote endpoint not found or reached.  endpoint may not found or      // reachable because remote endpoint down, remote endpoint unreachable, or because remote network unreachable.     catch (endpointnotfoundexception enfe)     {        mostrecentex = enfe;      securesecretservice.abort();         //  delay (backoff) , retry          thread.sleep(1000 * (i + 1));      }      // following exception thrown when server busy accept message.     catch (servertoobusyexception stbe)     {       mostrecentex = stbe;         securesecretservice.abort();          //  delay (backoff) , retry          thread.sleep(1000 * (i + 1));      }      catch(exception ex)     {           throw ex;  // rethrow other exception not defined here     } } if (mostrecentex != null)  {     throw new exception("wcf call failed after 5 retries.", mostrecentex ); } 

Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -