c# - Correct method of retreaving data from another class thats wraped in using statment -


i'm getting exception "cannot access disposed object.". know i'm getting exception because object has been dispoed before had chance return results. know whats correct way retreave objects. may have unnecessary steps in code , may doing wrong?

so have button click event in main class. calls method shown in code below. getadcomputer method located in class (i made class file .cs) called activedirectory.cs. i'm geting exception when attempting access results of dispoed object.

public static principalsearchresult<principal> getadcomputer(string pcname)     {         using (principalcontext ctx = new principalcontext(contexttype.domain))         {             using (computerprincipal computer = new computerprincipal(ctx))             {                 computer.name = string.format("*{0}*", pcname);                  using (principalsearcher searcher = new principalsearcher())                 {                     searcher.queryfilter = computer;                     return searcher.findall();                 }             }         }                 }     

the problem directoryservices uses lazy loading it's information, because closed context can't retrieve information principal anymore.

you have 2 options, either pass principalcontext in query not go out of scope when return:

public static dosomthing() {     using(var ctx = new principalcontext(contexttype.domain))     using(principalsearchresult<principal> result = getadcomputer("some name", ctx))     {         //do 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 need convert results in not rely on lazy loading can close connection 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;} } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -