重启后,C#代码无法连接到ldap

时间:2014-12-15 11:04:49

标签: c# ldap

客户端:win XP嵌入式;服务器:win server 2008

代码:

    private DirectorySearcher getDirectorySearcher()
    {
        Runtime.Log.Info("Root Path Format: " + _rootPath);
        _directoryEntry = new DirectoryEntry(_rootPath, _username, _password);
        try
        {
            _directoryEntry.RefreshCache();

            return new DirectorySearcher(_directoryEntry)
            {
                PageSize = 1000
            };
        }
        catch (Exception ex)
        {
            _directoryEntry.Close();
            _directoryEntry.Dispose();
            throw;
        }
    }

这些代码在正常情况下运作良好。

连接到未启动的LDAP服务器后会说错误,但是当LDAP服务器重新启动时,它也无法连接到服务器。在客户端重启exe将解决此问题。

错误信息是:

    Exception authenticating user 'Administrator'

    System.Runtime.InteropServices.COMException (0x8007203A): The server is not operational.

       at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
       at System.DirectoryServices.DirectoryEntry.Bind()
       at System.DirectoryServices.DirectoryEntry.RefreshCache()
       at ThreeVR.EnterpriseServer.Ldap.LdapContext.getDirectorySearcher()

我尝试了每个方法来刷新或处理DirectoryEntry和DirectorySearcher对象,但它不起作用。

有人可以给我一些建议吗?有什么想法吗?

1 个答案:

答案 0 :(得分:0)

可能正在刷新DNS缓存可以帮忙吗?

using System.Runtime.InteropServices;

...

[DllImport("dnsapi.dll",EntryPoint="DnsFlushResolverCache")]
private static extern UInt32 DnsFlushResolverCache ();

...

public static void FlushResolverCache()
{
    UInt32 result = DnsFlushResolverCache();
}