I'm pushing a lot of data into CRM at times, like bulk updates. It is done in a workflow (CodeActivity
) class, with its Execute()
method. I can use the argument (the context) passed to that method to get the ServiceFactory, and then in turn create the IOrganizationService.
I then iterate a list of objects to update, change properties, and call service.Update(myObj)
. So far so good.
Now, to speed things up, I thought I'd run 2-4 threads that do the update in parallel.
Question 1 Should I reuse the service I just created, or create a new one, if I run two or more threads? I.e. create one service per thread, or share it?
I'm basically doing Parallel.Invoke(action1, action2, action3);
where each action needs a service instance to call service.Update(myObj);
Question 2
If calling serviceFactory.CreateOrganizationService(context.UserId)
, will it actually create a new service, or return an existing one?
I have everything else nailed down, and it is working just fine, but wanted to get a recommendation/best practice for when multithreading within a workflow's Execute()
method.