locked
Using Microsoft CRM 2011 SDK calls in Threading application RRS feed

  • Question

  • Hello All,

    I am using threading application in which I have CRM sdk call(like Create,Update,Execute) on crm entities which are different in each thread. I am using early bound entity classes generated by crmsvcutil tool. I am not adding service in service reference. Sometimes, I am getting following two errors indefinately.

    1)

    The HTTP request to 'https://orgname/XRMServices/2011/Organization.svc' was aborted.  This may be due to the local channel being closed while the request was still in progress.  If this behavior is not desired, then update your code so that it does not close the channel while request operations are still in progress.

    2)

    "Cannot access a disposed object.\r\nObject name: 'System.ServiceModel.Security.TransportSecurityProtocol'."

    Is there any settings which I need to change on server side where CRM is installed or I am missing anything else.

    Can any one please guid me on this?

    Monday, May 14, 2012 4:20 PM

Answers

  • The CRM SDK assemblies ultimately act as a WCF client, but generate the client settings without the use of an app.config file. I'm not aware of any opportunity to change the behaviour via configuring the server, but you can access and modify the client behaviour programmatically. The main starting point is the OrganizationServiceProxy.ServiceConfiguration.CurrentServiceEndpoint property. For example, you can iterate through the BindingElements with:

    OrganizationServiceProxy osp;
    // Instantiate osp
      
    if (osp.ServiceConfiguration.CurrentServiceEndpoint.Binding is CustomBinding)
    {
     CustomBinding cb = (CustomBinding)osp.ServiceConfiguration.CurrentServiceEndpoint.Binding;
     foreach (BindingElement be in cb.Elements)
     {
    
     }
    }


    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Monday, May 14, 2012 5:14 PM
    Moderator

All replies

  • The CRM SDK assemblies ultimately act as a WCF client, but generate the client settings without the use of an app.config file. I'm not aware of any opportunity to change the behaviour via configuring the server, but you can access and modify the client behaviour programmatically. The main starting point is the OrganizationServiceProxy.ServiceConfiguration.CurrentServiceEndpoint property. For example, you can iterate through the BindingElements with:

    OrganizationServiceProxy osp;
    // Instantiate osp
      
    if (osp.ServiceConfiguration.CurrentServiceEndpoint.Binding is CustomBinding)
    {
     CustomBinding cb = (CustomBinding)osp.ServiceConfiguration.CurrentServiceEndpoint.Binding;
     foreach (BindingElement be in cb.Elements)
     {
    
     }
    }


    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Monday, May 14, 2012 5:14 PM
    Moderator
  • I believe each thread will need its own instance of the CRM Service.  I have not been able to successfully get multi-threading to work from inside a plugin though.

    Blake Scarlavai - http://mscrmdev.blogspot.com/ - Sonoma Partners - http://www.sonomapartners.com/ - Follow @bscarlav
    CRM 2011 JavaScript Model Generator - CRM 2011 Appender for log4net

    Monday, May 14, 2012 7:33 PM
  • Jayashree,

    In your multi threading code, are you using the CrmServiceContext or the classic OrganizationService?

    If you are using CrmServiceContext, you will need to make sure that each thread should have its own copy of the CrmServiceContext. The CrmServiceContext class is not thread safe and should not be updated from multiple threads.

    I believe the two issues you are facing above is due to a single instance of CrmServiceContext is shared across multiple threads.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Monday, May 14, 2012 11:26 PM
  • Hello All,

    When I tried different instance of orgservice object in each thread I got following error:

    {"Secure channel cannot be opened because security negotiation with the remote endpoint has failed. This may be due to absent or incorrectly specified EndpointIdentity in the EndpointAddress used to create the channel. Please verify the EndpointIdentity specified or implied by the EndpointAddress correctly identifies the remote endpoint. "}

    Inner Exception-The request for security token has invalid or malformed elements.

    Dimaz, I am not using CrmServiceContext. I have created one test project and in that I am only using object of  IOrganizationService and also only execute method.

    Scenario which I understood till now is if we have following two statements which has been used by all threads:

    RetrieveAttributeRequest attributeRequest = new RetrieveAttributeRequest
                    {
                        EntityLogicalName =
                            EntityName,
                        LogicalName = optionlist.Key
                    };

                    // Execute the request
                    RetrieveAttributeResponse attributeResponse = (RetrieveAttributeResponse)crmDao.Orgservice.Execute(attributeRequest);
                    ICollection<object> values = attributeResponse.Results.Values;
                    OptionMetadataCollection op = null;
                    foreach (PicklistAttributeMetadata lookupobj in values)
                    {
                        op = lookupobj.OptionSet.Options;
                    }

    And if after creating request object in thread1 if pointer goes to thread 2 and perform some processing in thread 2 and them come back in thread 1,it doesn't have proper token values in Orgservice object and gives above error.

    Please help me with this and also if anyone tried threading application before can I get sample code related to this. I am really stuck up on this very badly...:(

    Tuesday, May 15, 2012 11:20 AM
  • Do you have separate instance of crmDao and Orgservice for each thread?

    The latest error message doesn't look like caused by the multi-threading stuff. Can you verify that your code still works even when running single thread?


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Tuesday, May 15, 2012 11:43 AM
  • Hello Dimaz,

    Thanks for your support.  I have tried with seperate object instance of crmDao and Orgservice for each thread. Also it's working but sometimes it gives above error. I am not getting any clue why its working for some time and then gives above error indefinately and then again starts working.

    DavidJennaway

    Can you please explain me little more about how can I use  mentioned code to avoide above errors?


    Wednesday, May 16, 2012 6:30 AM
  • Jayashree,

    I've just remembered there was an issue similar to this back from CRM 4.

    Essentially if you are calling the CRM Web Service too many times within a short period of time, you could get this error.

    You could try putting some sort of Thread.Sleep in your code for 50-100 milliseconds / use the UnsafeConnection property. Generally that should fix the problem.

    The code that David provided should be inserted during the creation of IOrganizationService. When you create the IOrganizationService, you have to create the OrganizationServiceProxy object first. That's the place to put it.

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


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits


    Wednesday, May 16, 2012 6:46 AM
  • The symptoms where the code works, then stops for a while, then starts again sounds like socket exhaustion, which can occur if you submit requests at a rate of around 1000 per minute. This can be avoided either by server configuration, or using UnsafeConnectionSharing in your client code. To set UnsafeConnectionSharing if using the SDK assemblies, try:

    OrganizationServiceProxy osp;
    // Instantiate osp
    
    if (osp.ServiceConfiguration.CurrentServiceEndpoint.Binding is CustomBinding)
    {
     CustomBinding cb = (CustomBinding)osp.ServiceConfiguration.CurrentServiceEndpoint.Binding;
     foreach (BindingElement be in cb.Elements)
     {
      if (be is HttpTransportBindingElement)
      {
       ((HttpTransportBindingElement)be).UnsafeConnectionNtlmAuthentication = true;
      }
     }
    }

    You could also try restructuring your code. It seems inefficient and unnecssary to continually execute RetrieveAttribute requests, as the data retrieved is generally static. I'd suggest you retrieve all the attribute metadata that you might need, and cache it locally

    Microsoft CRM MVP - http://mscrmuk.blogspot.com  http://www.excitation.co.uk


    Wednesday, May 16, 2012 7:00 AM
    Moderator
  • David,

    Thanks for that code. So that's the missing UnsafeConnection property in CRM 2011.

    Back in CRM 4, that property was directly exposed in the OrganizationService class.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Wednesday, May 16, 2012 7:03 AM
  • Thanks David,.

    I tried with above code, but still got same error again. Is there anything which I also need to do at server side?

    Wednesday, May 16, 2012 7:25 AM
  • Jayashree,

    You are pretty limited to what you can change on the server side. One little change could cause your whole implementation become unsupported by Microsoft.

    Can you try putting thread.sleep for about 50ms, see if it works?

    Cheers.


    Dimaz Pramudya - CRM Developer - CSG (Melbourne) www.xrmbits.com http://twitter.com/xrmbits

    Wednesday, May 16, 2012 7:31 AM
  • Sorry I missed link of your blog mentioned in your reply. I will try to do that settings. Thanks for your help David.
    Wednesday, May 16, 2012 7:32 AM
  • Hi All,

    I am facing the same problem, was anyone able to solve this issue?

    Thanks!!

    Wednesday, July 11, 2012 5:08 PM
  • Hi Jayashree,

    I am facing the same problem, could you tell me how did you resolve this issue?

    Thanks !!!!!

    Friday, January 23, 2015 5:58 AM