3

在我的 ASP.NET MVC 应用程序中,我试图检索具有版本历史记录的列表中的所有项目,然后将它们转换为自定义对象。为此,我正在使用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 方法:

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(布尔 includeTaskCanceledExceptions)

在 System.Threading.Tasks.Task.Wait(Int32 毫秒超时,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 源,ParallelOptions 并行选项,Action'1 主体,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 源,Action'1 主体)

在 GetSPObjectsWithHistory(SPQuery 查询,List`1 过滤器)中...

内部异常:

消息:尝试在单线程模式下调用多个线程。(来自 HRESULT 的异常:0x80010102 (RPC_E_ATTEMPTED_MULTITHREAD))

类型: Microsoft.SharePoint.SPException

堆栈跟踪:

在 Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)

在 Microsoft.SharePoint.Library.SPRequest.SetVar(字符串 bstrUrl,字符串 bstrName,字符串 bstrValue)

在 Microsoft.SharePoint.SPListItemVersionCollection.EnsureVersionsData()

在 Microsoft.SharePoint.SPListItemVersionCollection.get_Item(Int32 iIndex)

构造函数中的行。double.TryParse(spItem.Versions[0].VersionLabel, out _currentVersion);SPObjectWithHistory

内部异常

消息:尝试在单线程模式下调用多个线程。(来自 HRESULT 的异常:0x80010102 (RPC_E_ATTEMPTED_MULTITHREAD))

类型: System.Runtime.InteropServices.COMException

堆栈跟踪:

在 Microsoft.SharePoint.Library.SPRequestInternalClass.SetVar(字符串 bstrUrl,字符串 bstrName,字符串 bstrValue)

在 Microsoft.SharePoint.Library.SPRequest.SetVar(字符串 bstrUrl,字符串 bstrName,字符串 bstrValue)

会有人知道我如何让我的代码工作吗?

提前致谢!

4

1 回答 1

2

显然,我试图做的事情是不可能的。命名空间的Microsoft.SharePointSP 对象不是线程安全的,就像@JeroenMostert 所说的那样。

COM 是单线程的,除非代码明确指出,以避免多线程固有的所有问题。这个组件并不表示它对线程是安全的,所以它对线程是不安全的,无论你多么想要它。考虑使用延迟加载——例如,是否真的有必要预先检索该列表项的所有 80,000 项?什么用户会浏览它?即使您想要自定义对象,您也可以将必要的引用数据存储在自定义集合中,并按需实现/检索这些数据。

由于延迟加载对我来说不是一个选项,因此我决定将我的逻辑分成多个批次(使用System.Threading.Task),每个批次都执行我原始帖子中的代码(SPQuery.Query每个批次都进行更改)。之后,来自 my 的结果GetSPObjectsWithHistory将合并到一个列表中。

于 2018-01-16T10:16:52.930 回答