UserPrincipal.GetGroups泄漏

时间:2011-05-09 16:06:34

标签: c# .net-3.5 active-directory group-membership

我已经跟踪了我的群组枚举代码泄漏。我写了一个测试程序,我试图处理所有的东西,但它仍然泄漏。

有谁看到我做错了什么?在我的测试中,我连续100次调用它,在我的小域上,内存占用量从32MB到超过150MB。

    private void EnumGroups()
    {
        try
        {
            using (PrincipalContext context = new PrincipalContext(ContextType.Domain, "domainname.com"))
            {

                using (PrincipalSearcher srch = new PrincipalSearcher())
                {
                    srch.QueryFilter = new UserPrincipal(context);

                    // do the search
                    using (PrincipalSearchResult<Principal> results = srch.FindAll())
                    {
                        // enumerate over results
                        foreach (UserPrincipal up in results)
                        {
                            using (PrincipalSearchResult<Principal> groups = up.GetGroups())
                            {
                                foreach (GroupPrincipal gp in groups)
                                    gp.Dispose();
                            }
                            up.Dispose();
                        }
                    }
                }
            }
        }
        catch (Exception exc)
        {
            Trace.WriteLine(exc);
        }
    }

1 个答案:

答案 0 :(得分:0)

从另一个临时域执行GetMembers:

dTaskTemporaryDomain = AppDomain.CreateDomain("BUHLO_POBEDIT_ZLO");
        var currentAssembly = Assembly.GetExecutingAssembly();
        AdsApiHelper = (AdsApiHelper)_adTaskTemporaryDomain.CreateInstanceAndUnwrap(currentAssembly.GetName().FullName, typeof(AdsApiHelper).ToString());
        var members = AdsApiHelper.GetMembers(GroupName);
 AppDomain.Unload(_adTaskTemporaryDomain)

...
[serializeble]
class AdsApiHelper {
  GetMembers(String GroupName){
   .....
   return groupPrincipal.GetMembers().Select(m=>m.SamAccountName);
  }