我有一个运行良好的 foreach 循环。但我想实现 TPL ,所以做了以下:
Parallel.ForEach(fileList, currentfileItem =>
{
_clientContext.Load(currentfileItem, w => w.File);
_clientContext.ExecuteQuery();
if (currentfileItem.File == null)
{
throw new Exception(
String.Format("File information not found for the item {0}",
currentfileItem.DisplayName));
}
var currentFileName = currentfileItem.File.Name;
if (!string.IsNullOrEmpty(docRevVersionId))
{
var info = Microsoft.SharePoint.Client.File.OpenBinaryDirect(
_clientContext, currentfileItem["fRef"].ToString());
if (info != null)
{
UpdateToServer(Id, currentFileName, info.Stream);
}
}
});
一旦我实施 TPL,我就会得到StackOverflow Exception
. 我很确定这是因为 TPL,因为没有 TPL,应用程序运行良好。
fileList
是IEnumerable<ListItem>
编辑:
SOE 可能是为了这个:(VS 已用尽内存来存储异常)
和更新的代码:
Parallel.ForEach(fileList,
() => CreateClientContext(ConfigurationDetails.SharePointUri), //This function creates a new context for the specified url.
(currentfileItem, loopState, localContext) =>
{
_clientContext.Load(currentfileItem, w => w.File);
_clientContext.ExecuteQuery();
if (currentfileItem.File == null)
{
throw new Exception(String.Format("File information not found for the item {0}", currentfileItem.DisplayName));
}
var currentFileName = currentfileItem.File.Name;
if (!string.IsNullOrEmpty(docRevVersionId))
{
var info = Microsoft.SharePoint.Client.File.OpenBinaryDirect(_clientContext, currentfileItem["fRef"].ToString());
if (info != null)
{
UpdateToServer(Id, currentFileName, info.Stream);
}
}
},localContext => localContext.Dispose());
private static ClientContext CreateClientContext(string URI)
{
ClientContext context = new ClientContext(URI);
//...Magic...
return context;
}