3

Is ClientContext thread-safe? I'm using Parallel framework to parallelize some work that creates new items on the list. Example:

using(var ctx = new ClientContext(siteUrl))
{
    var list = ctx.Web.GetList('/List/somelist')
    var subList = ctx.Web.GetList('/List/sublist')
    ctx.Load(list):
    ctx.Load(subList):
    ctx.ExecuteQuery();
    var myExternalItems = collection of items

    Parallel.ForEach(myExternalItems , new ParallelOptions() { MaxDegreeOfParallelism = 4 }, myitem =>
    {
        var item = list.AddItem(new ListItemCretionInformation());

        item["Title"] = myitem.Title; //just an example

        item.Update();
        ctx.ExecuteQuery();

        foreach(var sitem in myitem.SubItems)
        {
            var subitem = subList.AddItem(new ListItemCretionInformation());

            subitem["Title"] = sitem.Title;

            subitem.Update();
            ctx.ExecuteQuery();
        }
    });
}

It fails with:

An unhandled exception of type 'System.StackOverflowException' occurred in Microsoft.SharePoint.Client.Runtime.dll

And in Call Stack:

The maximum number of stack frames supported by Visual Studio has been exceeded.

It works if I replace Parallel.ForEach with a normal sequent foreach loop. It's not an error in field values or names, but something related to ClientContext's web request I guess. So is Client Object Model thread safe?

1 Answer 1

6

As the MSDN documentation states, instance members of ClientContext are not guaranteed to be thread-safe:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.clientcontext.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.