locked
Unhandled Exception: System.TimeoutException: The request channel timed out while waiting for a reply after 00:01:59.9139778.Increase the timeout value passed to the call to Request or increase the SendTimeout value on the Binding. RRS feed

  • Question

  • Hi, 

    I created a simple plugin and since i wanted to use Early Binding i added Xrm.cs file to my solution.After i tried registering the plugin (using the Plugin Registration Tool) the plugin does not gets registered and i get the below mentioned Exception.

    Unhandled Exception: System.TimeoutException: The request channel timed out while waiting for a reply after 00:01:59.9139778. 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.

    Server stack trace: 
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.SecurityChannelFactory`1.SecurityRequestChannel.Request(Message message, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

    Exception rethrown at [0]: 
       at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       at Microsoft.Xrm.Sdk.IOrganizationService.Update(Entity entity)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.UpdateCore(Entity entity)
       at Microsoft.Crm.Tools.PluginRegistration.RegistrationHelper.UpdateAssembly(CrmOrganization org, String pathToAssembly, CrmPluginAssembly assembly, PluginType[] type)
       at Microsoft.Crm.Tools.PluginRegistration.PluginRegistrationForm.btnRegister_Click(Object sender, EventArgs e)
    Inner Exception: System.TimeoutException: The HTTP request to 'https://demoorg172.api.crm.dynamics.com/XRMServices/2011/Organization.svc' has exceeded the allotted timeout of 00:01:59.9430000. The time allotted to this operation may have been a portion of a longer timeout.
       at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
    Inner Exception: System.Net.WebException: The operation has timed out
       at System.Net.HttpWebRequest.GetResponse()
       at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)

    And to my Surprise after i remove the Xrm.cs file from my solution the Plugin got registered!
    Not understanding what exactly is the issue.

    Any Suggestions are highly appreciated.

    Thanks,
    Shradha

      
    Thursday, October 31, 2013 6:28 AM

Answers

  • Hello Shardha,

                            I really appreciate that you have faced this issue.This is really very strange issue and basically it occurs because of big size of your early bound class and slow internet connection.

                            I would strictly recommend you to reduce the file size of your early bound class and then register.By default early bound class is created for all the entities which are present in CRM(System entities as well custom entities).Such kind of early bound classes takes lots of time to register on server and hence timeout exception comes.

                            There is some standard define to reduce the size of early bound class.Please follow the link to get rid from big size of early bound class.

    1. Create a new C# class library project in Visual Studio called SvcUtilFilter.
    2. In the project, add references to the following:
      1. CrmSvcUtil.exe(from sdk)   This exe has the interface we will implement.
      2. Microsoft.Xrm.Sdk.dll  (found in the CRM SDK).
      3. System.Runtime.Serialization.
    3.   Add the following class to the project:

    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using Microsoft.Crm.Services.Utility;
    using Microsoft.Xrm.Sdk.Metadata;
    namespace SvcUtilFilter
    {
        /// <summary>
        /// CodeWriterFilter for CrmSvcUtil that reads list of entities from an xml file to
        /// determine whether or not the entity class should be generated.
        /// </summary>
        public class CodeWriterFilter : ICodeWriterFilterService
        {
            //list of entity names to generate classes for.
            private HashSet<string> _validEntities = new HashSet<string>();
           
            //reference to the default service.
            private ICodeWriterFilterService _defaultService = null;
            /// <summary>
            /// constructor
            /// </summary>
            /// <param name="defaultService">default implementation</param>
            public CodeWriterFilter( ICodeWriterFilterService defaultService )
            {
                this._defaultService = defaultService;
                LoadFilterData();
            }
            /// <summary>
            /// loads the entity filter data from the filter.xml file
            /// </summary>
            private void LoadFilterData()
            {
                XElement xml = XElement.Load("filter.xml");
                XElement entitiesElement = xml.Element("entities");
                foreach (XElement entityElement in entitiesElement.Elements("entity"))
                {
                    _validEntities.Add(entityElement.Value.ToLowerInvariant());
                }
            }
            /// <summary>
            /// /Use filter entity list to determine if the entity class should be generated.
            /// </summary>
            public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
            {
                return (_validEntities.Contains(entityMetadata.LogicalName.ToLowerInvariant()));
            }
            //All other methods just use default implementation:
            public bool GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
            {
                return _defaultService.GenerateAttribute(attributeMetadata, services);
            }
            public bool GenerateOption(OptionMetadata optionMetadata, IServiceProvider services)
            {
                return _defaultService.GenerateOption(optionMetadata, services);
            }
            public bool GenerateOptionSet(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
            {
                return _defaultService.GenerateOptionSet(optionSetMetadata, services);
            }
            public bool GenerateRelationship(RelationshipMetadataBase relationshipMetadata, EntityMetadata otherEntityMetadata, IServiceProviderservices)
            {
                return _defaultService.GenerateRelationship(relationshipMetadata, otherEntityMetadata, services);
            }
            public bool GenerateServiceContext(IServiceProvider services)
            {
                return _defaultService.GenerateServiceContext(services);
            }
        }
    }

    This class implements the ICodeWriterFilterService interface.  This interface is used by the class generation utility to determine which entities, attrributes, etc. should actually be generated.  The interface is very simple and just has seven methods that are passed metadata info and return a boolean indicating whether or not the metadata should be included in the generated code file.   

    For now I just want to be able to determine which entities are generated, so in the constructor I read from an XML file (filter.xml) that holds the list of entities to generate and put the list in a Hashset.  The format of the xml is this:

    <filter>
      <entities>
        <entity>systemuser</entity>
        <entity>team</entity>
        <entity>role</entity>
        <entity>businessunit</entity>
      </entities>
    </filter>

    Take a look at the methods in the class. In the GenerateEntity method, we can simply check the EntityMetadata parameter against our list of valid entities and return true if it's an entity that we want to generate.

    For all of the other methods we want to just do whatever the default implementation of the utility is.  Notice how the constructor of the class accepts a defaultService parameter.  We can just save a reference to this default service and use it whenever we want to stick with the default behavior.  All of the other methods in the class just call the default service.

    To use our extension when running the utility, we just have to make sure the compiled DLL and the filter.xml file are in the same folder as CrmSvcUtil.exe, and set the /codewriterfilter command-line argument when running the utility (as described in the SDK):

    crmsvcutil.exe /url:http://<server>/<org>/XrmServices/2011/Organization.svc /out:sdk.cs  /namespace:<namespace> /codewriterfilter:SvcUtilFilter.CodeWriterFilter,SvcUtilFilter /username:abc@xyz.onmicrosoft.com /password:xxxx

    That's it! You now have a generated sdk.cs file that is only a few hundred kilobytes instead of 5MB. 

    One final note:  There is actually a lot more you can do with extensions to the code generation utility.  For example: if you return true in the GenerateOptionSet method, it will actually generated Enums for each CRM picklist (which it doesn't normally do by default).

    Also, the source code for this SvcUtilFilter example can be found here.  Use at your own risk, no warranties, etc. etc. 

     -------------------------------------------------------------------------------------------------------------------------------------------------------------

    Please mark as a answer if this post is useful to you.

    • Marked as answer by shradha_singh Thursday, October 31, 2013 6:52 AM
    Thursday, October 31, 2013 6:44 AM

All replies

  • Hello Shardha,

                            I really appreciate that you have faced this issue.This is really very strange issue and basically it occurs because of big size of your early bound class and slow internet connection.

                            I would strictly recommend you to reduce the file size of your early bound class and then register.By default early bound class is created for all the entities which are present in CRM(System entities as well custom entities).Such kind of early bound classes takes lots of time to register on server and hence timeout exception comes.

                            There is some standard define to reduce the size of early bound class.Please follow the link to get rid from big size of early bound class.

    1. Create a new C# class library project in Visual Studio called SvcUtilFilter.
    2. In the project, add references to the following:
      1. CrmSvcUtil.exe(from sdk)   This exe has the interface we will implement.
      2. Microsoft.Xrm.Sdk.dll  (found in the CRM SDK).
      3. System.Runtime.Serialization.
    3.   Add the following class to the project:

    using System;
    using System.Collections.Generic;
    using System.Xml.Linq;
    using Microsoft.Crm.Services.Utility;
    using Microsoft.Xrm.Sdk.Metadata;
    namespace SvcUtilFilter
    {
        /// <summary>
        /// CodeWriterFilter for CrmSvcUtil that reads list of entities from an xml file to
        /// determine whether or not the entity class should be generated.
        /// </summary>
        public class CodeWriterFilter : ICodeWriterFilterService
        {
            //list of entity names to generate classes for.
            private HashSet<string> _validEntities = new HashSet<string>();
           
            //reference to the default service.
            private ICodeWriterFilterService _defaultService = null;
            /// <summary>
            /// constructor
            /// </summary>
            /// <param name="defaultService">default implementation</param>
            public CodeWriterFilter( ICodeWriterFilterService defaultService )
            {
                this._defaultService = defaultService;
                LoadFilterData();
            }
            /// <summary>
            /// loads the entity filter data from the filter.xml file
            /// </summary>
            private void LoadFilterData()
            {
                XElement xml = XElement.Load("filter.xml");
                XElement entitiesElement = xml.Element("entities");
                foreach (XElement entityElement in entitiesElement.Elements("entity"))
                {
                    _validEntities.Add(entityElement.Value.ToLowerInvariant());
                }
            }
            /// <summary>
            /// /Use filter entity list to determine if the entity class should be generated.
            /// </summary>
            public bool GenerateEntity(EntityMetadata entityMetadata, IServiceProvider services)
            {
                return (_validEntities.Contains(entityMetadata.LogicalName.ToLowerInvariant()));
            }
            //All other methods just use default implementation:
            public bool GenerateAttribute(AttributeMetadata attributeMetadata, IServiceProvider services)
            {
                return _defaultService.GenerateAttribute(attributeMetadata, services);
            }
            public bool GenerateOption(OptionMetadata optionMetadata, IServiceProvider services)
            {
                return _defaultService.GenerateOption(optionMetadata, services);
            }
            public bool GenerateOptionSet(OptionSetMetadataBase optionSetMetadata, IServiceProvider services)
            {
                return _defaultService.GenerateOptionSet(optionSetMetadata, services);
            }
            public bool GenerateRelationship(RelationshipMetadataBase relationshipMetadata, EntityMetadata otherEntityMetadata, IServiceProviderservices)
            {
                return _defaultService.GenerateRelationship(relationshipMetadata, otherEntityMetadata, services);
            }
            public bool GenerateServiceContext(IServiceProvider services)
            {
                return _defaultService.GenerateServiceContext(services);
            }
        }
    }

    This class implements the ICodeWriterFilterService interface.  This interface is used by the class generation utility to determine which entities, attrributes, etc. should actually be generated.  The interface is very simple and just has seven methods that are passed metadata info and return a boolean indicating whether or not the metadata should be included in the generated code file.   

    For now I just want to be able to determine which entities are generated, so in the constructor I read from an XML file (filter.xml) that holds the list of entities to generate and put the list in a Hashset.  The format of the xml is this:

    <filter>
      <entities>
        <entity>systemuser</entity>
        <entity>team</entity>
        <entity>role</entity>
        <entity>businessunit</entity>
      </entities>
    </filter>

    Take a look at the methods in the class. In the GenerateEntity method, we can simply check the EntityMetadata parameter against our list of valid entities and return true if it's an entity that we want to generate.

    For all of the other methods we want to just do whatever the default implementation of the utility is.  Notice how the constructor of the class accepts a defaultService parameter.  We can just save a reference to this default service and use it whenever we want to stick with the default behavior.  All of the other methods in the class just call the default service.

    To use our extension when running the utility, we just have to make sure the compiled DLL and the filter.xml file are in the same folder as CrmSvcUtil.exe, and set the /codewriterfilter command-line argument when running the utility (as described in the SDK):

    crmsvcutil.exe /url:http://<server>/<org>/XrmServices/2011/Organization.svc /out:sdk.cs  /namespace:<namespace> /codewriterfilter:SvcUtilFilter.CodeWriterFilter,SvcUtilFilter /username:abc@xyz.onmicrosoft.com /password:xxxx

    That's it! You now have a generated sdk.cs file that is only a few hundred kilobytes instead of 5MB. 

    One final note:  There is actually a lot more you can do with extensions to the code generation utility.  For example: if you return true in the GenerateOptionSet method, it will actually generated Enums for each CRM picklist (which it doesn't normally do by default).

    Also, the source code for this SvcUtilFilter example can be found here.  Use at your own risk, no warranties, etc. etc. 

     -------------------------------------------------------------------------------------------------------------------------------------------------------------

    Please mark as a answer if this post is useful to you.

    • Marked as answer by shradha_singh Thursday, October 31, 2013 6:52 AM
    Thursday, October 31, 2013 6:44 AM
  • Hi Shradha, while Prashant is correct that you should try to filter your context, and I agree that it's possible for the combination of low bandwidth and large context can contribute to this message, I recently ran into this while using the SDK to migrate notes and attachments. My error occurred even after setting the ServiceProxy.Timeout value like so:

    _serviceProxyS.Timeout = new TimeSpan(0, 8, 0);

    ...even increasing the timeout value didn't work - when querying the annotation or activitymimeattachment entities 5,000 or even 1,000 records were too much for the environment I was working in, so it failed with similar error as yours.

    What I basically did is only return the Guid for the Annotation then for each record I did a QueryByAttribute to get the fields for that record one by one. I could not see another way to do it.

    If Prashant's tactic helped you, you may not have had the same issue as me, but for those looking at this in the future this may be helpful


    I hope this helps. If my response answered your question, please mark the response as an answer and also vote as helpful. Michael Mayo

    Wednesday, January 28, 2015 12:17 PM