Retry WCF Service Call When Down

The scope of this article is quite different.

Every developer must make a Web Service or WCF calls for some or other reason.

Web Services or WCF are something that are not in our control and they only answer when the server on which the services are hosted are up and running.

Let's say we need to call multiple web service at a single time and one or more fails. We want the failng service call(s) to be retried again (at least for some interval of time).

Basically our task is to retry the web service or WCF call, if any of the services are down or the server is down.

We can do this using many others tools like polly or using async await.

Let's just not use any tools and implement it using just a simple generic class.

We will call a WCF service from a console application. If the WCF service is not running then the console application or client will try continuously every few seconds to get the response from that service.

Use the following procedure.

Step 1: Create a WCF Service and add a method that just returns a string.

*Create a WCF Service

Step 2: Now create a client and call this WCF service without using any retries method.

*create a client

This service will reply until the hosted server is up. But what if my server is not up and due to some deadlocks the server is blocked for a while?

*hosted server

For that we need to retry the same call after a period of time. So that when the service is up again we can call the same method and get the desired result without losing the data that we shared passed over the WWW.

Because once the preceding exception is thrown, we lost our data and the client and server nobody knows about the information that was passed over the WWW.

Step 3: Let's create a Generic class that will retry this service call for us.

* Create a Generic class

This is the simple generic class that accepts one function  and two integer values and returns the function if the service called succeed else it waits and calls the service again.

Step 4: Now when our generic class is ready to retry, let's call our service method using this class in our main function.

call our service method

Step 5: Check the result for success or failure.


If the WCF call fails the first time.

call fails 1st time

If the WCF call fails the second time.

WCF call fails

If the WCF service comes up or the server on which it is hosted works fine then after some time we get a WCF Succeed result.

WCF Succeed Result

So, it is now very simple to just retry your method or service call upon failure. This approach is mostly used for calling some method that fails due to some time issue or some deadlocks.

Find the attachment of this code with this article.

Feel free to ask , if you have doubt.

Similar Articles