Prepare your code for Disaster recovery in Dynamics CRM Online – Dynamics CRM 2016

Not a very familiar topic I would say, at-least from what the title says. But I feel this will become more and more prominent as Dynamics CRM online customers keeps on increasing every hour.

So how do you handle if there is  some unplanned service interruption for you CRM online customer. Unplanned interruption happens when something goes wrong in the Microsoft Data Center hosting your CRM cloud environment. Thinking why you need some disaster recovery for that? Isn’t the wonderful engineers at Microsoft already at their job to make the stuff up as soon as possible? The answer is- “yes they are”. But sometimes you need to handle that from your side too.

Let us take the example of a recent request by one of my clients. Our client had a Dynamics CRM Online environment and there are jobs scheduled in the windows task scheduler which runs at specified intervals and loads data from a specified system into CRM. It is kind of incremental data upload and my client was very particular about the uninterrupted working of the service.

So we were to handle all sorts of possible exceptions. But how to handle such service  interruption from the Microsoft Data Centers?

Need not worry. Microsoft has covered you back in this case. So let’s see how it can be done.

First of all I will construct the OrganizationServiceProxy class instance. This is the class you would normally use when you try to interact with CRM from external client apps.

var orgServiceUrl = new Uri("<org service url>");
var clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "<office 365 user name>";
clientCredentials.UserName.Password = "<Office 365 password>";

var proxy = new OrganizationServiceProxy(orgServiceUrl, null, clientCredentials, null);

 

Nothing fancy about the above stuff right? Now comes the fun part. The organization service has events which gets fired when a failover has happened. The events are

  • EndpointSwitched – Occurs when a failover recovery has completed and the organization’s current endpoint has switched to an alternate endpoint
  • EndpointSwitchRequired – Occurs when a failover has occurred and a switch from the organization’s current endpoint to alternate endpoint is required.

However these events only fire when the failover is enabled for your organization. Let’s check for the code below which first checks whether the organization’s failover is enabled and then registers the above mentioned events.

if (proxy.EndpointAutoSwitchEnabled)
{
                proxy.EndpointSwitched += proxy_EndpointSwitched;
                proxy.EndpointSwitchRequired += proxy_EndpointSwitchRequired;
}

The above code first checks for the property EndpointAutoSwitchEnabled. If the value for this property is true, failover is enable for this organization. Below are the event handler for the events.

static void proxy_EndpointSwitched(object sender, EndpointSwitchEventArgs e)
        {
            // make the service call again here. and this time it would succeed.
            var oldEndpoint = e.PreviousUrl;
            var newEndpoint = e.NewUrl;
        }

        static void proxy_EndpointSwitchRequired(object sender, EndpointSwitchEventArgs e)
        {
           
        }

You can get the new EndPoint Url of the organization and make the service call again. Since the failover is successful, the code would continue working.

So what if failover is not enabled. In that case subscribing to the above events does not make any value.

However you can handle the following type of exceptions in your code for the service interruption.

  • EndpointNotFoundException
  • FaultException<OrganizationServiceFault> where fault.Detail.ErrorCode == -2147176347.
  • TimeoutException

The above exceptions can happen in some other circumstances as well. However you can catch these exception and inside that put in some code to wait till the CRM service is up again.

Hope this helps!

Advertisements

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s