I have an 开发者_C百科exception which is fired when I test some code of mine using the library Linq to AD.
I am using a repository with this function :
public UserAD GetUser(string username)
{
UserAD user = null;
using (Root = CreateDirectoryEntry())
{
var users = new DirectorySource<UserAD>(Root, SearchScope.Subtree);
user = users.Where(x => x.AccountName == username)
.AsEnumerable()
.SingleOrDefault(); //not supported in LDAP; alternative in-memory SingleOrDefault
}
return user;
}
This works fine when I call it directly:
[TestMethod]
public void RepositoryUtenteAD_GetUser()
{
UserAD user = repositoryAD.GetUser("TestAD_OK");
Assert.AreEqual("blablabla", user.DistinguishedName);
Assert.IsFalse(user.AccountDisabled);
}
But I may use another method which will call GetUser:
[TestMethod]
public void RepositoryUtenteAD_AutenticazioneUtente_Authentication()
{
IAutenticazione auth = repositoryAD.AutenticazioneUtente("TestAD_OK", "TestAD_OK");
Assert.IsTrue(auth.IsAuthenticated);
}
and the Authentication method is as follows (cleared of meaning and details, the error remaining there) :
public IAutenticazione AutenticazioneUtente(string username, string password)
{
bool IsWhyNotAuthentifiedFound = false;
IAutenticazione authenticazione = new Autenticazione();
UserAD user = GetUser(username);
return authenticazione;
}
The test is running fine, the assert is giving me the good value, but after the Cleanup of my test I have a disconnectedcontext exception fired. I assume it comes from the Interop.Adsi dll I use.
In GetUser(username) Should I copy everything I have in my UserAD to make a clone and so be clear off context...? Or is there a more clever way to approach?
Thanks for your reading!
Found the solution even if it is a strange one. The bug only happens when running the test in debug mode, if i choose run instead of debug, everything is fine.
That close the chapter. Thanks for your reading.
精彩评论