Customer Engagement & Dynamics CRM Forum

Expand all | Collapse all

CRM SDK with Parallel Processing

  • 1.  CRM SDK with Parallel Processing

    Posted 13 days ago

    Hi Everyone,

    We are running into an Issue where Organizations Service is getting Disposed when we ran Multiple records in Parallel.

    We created a webapi with 6 controller each processing 20 records one time. For each record we are initiating Service and passing it to multiple functions to get records from CRM to do various calculations and Most of them run good but at least 14% of them failing with "Cannot access a disposed object Object name: 'System.ServiceModel.Security.TransportSecurityProtocol" Error for CRM Service.

    Below is the code for parallel processing.

    Parallel.ForEach<Tuple<int, int>, IOrganizationService>(rangePartitioner,
    new ParallelOptions() { MaxDegreeOfParallelism = 10},
    () =>
    {
    var service = orghelper.GetOrganizationService(connectionstring);//getting organization service
    return service;
    },
    (range, loopState, index, service) =>
    {
    for (int i = range.Item1; i < range.Item2; i++)
    {
    XrmServiceContext _service = null;
    RunProcess RunProcess = null;
    Retry retry = null;
    var id = listParams[i].Id;
    try
    {
    _service=orghelper.GetXrmService(connectionstring);//Getting XRM Service
    RunProcess = new RunProcess (listParams[i]);//initiating new class
    retry = new Retry(); //initiating new classv
    service = orghelper.GetOrganizationService(connectionstring);
    bool status = retry.Do(() => RunProcess.Execute(_service, service, connection));
    if (status)
    {
    var itemtoremove = List.Single(r => r.Id == id);
    List.Remove(itemtoremove);
    }
    else
    {
    exceptions.Enqueue(new Exception("Failed: for id " + id.ToString()));
    }

    }
    catch (AggregateException ex)
    {
    foreach (var e in ex.Flatten().InnerExceptions)
    {
    exceptions.Enqueue(e);
    }
    }
    catch (Exception ex)
    {
    exceptions.Enqueue(ex);
    }
    finally
    {
    if(_service!=null)_service = null;
    if RunProcess != null) RunProcess = null;
    if(retry != null)retry = null;
    }

    }

    return service;
    },
    (service) =>
    {
    service = null;
    }
    );


    Any help would be greatly appreciated.


    Thanks!



    ------------------------------
    Vikas Gangishetti
    CAPFinancial Partners, LLC
    Raleigh NC
    ------------------------------
    Conference-CRMUG_200x200


  • 2.  RE: CRM SDK with Parallel Processing

    TOP CONTRIBUTOR
    Posted 11 days ago
    Not sure if it's related, but you seem to be creating more IOrganizationService instances than required. You create one in your loop initializer function, which then gets passed to your loop body as the service parameter. But within your loop body you then overwrite this with a new IOrganizationService instance again, rather than reusing the one that's already there. You might want to try removing that second call to orghelper.GetOrganizationService and see what difference that makes.

    ------------------------------
    Mark Carrington
    Chief Technologist
    Data8
    Chester
    ------------------------------

    Conference-CRMUG_200x200


  • 3.  RE: CRM SDK with Parallel Processing

    Posted 10 days ago
    Thanks for the Response Mark. I did try that and still getting the same response.

    ------------------------------
    Vikas Gangishetti
    CAPFinancial Partners, LLC
    Raleigh NC
    ------------------------------

    Conference-CRMUG_200x200


  • 4.  RE: CRM SDK with Parallel Processing

    TOP CONTRIBUTOR
    Posted 10 days ago
    It definitely looks like you are reusing an organisation service from one thread on another based on the exception. Is there any way your orghelper can return the same instance twice, or your RunProcess or Retry classes could run your code on another thread? What about your connection object, does that represent an SDK connection in any way as that is shared across your threads?

    ------------------------------
    Mark Carrington
    Chief Technologist
    Data8
    Chester
    ------------------------------

    Conference-CRMUG_200x200


  • 5.  RE: CRM SDK with Parallel Processing

    Posted 10 days ago
    Hey Mark,

    I looked at the code again and did not find anywhere we are using same connection twice. we are creating service before each task in parallel process and passing it in to functions.

    ------------------------------
    Vikas Gangishetti
    CAPFinancial Partners, LLC
    Raleigh NC
    ------------------------------

    Conference-CRMUG_200x200


If you've found this thread useful, dive deeper into User Group community content by role