C# Linq到ad,已触发disconnectedcontext异常
我有一个异常,当我使用库测试我的一些代码时,它被触发 我正在使用具有以下功能的存储库:C# Linq到ad,已触发disconnectedcontext异常,c#,linq,active-directory,C#,Linq,Active Directory,我有一个异常,当我使用库测试我的一些代码时,它被触发 我正在使用具有以下功能的存储库: public UserAD GetUser(string username) { UserAD user = null; using (Root = CreateDirectoryEntry()) { var users = new DirectorySource<UserAD&g
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;
}
但我可以使用另一种方法调用GetUser:
[TestMethod]
public void RepositoryUtenteAD_AutenticazioneUtente_Authentication()
{
IAutenticazione auth = repositoryAD.AutenticazioneUtente("TestAD_OK", "TestAD_OK");
Assert.IsTrue(auth.IsAuthenticated);
}
认证方法如下(清除了含义和细节,错误仍然存在):
测试运行良好,断言为我提供了良好的值,但在清理测试后,我触发了disconnectedcontext异常。我假设它来自我使用的Interop.Adsi dll
在GetUser(username)中,我是否应该复制UserAD中的所有内容以进行克隆,从而清除上下文。。。?还是有更聪明的方法
谢谢你的阅读 找到了解决方案,即使它很奇怪。只有在调试模式下运行测试时才会出现错误,如果我选择run而不是debug,那么一切都很好 这就结束了这一章。谢谢你的阅读
[TestMethod]
public void RepositoryUtenteAD_AutenticazioneUtente_Authentication()
{
IAutenticazione auth = repositoryAD.AutenticazioneUtente("TestAD_OK", "TestAD_OK");
Assert.IsTrue(auth.IsAuthenticated);
}
public IAutenticazione AutenticazioneUtente(string username, string password)
{
bool IsWhyNotAuthentifiedFound = false;
IAutenticazione authenticazione = new Autenticazione();
UserAD user = GetUser(username);
return authenticazione;
}