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
------------------------------
Original Message:
Sent: Nov 11, 2019 08:55 AM
From: Vikas Gangishetti
Subject: CRM SDK with Parallel Processing
Thanks for the Response Mark. I did try that and still getting the same response.
------------------------------
Vikas Gangishetti
CAPFinancial Partners, LLC
Raleigh NC
Original Message:
Sent: Nov 11, 2019 04:55 AM
From: Mark Carrington
Subject: CRM SDK with Parallel Processing
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
Original Message:
Sent: Nov 08, 2019 04:04 PM
From: Vikas Gangishetti
Subject: CRM SDK with Parallel Processing
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
------------------------------