How to resolve timeouts when executing RetrieveAllEntitiesRequest

When you try to execute RetrieveAllEntitiesRequest to retrieve metadata for all the entities in your organisation you may receive the following error: “Message: The request channel timed out while waiting for a reply after 00:01:59.7655710. Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. The time allotted to this operation may have been a portion of a longer timeout.” Because of the large amount of data retrieved when executing RetrieveAllEntitiesRequest the default timeout value of 2 minutes is unlikely to be enough.

To increase the timeout value you need to set the timeout property on the OrganizationServiceProxy object. If you use the OrganizationServiceContext (as generated by CrmSvcUtil) you have to set the timeout on the OrganizationServiceProxy object you pass into the OrganizationServiceContext as there is no way to set the Timeout directly for the OrganizationServiceContext.

Of course timeouts can happen when you are doing other requests as well (and the solution is the same), but it is highly likely you’ll run into your first timeout issue when you start playing with the RetrieveAllEntitiesRequest request.

  1. using System;
  2. using System.Linq;
  3. using System.Net;
  4. using System.ServiceModel.Description;
  5. using Microsoft.Xrm.Sdk.Client;
  6. using Microsoft.Xrm.Sdk.Messages;
  7. using Microsoft.Xrm.Sdk.Metadata;
  8. namespace MetadataConsoleApplication
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             var organizationUrl
  15.                 = string.Format(
  16.                     “{0}/{1}/XRMServices/2011/Organization.svc”,
  17.                     “http://{your_ip}”, “{organization_name}”);
  18.             var credentials = new ClientCredentials();
  19.             credentials.Windows.ClientCredential
  20.                 = new NetworkCredential(
  21.                   “{username}”, “{password}”, “{domain}”);
  22.             var serviceProxy
  23.                 = new OrganizationServiceProxy(
  24.                     new Uri(organizationUrl), null,
  25.                     credentials, null);
  26.             serviceProxy.ServiceConfiguration
  27.                 .CurrentServiceEndpoint.Behaviors.Add(
  28.                     new ProxyTypesBehavior());
  29.             // Increase the timeout to 30 minutes
  30.             // so the operation succeeds
  31.             serviceProxy.Timeout = new TimeSpan(0, 0, 30, 0);
  32.             var context = new AcmeContext(serviceProxy);
  33.             //  Retrieve metadata from CRM
  34.             var request = new RetrieveAllEntitiesRequest
  35.             {
  36.                 EntityFilters = EntityFilters.All,
  37.                 RetrieveAsIfPublished = true,
  38.             };
  39.             var response
  40.                 = (RetrieveAllEntitiesResponse)
  41.                     context.Execute(request);
  42.             Console.WriteLine(
  43.                 “Entities retrieved: “
  44.                 + response.EntityMetadata.Count());
  45.             Console.ReadKey();
  46.         }
  47.     }
  48. }

 

Advertisement

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 )

Connecting to %s