The problem is DirectoryServices uses Lazy Loading for it's information, so because you closed the context you can't retrieve the information from the Principal
anymore.
You have two options, either pass the PrincipalContext
in to the query so it does not go out of scope when you return:
public static DoSomthing()
{
using(var ctx = new PrincipalContext(ContextType.Domain))
using(PrincipalSearchResult<Principal> result = GetADComputer("some Name", ctx))
{
//do something with the result here.
}
}
public static PrincipalSearchResult<Principal> GetADComputer(string pcName, PrincipalContext ctx)
{
using (var computer = new ComputerPrincipal(ctx))
{
computer.Name = String.Format("*{0}*", pcName);
using (var searcher = new PrincipalSearcher())
{
searcher.QueryFilter = computer;
return searcher.FindAll();
}
}
}
Or you will need to convert the results in to something that does not rely on lazy loading so you can close the connection to the directory server.
public static List<ComputerInfo> GetADComputer(string pcName, PrincipalContext ctx)
{
using (var computer = new ComputerPrincipal(ctx))
{
computer.Name = String.Format("*{0}*", pcName);
using (var searcher = new PrincipalSearcher())
{
searcher.QueryFilter = computer;
using (PrincipalSearchResult<Principal> result = searcher.FindAll())
{
return result.Select(p=> new ComputerInfo(p.Name, p.SID)).ToList();
}
}
}
}
public class ComputerInfo
{
public ComputerInfo(string name, SecurityIdentifier sid)
{
Name = name;
SID = sid;
}
public string Name {get; set;}
public SecurityIdentifier SID {get; set;}
}