问题
I have an application that makes a call to a web service that uses wsHttpBinding. I need to implement some sort of retry functionality to the web service call in case of connection timeout etc. What is the best way to do this?
I have read about WS-ReliableMessaging, but isn't this something that the publisher of the web service have to implement on the service and not something that I have to do on the client side?
回答1:
I am assuming your call is a write operation and so you need to ensure the write was successful.
You are correct, WS-ReliableMessaging standard does need to be implemented on the service end.
Your main problem is that you can encounter different classes of failures; retry-able failures and non retry-able failures, and your client must be aware of which kind is which.
Retry-able failure examples
- Failure due to intermittent network availability
- Failure due to database deadlock
- Service timeout (only when service operation is idempotent)
Non retry-able failure examples
- Problem with bad data in call (no point retrying as will fail each time)
- Problem with client channel
- Bug introduced to service
- Service timeout (when service operation is non-idempotent)
Determining why your call is failing would be made much simpler if the service exposed fault contracts, failing this you're forced to catch and interrogate exceptions thrown on the service as type SoapException, and hope the original exception details are included (by default they are not). Other types of failure will be raised to you as various WCF channel exceptions, all of which will abort the client channel (including any session) if unhandled.
Your specific question about timeouts is especially tricky - the timeout can have multiple causes, but basically you cannot make an assumption that your call has failed - the call may well have committed but directly the client channel timed out before the response can be sent. In this instance it's only safe to retry if you can call the service multiple times with the same data with no side effects.
If the service operation is unable to perform as described above the best solution is to perform a lookup against the data you just tried to commit with. If the call succeeded the data should be there, otherwise you can retry safely.
来源:https://stackoverflow.com/questions/24360779/how-to-add-retries-to-call-to-web-service