Proper resource allocation when connecting to LDAP using C # directory services

It seems to me that you always need to properly manage resources when calling the Directory Services API without any exceptions (but many examples, blogs and manuals are often ignored or make one way with this call and the other with another call). Since all of the following DS classes implement the Dispose method, so I just want to confirm once and for all:

using (DirectoryEntry dirEntry = new DirectoryEntry()) { using (DirectorySearcher dirSearcher = new DirectorySearcher()) { dirSearcher.SearchRoot = dirEntry; dirSearcher.Filter = ...; using (SearchResultCollection src = dirSearcher.FindAll()) { //Other code that deals with result } } } 

always have to do. Do I face any risk, always systematically and religiously doing this?

+4
source share
1 answer

It is actually very important to delete Directory Services objects - many of them wrap COM + resources and you will cause resource leaks if you do not want to dispose of them.

So yes, you are doing the right thing, definitely, and no, there is no risk when you complete them in using .

+9
source

Source: https://habr.com/ru/post/1302933/


All Articles