在我的ASP.NET MVC应用程序中,我正在尝试检索列表中所有带有版本历史的项目,然后将它们转换为自定义对象。为此,我正在使用
最初,我是这样做的:
这种方式在代码运行时能够实现功能,但是在处理大型列表时速度非常慢。其中一个列表包含超过80000个项目,在构造函数的逻辑中创建一个
为了加快该过程,我想使用
然后,我的
当我尝试运行应用程序时,
内容:一个或多个错误发生
类型:System.AggregateException
堆栈跟踪:
在 System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
在 System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
在 System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action'1 body, Action'2 bodyWithState, Func'4 bodyWithLocal, Func'1 localInit, Action'1 localFinally)
在 System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable'1 source, ParallelOptions parallelOptions, Action'1 body, Action'2 bodyWithState, Action'3 bodyWithStateAndIndex, Func'4 bodyWithStateAndLocal, Func'5 bodyWithEverything, Func'1 localInit, Action'1 localFinally)
在 System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable'1 source, Action'1 body)
在 GetSPObjectsWithHistory(SPQuery query, List`1 filters) in...
内部异常:
内容:尝试在单线程模式下使用多个线程进行调用。(HRESULT 异常: 0x80010102 (RPC_E_ATTEMPTED_MULTITHREAD))
类型:Microsoft.SharePoint.SPException
堆栈跟踪:
在 Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
在 Microsoft.SharePoint.Library.SPRequest.SetVar(String bstrUrl, String bstrName, String bstrValue)
在 Microsoft.SharePoint.SPListItemVersionCollection.EnsureVersionsData()
在 Microsoft.SharePoint.SPListItemVersionCollection.get_Item(Int32 iIndex)
在 SPObjectWithHistory 构造函数中的
内部异常:
内容:尝试在单线程模式下使用多个线程进行调用。(HRESULT 异常: 0x80010102 (RPC_E_ATTEMPTED_MULTITHREAD))
类型:System.Runtime.InteropServices.COMException
堆栈跟踪:
在 Microsoft.SharePoint.Library.SPRequestInternalClass.SetVar(String bstrUrl, String bstrName, String bstrValue)
在 Microsoft.SharePoint.Library.SPRequest.SetVar(String bstrUrl, String bstrName, String bstrValue)
请问哪位知道如何使我的代码正常运行?
Microsoft.SharePoint
。最初,我是这样做的:
Util.GetSPItemCollectionWithHistory
方法:public static SPListItemCollection GetSPItemCollectionWithHistory(string listName, SPQuery filterQuery)
{
using (SPSite spSite = new SPSite(sp_URL))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
SPList itemsList = spWeb.GetList("/Lists/" + listName);
SPListItemCollection listItems = itemsList.GetItems(filterQuery);
return listItems;
}
}
}
GetSPObjectsWithHistory method:
protected static List<SPObjectWithHistory<T>> GetSPObjectsWithHistory(SPQuery query = null, List<string> filters = null)
{
List<SPObjectWithHistory<T>> resultsList = new List<SPObjectWithHistory<T>>();
Type objectType = typeof(T);
string listName = "";
query = query ?? Util.DEFAULT_SSOM_QUERY;
if (objectType == typeof(SPProject)) listName = Util.PROJECTS_LIST_NAME;
else if (objectType == typeof(SPTask)) listName = Util.TASKS_LIST_NAME;
else throw new Exception(String.Format("Could not find the list name for {0} objects.", objectType.Name));
SPListItemCollection results = Util.GetSPItemCollectionWithHistory(listName, query);
foreach (SPListItem item in results)
{
resultsList.Add(new SPObjectWithHistory<T>(item, filters));
}
return resultsList;
}
SPObjectWithHistory类构造函数:
public SPObjectWithHistory(SPListItem spItem, List<string> filters = null)
{
double.TryParse(spItem.Versions[0].VersionLabel, out _currentVersion);
History = new Dictionary<double, T>();
if (spItem.Versions.Count > 1)
{
for (int i = 1; i < spItem.Versions.Count; i++)
{
if (filters == null)
History.Add(double.Parse(spItem.Versions[i].VersionLabel), SPObject<T>.ConvertSPItemVersionObjectToSPObject(spItem.Versions[i]));
else
{
foreach (string filter in filters)
{
if (i == spItem.Versions.Count - 1 || (string)spItem.Versions[i][filter] != (string)spItem.Versions[i + 1][filter])
{
History.Add(double.Parse(spItem.Versions[i].VersionLabel), SPObject<T>.ConvertSPItemVersionObjectToSPObject(spItem.Versions[i]));
break;
}
}
}
}
}
}
这种方式在代码运行时能够实现功能,但是在处理大型列表时速度非常慢。其中一个列表包含超过80000个项目,在构造函数的逻辑中创建一个
SPObjectWithHistory
项目需要约0.3秒钟。为了加快该过程,我想使用
Parallel.ForEach
代替普通的foreach
。然后,我的
GetSPObjectsWithHistory
更新为以下内容:protected static List<SPObjectWithHistory<T>> GetSPObjectsWithHistory(SPQuery query = null, List<string> filters = null)
{
ConcurrentBag<SPObjectWithHistory<T>> resultsList = new ConcurrentBag<SPObjectWithHistory<T>>();
Type objectType = typeof(T);
string listName = "";
query = query ?? Util.DEFAULT_SSOM_QUERY;
if (objectType == typeof(SPProject)) listName = Util.PROJECTS_LIST_NAME;
else if (objectType == typeof(SPTask)) listName = Util.TASKS_LIST_NAME;
else throw new Exception(String.Format("Could not find the list name for {0} objects.", objectType.Name));
List<SPListItem> results = Util.GetSPItemCollectionWithHistory(listName, query).Cast<SPListItem>().ToList();
Parallel.ForEach(results, item => resultsList.Add(new SPObjectWithHistory<T>(item, filters)));
return resultsList.ToList();
}
当我尝试运行应用程序时,
Parallel.ForEach
会出现以下异常:内容:一个或多个错误发生
类型:System.AggregateException
堆栈跟踪:
在 System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
在 System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken)
在 System.Threading.Tasks.Parallel.ForWorker[TLocal](Int32 fromInclusive, Int32 toExclusive, ParallelOptions parallelOptions, Action'1 body, Action'2 bodyWithState, Func'4 bodyWithLocal, Func'1 localInit, Action'1 localFinally)
在 System.Threading.Tasks.Parallel.ForEachWorker[TSource,TLocal](IEnumerable'1 source, ParallelOptions parallelOptions, Action'1 body, Action'2 bodyWithState, Action'3 bodyWithStateAndIndex, Func'4 bodyWithStateAndLocal, Func'5 bodyWithEverything, Func'1 localInit, Action'1 localFinally)
在 System.Threading.Tasks.Parallel.ForEach[TSource](IEnumerable'1 source, Action'1 body)
在 GetSPObjectsWithHistory(SPQuery query, List`1 filters) in...
内部异常:
内容:尝试在单线程模式下使用多个线程进行调用。(HRESULT 异常: 0x80010102 (RPC_E_ATTEMPTED_MULTITHREAD))
类型:Microsoft.SharePoint.SPException
堆栈跟踪:
在 Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
在 Microsoft.SharePoint.Library.SPRequest.SetVar(String bstrUrl, String bstrName, String bstrValue)
在 Microsoft.SharePoint.SPListItemVersionCollection.EnsureVersionsData()
在 Microsoft.SharePoint.SPListItemVersionCollection.get_Item(Int32 iIndex)
在 SPObjectWithHistory 构造函数中的
double.TryParse(spItem.Versions[0].VersionLabel, out _currentVersion);
内部异常:
内容:尝试在单线程模式下使用多个线程进行调用。(HRESULT 异常: 0x80010102 (RPC_E_ATTEMPTED_MULTITHREAD))
类型:System.Runtime.InteropServices.COMException
堆栈跟踪:
在 Microsoft.SharePoint.Library.SPRequestInternalClass.SetVar(String bstrUrl, String bstrName, String bstrValue)
在 Microsoft.SharePoint.Library.SPRequest.SetVar(String bstrUrl, String bstrName, String bstrValue)
请问哪位知道如何使我的代码正常运行?
预先感谢您!
Parallel.ForEach
,实际上会更快,因为应用程序将同时执行resultsList.Add(new SPObjectWithHistory<T>(item, filters));
(+ -0.3秒)多次。 在80000个项目的集合上,这将产生相当大的影响。 即使只在2个并行线程上执行,也会产生巨大的差异。 - DylanVB