这个异步代码出了什么问题?

时间:2018-05-22 20:03:36

标签: c# .net parallel-processing active-directory

我有下面的代码从Active Directory中检索组。当我仅为MY_GROUP_NAME运行代码时(下面已注释掉),输出正如预期的那样。

当我从AD运行完整的组时,最终数据集不正确。一个具体的例子是,我最终在列表中有多个具有相同组名但不同ParentGroupGuids的广告组。这是一个无效的场景。此问题似乎与Parallel.ForEach()调用下面的递归方法有关。

知道问题可能是什么以及如何解决?

private ConcurrentBag<Core.Models.ADGroup> adGroups;        

public async Task<List<Core.Models.ADGroup>> GetADGroupsFromADAsync(string domainName)
{
    return await Task.Run(async() =>
    {
        var domainId = await new DomainRepository().GetDomainId(domainName);

        using (var context = new PrincipalContext(ContextType.Domain, domainName))
        {
            var ps = new PrincipalSearcher(new GroupPrincipal(context));
            Parallel.ForEach(
                ps.FindAll().ToList(),
                //ps.FindAll().Where(x => x.Name == "MY_GROUP_NAME").ToList(),
                new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
                async (group, loopState) =>
                {
                    await GetGroupsRecursive((Guid)domainId, null, (GroupPrincipal)group);
                });
        }

        //return group list
        return adGroups.ToList();
    });
}

private async Task GetGroupsRecursive(Guid domainId, Guid? parentGroupGuid, GroupPrincipal group)
{
    //cast result to adgroup
    var adGroup = Mapper.Map<Core.Models.ADGroup>(group);

    //set domainid
    adGroup.DomainId = domainId;

    //set parent group id
    adGroup.ParentGroupGuid = parentGroupGuid;

    //process child groups
    foreach (var member in group.Members)
        if (member is GroupPrincipal)
            await GetGroupsRecursive(domainId, adGroup.Guid, (GroupPrincipal)member);

    //add to the list
    adGroups.Add(adGroup);
}

1 个答案:

答案 0 :(得分:4)

同时使用多个线程中的PrincipalContext是不安全的。您在group.Members will call GetGroupsRecursive使用主要上下文的ContextRaw.QueryCtx.GetGroupMembership(this, false);来电内部。

这可能会导致您遇到的错误。您需要每个线程的上下文,或者不需要多线程成员查找。

编辑:您的代码还有另一个主要问题(在我尝试编写示例之前没有看到它),您正在使用as {/ await和Parallel.ForEach调用。 不支持, 您只能使用Parallel.ForEach执行同步方法,摆脱异步或切换到TPL Dataflow

以下是修复async / await并使其成为每个线程的上下文

的示例
public async Task<List<Core.Models.ADGroup>> GetADGroupsFromADAsync(string domainName)
{
    return await Task.Run(async() =>
    {
        var domainId = await new DomainRepository().GetDomainId(domainName);

        using (var searchContext = new PrincipalContext(ContextType.Domain, domainName))
        {
            var ps = new PrincipalSearcher(new GroupPrincipal(searchContext));
            Parallel.ForEach(
                ps.FindAll().Select(x=>x.DistinguishedName),
                new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount },
                () => new PrincipalContext(ContextType.Domain, domainName),
                (distinguishedName, loopState, threadLocalContext) =>
                {
                    var threadLocalGroup = GroupPrincipal.FindByIdentity(threadLocalContext, IdentityType.DistinguishedName, distinguishedName);
                    GetGroupsRecursive((Guid)domainId, null, threadLocalGroup);
                    return threadLocalContext;
                },
                threadLocalContext => threadLocalContext.Dispose());
        }

        //return group list
        return adGroups.ToList();
    });
}

private void GetGroupsRecursive(Guid domainId, Guid? parentGroupGuid, GroupPrincipal group)
{
    //cast result to adgroup
    var adGroup = Mapper.Map<Core.Models.ADGroup>(group);

    //set domainid
    adGroup.DomainId = domainId;

    //set parent group id
    adGroup.ParentGroupGuid = parentGroupGuid;

    //process child groups
    foreach (var member in group.Members)
        if (member is GroupPrincipal)
            GetGroupsRecursive(domainId, adGroup.Guid, (GroupPrincipal)member);

    //add to the list
    adGroups.Add(adGroup);
}