Retry to call Web Service in case of "The remote name could not be resolved" error.

I found that sometimes, if I have an error "The remote name could not be resolved" to access remote web services, I should re-try the call and it helps.
I've considered to increase timeout for WebServices method call, but some methods,e.g. DataSet.ReadXml (String)  doesn't have the option.

So I've created a static method that will retry remote call a few times. The actual function to call can be passed as delegate:

    public class WSClientHelper

    {

        public delegate void CallWebServiceDelegate ();

        public static void RetryWebServiceCall(int nRetry, CallWebServiceDelegate dlgt)

        {         // I beleive that it's a good idea to re-try in case of "The remote name could not be resolved"

                for (int i = 0; i < nRetry; i++)

                {

                    try

                    {

                        dlgt();// ds = ReadRssUrlAsDataSet(timeStart, url);

                        break;

                    }

                    catch (WebException exc)

                    {

                        if (exc.Message.Contains("The remote name could not be resolved") && (i < nRetry-1))

                        {  DebugHelper.TracedLine("Attempt " + i.ToString() + " failed." +exc.Message);

                            continue;//try n times

                        }

                        throw;

                    }

                }

        }

    }

 

The sample to use utilizes C# anonymous delegates(new for .Net 2.0), which makes the use very compact and simple

                FSCSharpLib.ASP.WSClientHelper.RetryWebServiceCall(3, delegate

                {

//Yo can put any code you like with unrestricted access to local variables and class members

                    ds = ReadRssUrlAsDataSet(timeStart, url); //just sample method

                });

 

The StackOverflow question http://stackoverflow.com/questions/1563191/c-sharp-cleanest-way-to-write-retry-logic/1563234#1563234 discuss similar approach.

posted @ Thursday, September 7, 2006 12:52 PM
Print

Comments on this entry:

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Quoc Linh at 9/22/2006 4:59 AM
Gravatar
Hi Michael,

I copied and paste your code to try it out and the compiler didn't like the call here:

FSCSharpLib.ASP.WSClientHelper.RetryWebServiceCall(3, delegate

{

//Yo can put any code you like with unrestricted access to local variables and class members

ds = ReadRssUrlAsDataSet(timeStart, url); //just sample method

});

I am not sure how this delegate call suppose to work so could you please give me more information? Thanks in advance!

QuocLinh
lequoclinh@yahoo.com

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Quoc Linh at 9/22/2006 6:22 AM
Gravatar
Michael,

Never mind. I figured out the

delegate()
{
}

is C# Anonymous Methods only available in VS 2005.

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Jeffry van de Vuurst at 10/28/2006 2:06 AM
Gravatar
Just wondering, say i=2 and nRetry=3. In the catch you check for i<nRetry, which results in true. Then you continue with the loop, but then i=nRetry and it goes out of the loop, thus not throwing an exception.

So, either in the loop you do i<nRetry+1, or in the catch you do i<nRetry-1.

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Michael Freidgeim at 10/30/2006 9:03 AM
Gravatar
Jeffry,
Thank you for pointing me to the bug.
I will update the code.

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Alex M at 11/29/2008 11:29 PM
Gravatar
Thanks for the inspiration, I took your idea and adapted it slightly. My implementation takes a list of webservice URL's and if a request fails, the next URL in the list is used.

Code can be found at http://amaitland.wordpress.com/2008/11/22/webserviceoperation-timedout

Hope this helps to inspire others.

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Kelly Harrison at 12/18/2008 5:23 AM
Gravatar
Great stuff - solved a specific requirement for a project I'm working on. Thanks for sharing!

-k

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Kumar at 4/21/2010 11:16 AM
Gravatar
Could someone tell me how to do this in VB.net

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Michael Freidgeim at 4/22/2010 5:18 PM
Gravatar
Kumar,
Unfortunately, VB does not support anonymous methods/lambda expressions with a statement body.
(e.g. see http://social.msdn.microsoft.com/Forums/en/sharepointdevelopment/thread/24df5ec7-c45e-428b-8a9b-a0259d78ebbc)

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Kumar at 4/28/2010 10:01 AM
Gravatar
Mike

Thanks a lot for the quick response. Do you have a sample project of how you have done it. Bcos currently we have about 40+ services and i am trying to see if i can generalize the retry code. If so where to put it. May be in the Override getwebrequest or the webservice reference.vb. Any suggestions are welcome.

Thanks
Kumar

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.

Left by Michael Freidgeim at 5/1/2010 8:44 PM
Gravatar
Not sure that you can Override getwebrequest or the webservice reference.vb.
If you are using WCF, you probably can add some common behavior(sorry, I do not have enough knowledge to be more specific)

Your comment:



(not displayed)

 
 
 
 
 

Live Comment Preview:

 
«September»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011