处置工作不正常的DirectorySearcher从 [英] Dispose not working as expected in DirectorySearcher

查看:204
本文介绍了处置工作不正常的DirectorySearcher从的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试连接并做一些简单的功能,如搜索使用C#的Active Directory。然而,我被困在一个问题。我使用DirectorySearcher从搜索目录。有目录中的条目的负荷。

I am trying to connect and do simple functionalities such as search on an Active Directory using C#. However, I am stuck in a problem. I am using DirectorySearcher to search the directory. There are loads of entries in the directory.

这是函数

void RunThis()
{
        DirectoryEntry de = new DirectoryEntry();
        de.Path = "LDAP://" + domainName;
        de.Username = username;
        de.Password = password;
        de.AuthenticationType = AuthenticationTypes.Secure;

        DirectorySearcher deSearch = new DirectorySearcher(de);
        //Skipping properties to load
        try
        {
            deSearch.SearchScope = SearchScope.Subtree;
            SearchResultCollection rescoll = deSearch.FindAll();
            deSearch.Dispose();
            rescoll.Dispose();
        }
        catch (Exception obj)
        {
            System.Console.WriteLine("Exception in getting results. {0}",obj.Message);
        }

     }
     de.Dispose();

} // end of function 

这是一个简单的功能,我修剪下来。我能找到很多帖子里面说,呼吁明确处置DirectorySearcher从或ResultCollection对象就能解决问题。

This is a sample function I trimmed down to. I could find a lot of posts which said that calling dispose explicitly of the DirectorySearcher or ResultCollection object will solve the problem.

不过,我看到任务使用的内存不断增加。没有太多别的事情中的code中的另一部分。当我评论的功能,内存使用稳定。

However, I see that the memory used by the task is increasing constantly. There isnt much else going in the other part of the code. When i comment the function, the memory usage becomes stable.

有没有其他人所面临的问题,并找到了解决办法?

Has anyone else faced the issue and found a solution?

PS:还有没有出路。我需要做的findAll:(

PS: And there is no way out. I need to do the findall :(

推荐答案

解决的办法是在这里

<一个href="http://stackoverflow.com/questions/5631972/memory-leak-when-using-directorysearcher-findall">Memory使用DirectorySearcher.FindAll时,泄漏()

有些问题的的FindAll API实现。如果你不枚举的结果,而不是用它一次,然后正确地处置不会工作。然而,列举并简单enumerator.moveNext()做一次后,其配置非常干净。这解决了我的问题! :)

Some problem with the FindAll API implementation. If you dont enumerate the results and not use it once, then dispose wont work properly. However, after enumerating it and a simple enumerator.moveNext() done once, it disposes nice and clean. This has solved my problem.! :)

这篇关于处置工作不正常的DirectorySearcher从的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆