Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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