简体   繁体   中英

WCF Async methods failure because of bad network

I am new into WCF stuff. I am curious to know two things about async methods:

  1. What happens for some reason network goes down when I sending request to the service? Do I get any kind of exception or something k=like that?

  2. What happens if network goes down when service tries to respond to the request? Does the result gets lost?

Thanks for the help!

Sending a message through TCP (this includes HTTP) to WCF goes rougly through three phases:

  1. Client sends message to the WCF service
  2. Only when the entire message has been successfully received, it gets deserialized and its contents are passed to the appropriate service operation. The service operation runs to completion.
  3. The response (ie what you return or throw from the service operation) gets sent to the client.

If the connection is lost during 1., your service won't see the message and your client will throw an exception.

If the connection is lost during 2., I think (but you can easily simulate that, for example using Thread.Sleep() ) the service operation continues execution.

If the connection is lost during 3., the response will be lost, but the operation will have run.

exception will be throw if the service did not respond. the program will receive the exception. you can use try...catch block to catch it. then write to a log file if you want to record it.

Based on my experience, mostly the 1st connection takes time to initialize. Sometime I get EndpointNotFoundException or TimeoutException if the service dint responds.

Therefore, i create a method to initialize the connection by passing message "connecting" to the service, if the connection established, service will return me a "connected" message. else if it return a exception , i will just let it retry.

I set the number of retry = 5. So it will loop for 5 time if the connection still not establish. Once establish , it will continue for next process.

we cant control the network connection and we cant expect and which point , which part it fail. but we can control our program when the service failed. Depends on what you want to do if the service failed. maybe clean the data, restart the process or stop the program immediately and log the error for investigation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM