C# codes could not connect to ldap after it restart

0

Client: win XP embedded; server: win server 2008

code:

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

These codes works well in normal situation.

It will say error after connecting to the not started LDAP server, but when LDAP server restart-up, it also could not connect to the server. restart the exe in client will resolve this issue.

The error info is:

    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()

I tried every method to refresh or dispose DirectoryEntry and DirectorySearcher objects, but it not work.

Could anybody give me some advises? Any ideas?

c#
ldap
asked on Stack Overflow Dec 15, 2014 by Mark He • edited Dec 15, 2014 by Mark He

1 Answer

0

May be flushing the DNS cache can help?

using System.Runtime.InteropServices;

...

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

...

public static void FlushResolverCache()
{
    UInt32 result = DnsFlushResolverCache();
}
answered on Stack Overflow Dec 16, 2014 by baldpate

User contributions licensed under CC BY-SA 3.0