簡體   English   中英

遞歸組成員函數返回“對象已處置”錯誤

[英]Recursive Group member function returns 'Object is disposed' error

我正在嘗試編寫一個簡單的函數,我可以調用該函數以遞歸方式獲取Active Directory中某個組的組成員,但是我在一個我認為我不打算處置的對象上遇到``對象已處置''錯誤但可能是錯誤的):

namespace ADPlayArround
{
    using System;
    using System.DirectoryServices.AccountManagement;

    class Program
    {
        static void Main(string[] args)
        {
            var groupMembers = GetGroupMembership("SL-FILE-DAF-Agriculture-RID-PIFT", true);

            // I get the error here saying 'Cannot access a disposed object'
            foreach (var member in groupMembers)
            {
                Console.WriteLine(member.SamAccountName);
            }
        }

        private static PrincipalSearchResult<Principal> GetGroupMembership (string groupName, bool recurse = false)
        {
            PrincipalSearchResult<Principal> members;

            using (PrincipalContext context = new PrincipalContext(ContextType.Domain))
            {
                var group = GroupPrincipal.FindByIdentity(context, IdentityType.SamAccountName, groupName);

                members = @group?.GetMembers(recurse);

                return members;
            }
        }
    }
}

在發布此問題之前,我仔細查看了using語句和正確的方法來處置對象,據我所知,我正在處置稱為Context的PrincipalContext(因為在方法調用后不需要它),但我並未處置PrincipalSearchResult稱為成員(因為我想返回此Principals列表),但是我收到一條錯誤消息,指示仍在處理中?

我的理論是:

  • 成員正在以某種方式被處分 (我只是找不到方法)
  • 處理PrincipalContext會以某種方式引起鏈處理調用 (不太可能,但我不知道足夠的C#排除它)

所以我想我的問題是:

是否以某種方式處理了member變量,或者我的代碼中存在我無法理解的缺陷?

另外,有一種簡單的方法可以列出Principal的父組,以查看諸如

Group1 \\ Group2 \\ User1

使用ILSpy,我可以看到獲取SamAccountName的代碼:

public string SamAccountName
{
    [SecurityCritical]
    get
    {
        return this.HandleGet<string>(ref this.samName, "Principal.SamAccountName", ref this.samNameChanged);
    }
    [SecurityCritical]
    set
    {
        if (value == null || value.Length == 0)
        {
            throw new ArgumentNullException(string.Format(CultureInfo.CurrentCulture, StringResources.InvalidNullArgument, new object[]
            {
                "Principal.SamAccountName"
            }));
        }
        if (!this.GetStoreCtxToUse().IsValidProperty(this, "Principal.SamAccountName"))
        {
            throw new InvalidOperationException(StringResources.InvalidPropertyForStore);
        }
        this.HandleSet<string>(ref this.samName, value, ref this.samNameChanged, "Principal.SamAccountName");
    }
}

現在讓我們看看HandleGet <>函數內部的內容:

[SecurityCritical]
internal T HandleGet<T>(ref T currentValue, string name, ref LoadState state)
{
    this.CheckDisposedOrDeleted();
    if (state == LoadState.NotSet)
    {
        this.LoadIfNeeded(name);
        state = LoadState.Loaded;
    }
    return currentValue;
}

下一步,看看LoadIfNeeded:

[SecurityCritical]
internal void LoadIfNeeded(string principalPropertyName)
{
    if (!this.fakePrincipal && !this.unpersisted)
    {
        this.ctx.QueryCtx.Load(this, principalPropertyName);
    }
}

中提琴! 它嘗試訪問較早處置的PrincipalContext(ctx)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM