Home > C#, MS CRM > Try to work few times before saying “Ups..”

Try to work few times before saying “Ups..”

Sometimes it is impossible to give 100% guaranty, that some code will execute properly even if it has realy no mistakes. For example, web service method calls may cause error due to high server load or high concurency in server requests. Programming for MS CRM web services I had faced such situations and here is my solution. I propose to repeat an action few times before throwing error. Of cource, there is no 100% guaranty anyway, but.. only God can give 100%.

public delegate void VoidDelegate();    
static class Util
    {
        public static void AttemptAction(VoidDelegate action)
        {
            var attempts = 5;
            while (attempts > 0)
            {
                try
                {
                    action();
                    return;
                }
                catch
                {
                    attempts--;
                    if (attempts == 0)
                    {
                        throw;
                    }
                }
            }
        }
    }

So, we have a void delegate, which represents an action. Number of attempts in my variant is 5 but it can be implemented as a parameter. Example of usage:

Util.AttemptAction(
    () => responce = (RetrieveMultipleResponse) crmService.Execute(request));
Advertisements
Categories: C#, MS CRM Tags: ,
  1. 07.12.2010 at 00:17

    Hello. And Bye.
    Trinity Schiffelbein

  2. 31.01.2011 at 18:23

    Haha … let me guess where u r using it 😛

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: