locked
Multiple errors in SDK Developer Extensions while under load RRS feed

  • Question

  • I've developed a "middle tier" service layer, based on Agatha Request/Response WCF framework, to connect an On-Premise CRM 2011 deployment to multiple internet-facing SharePoint based client webportals. I've used the "Developer Extensions" to get up to speed quickly, but I'm facing some problems since we've gone live. The service layer is logging all errors, and once in a while (sometimes it takes days, sometimes hours) it goes into an error frenzie. It goes something like this:

    System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
    Parameter name: index
       at System.Collections.Generic.List`1.RemoveAt(Int32 index)
       at System.Collections.ObjectModel.Collection`1.Remove(T item)
       at System.Collections.Generic.KeyedByTypeCollection`1.Find[T](Boolean remove)
       at Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior.UpdateFormatterBehavior(OperationDescription operationDescription)
       at Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior.System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
       at System.ServiceModel.Description.DispatcherBuilder.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime)
       at System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters)
       at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose)
       at System.ServiceModel.ChannelFactory.CreateFactory()
       at System.ServiceModel.ChannelFactory.OnOpening()
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.ChannelFactory.EnsureOpened()
       at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
       at Microsoft.Xrm.Sdk.Client.ServiceChannel`1.CreateChannel()
       at Microsoft.Xrm.Sdk.Client.ServiceChannel`1.ConfigureNewChannel()
       at Microsoft.Xrm.Sdk.Client.ServiceChannel`1.get_Channel()
       at Microsoft.Xrm.Sdk.Client.ServiceContextInitializer`1.Initialize(ServiceProxy`1 proxy)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceContextInitializer..ctor(OrganizationServiceProxy proxy)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.RetrieveMultipleCore(QueryBase query)
       at Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService[TResult](Func`2 action)
       at CrmService.Common.Adaptors.CrmServiceAdaptor.GetInterests(String domainLogonAccount, Nullable`1 someId) in C:\Users\qqq\Documents\Visual Studio 2010\Projects\CrmService\CrmService.Common\Adaptors\CrmServiceAdaptor.cs:line 207
       at CrmService.ServiceLayer.Handlers.Q.GetInterestsHandler.Handle(GetInterestsRequest request) in C:\Users\qqq\Documents\Visual Studio 2010\Projects\CrmService\CrmService.ServiceLayer\Handlers\Q\InterestHandlers.cs:line 24

    After each request, the context is properly disposed. I'm using the non-cached OrganizationService. This is in my Web.config:

    <microsoft.xrm.client>
      <contexts default="Xrm">
        <add name="Xrm" type="Xrm.XrmServiceContext, Xrm" connectionStringName="Xrm" serviceName="Xrm"/>
      </contexts>
      <services default="Xrm">
        <add name="Xrm" type="Microsoft.Xrm.Client.Services.OrganizationService, Microsoft.Xrm.Client"/>
      </services>
    </microsoft.xrm.client>

    Here is another one, this time with a different Exception:

    System.ArgumentException: An item with the same key has already been added.
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at System.Collections.ObjectModel.KeyedCollection`2.InsertItem(Int32 index, TItem item)
       at System.Collections.Generic.KeyedByTypeCollection`1.InsertItem(Int32 index, TItem item)
       at Microsoft.Xrm.Sdk.Client.ProxyTypesBehavior.System.ServiceModel.Description.IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
       at System.ServiceModel.Description.DispatcherBuilder.ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime clientRuntime)
       at System.ServiceModel.Description.DispatcherBuilder.BuildProxyBehavior(ServiceEndpoint serviceEndpoint, BindingParameterCollection& parameters)
       at System.ServiceModel.Channels.ServiceChannelFactory.BuildChannelFactory(ServiceEndpoint serviceEndpoint, Boolean useActiveAutoClose)
       at System.ServiceModel.ChannelFactory.CreateFactory()
       at System.ServiceModel.ChannelFactory.OnOpening()
       at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
       at System.ServiceModel.ChannelFactory.EnsureOpened()
       at System.ServiceModel.ChannelFactory`1.CreateChannel(EndpointAddress address, Uri via)
       at Microsoft.Xrm.Sdk.Client.ServiceChannel`1.CreateChannel()
       at Microsoft.Xrm.Sdk.Client.ServiceChannel`1.ConfigureNewChannel()
       at Microsoft.Xrm.Sdk.Client.ServiceChannel`1.get_Channel()
       at Microsoft.Xrm.Sdk.Client.ServiceContextInitializer`1.Initialize(ServiceProxy`1 proxy)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceContextInitializer..ctor(OrganizationServiceProxy proxy)
       at Microsoft.Xrm.Sdk.Client.OrganizationServiceProxy.RetrieveMultipleCore(QueryBase query)
       at Microsoft.Xrm.Client.Services.OrganizationService.InnerOrganizationService.UsingService[TResult](Func`2 action)
       at CrmService.Common.Adaptors.QCrmServiceAdaptor.GetInterests(String domainLogonAccount, Nullable`1 someId) in C:\Users\qqq\Documents\Visual Studio 2010\Projects\CrmService\CrmService.Common\Adaptors\CrmServiceAdaptor.cs:line 207
       at CrmService.ServiceLayer.Handlers.Q.GetInterestsHandler.Handle(GetInterestsRequest request) in C:\Users\qqq\Documents\Visual Studio 2010\Projects\CrmService\CrmService.ServiceLayer\Handlers\Q\InterestHandlers.cs:line 24

    Now I'm fairly certain that this is at a layer I cannot access. Any ideas/pointers at what I could try?
    Tuesday, July 3, 2012 8:18 AM

All replies

  • Hi Thijs,

    Probably this is caused by sharing IServiceConfiguration or IServiceManagement in different threads. IServiceConfiguration is not safe thread. Also remember that thread safe are not guarantee with early-bound types. (See http://technet.microsoft.com/en-us/library/gg326004.aspx).

    You may find very interesting the new section Best Practices for Developing with Microsoft Dynamics CRM 2011, which is part of SDK 5.0.12 and newer ones.

    This is a quick solution you may try:

    CrmConnection crmConnection = new CrmConnection("myCRMConnection");
    crmConnection.ServiceConfigurationInstanceMode = ServiceConfigurationInstanceMode.PerInstance;
    using (CRMContext serviceContext = new CRMContext(crmConnection))
    {
    …
    }

    ServiceConfigurationInstanceMode.PerInstance is the key.

    Regards

    
    Monday, January 14, 2013 12:09 PM
  • Hi Ramon, thank you for your answer. Since it's already some time ago, I don't have access to the project anymore. But I'll keep it in mind next time I need to implement a multi-threaded application.
    Wednesday, January 23, 2013 10:35 PM