连接缓存

建立与服务器的连接后,连接句柄会缓存在该进程的客户端计算机上,直到该连接关闭。 如果在后续连接中使用同一服务器、端口和凭据,且仅有 ADS_FAST_BINDADS_SERVER_BIND 身份验证标记不同,ADSI 则会重用此现有连接。 ADSI 会按进程执行此连接缓存操作。

若要提高性能,请尽可能重用现有连接。

下面是在 Visual Basic 中使用 IAD 的示例(另请参阅使用 IADs 接口):

Dim cachedConn As IADs
Dim obj As IADs
Dim cachedName As String
Dim objName As String
 
' Connect to the server and maintain this handle to cache the connection.
Set cachedConn = GetObject("LDAP://MyMachine/DC=MyDomain,DC=Fabrikam,DC=com")
 
cachedName = cachedConn.Get("distinguishedName")
Debug.Print (cachedName)
 
' Reuse the connection to MyMachine opened by cachedConn.
' Be aware that this line executes quickly because it is not required
' to transmit over the network again.
Set obj = GetObject("LDAP://MyMachine/CN=Bob,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com")
 
objName = obj.Get("distinguishedName")
Debug.Print (objName)
 
' Release the second connection.
Set obj = Nothing
 
' Reuse the connection to MyMachine opened by cachedConn again.
Set obj = GetObject("LDAP://MyMachine/CN=Administrator,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com")
 
objName = obj.Get("distinguishedName")
Debug.Print (objName)
 
' Release the second connection again.
Set obj = Nothing
 
' Release the first connection.
Set cachedConn = Nothing
 
' The connection to MyMachine is closed.

以下替代示例显示如何使用 .NET 中的 DirectoryEntry 对象进行连接缓存:

// Connect to the server and maintain this handle to cache the connection.
using (DirectoryEntry? cachedConn = new DirectoryEntry("LDAP://MyMachine/DC=MyDomain,DC=Fabrikam,DC=com")) 
{
    DirectoryEntry? secondConn;
    string? cachedName;
    string? objName;

    cachedName = cachedConn.Properties["distinguishedName"].Value?.ToString();
    Debug.WriteLine(cachedName);

    // Reuse the connection to MyMachine opened by cachedConn.
    // Be aware that this line executes quickly because it is not required
    // to transmit over the network again.
    using (secondConn = new DirectoryEntry("LDAP://MyMachine/CN=Bob,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com"))
    {
        objName = secondConn.Properties["distinguishedName"].Value?.ToString();
        Debug.WriteLine(objName);

        // Release the second connection.
        secondConn = null;

        // Reuse the connection to MyMachine opened by cachedConn again.
        secondConn = new DirectoryEntry("LDAP://MyMachine/CN=Administrator,CN=Users,DC=MyDomain,DC=Fabrikam,DC=com");

        objName = secondConn.Properties["distinguishedName"].Value?.ToString();
        Debug.WriteLine(objName);
    }
    // Release and dispose the second connection
}
// The connection to MyMachine is closed and disposed

另请参阅

DirectoryEntry

使用 IADs 接口