Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到ad,已触发disconnectedcontext异常_C#_Linq_Active Directory - Fatal编程技术网

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;
}