Geeks With Blogs

Michael Freidgeim's Blog MS .Net Development

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 on Thursday, September 7, 2006 12:52 PM ASP.NET | Back to top


Comments on this post: Retry to call Web Service in case of "The remote name could not be resolved" error.

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting 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
Left by Quoc Linh on Sep 22, 2006 4:59 AM

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

Never mind. I figured out the

delegate()
{
}

is C# Anonymous Methods only available in VS 2005.
Left by Quoc Linh on Sep 22, 2006 6:22 AM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting 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.
Left by Jeffry van de Vuurst on Oct 28, 2006 2:06 AM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting Gravatar...
Jeffry,
Thank you for pointing me to the bug.
I will update the code.
Left by Michael Freidgeim on Oct 30, 2006 9:03 AM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting 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.
Left by Alex M on Nov 29, 2008 11:29 PM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting Gravatar...
Great stuff - solved a specific requirement for a project I'm working on. Thanks for sharing!

-k
Left by Kelly Harrison on Dec 18, 2008 5:23 AM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting Gravatar...
Could someone tell me how to do this in VB.net
Left by Kumar on Apr 21, 2010 11:16 AM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting 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)
Left by Michael Freidgeim on Apr 22, 2010 5:18 PM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting 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
Left by Kumar on Apr 28, 2010 10:01 AM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting 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)
Left by Michael Freidgeim on May 01, 2010 8:44 PM

# re: Retry to call Web Service in case of "The remote name could not be resolved" error.
Requesting Gravatar...
Could someone help me on how to hit the webservice endpoint at runtime, pass the request in the webservice request method and obtain the response via the webservice response method. I need help programmatically to hit the endpoint in runtime in C# code.

Thanks in advance.
Left by kishan on Dec 16, 2014 9:07 PM

Your comment:
 (will show your gravatar)


Copyright © Michael Freidgeim | Powered by: GeeksWithBlogs.net | Join free