Skip to content

Commit 90b85ea

Browse files
authored
Register a callback that tries to load other versions Open LDAP library if the default is failed (#88851)
* Register a callback to AssemblyLoadContext.Default.ResolvingUnmanagedDll event that tries to load other libraries * Do not throw when library not found * Check version 2.6 first
1 parent 023b54a commit 90b85ea

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/libraries/Common/src/Interop/Linux/Interop.Libraries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ internal static partial class Libraries
99
internal const string Liblog = "liblog";
1010

1111
internal const string Odbc32 = "libodbc.so.2";
12-
internal const string OpenLdap = "libldap-2.4.so.2";
12+
internal const string OpenLdap = "libldap-2.5.so.0";
1313
internal const string MsQuic = "libmsquic.so";
1414
}
1515
}

src/libraries/Common/src/Interop/Linux/OpenLdap/Interop.Ldap.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System;
5-
using System.Runtime.InteropServices;
65
using System.DirectoryServices.Protocols;
6+
using System.Reflection;
7+
using System.Runtime.InteropServices;
8+
using System.Runtime.Loader;
79

810
namespace System.DirectoryServices.Protocols
911
{
@@ -67,6 +69,26 @@ internal static partial class Ldap
6769
{
6870
static Ldap()
6971
{
72+
Assembly currentAssembly = typeof(Ldap).Assembly;
73+
74+
// Register callback that tries to load other libraries when the default library "libldap-2.5.so.0" not found
75+
AssemblyLoadContext.GetLoadContext(currentAssembly).ResolvingUnmanagedDll += (assembly, ldapName) =>
76+
{
77+
if (assembly != currentAssembly || ldapName != Libraries.OpenLdap)
78+
{
79+
return IntPtr.Zero;
80+
}
81+
82+
// Try load next (libldap-2.6.so.0) or previous (libldap-2.4.so.2) versions
83+
if (NativeLibrary.TryLoad("libldap-2.6.so.0", out IntPtr handle) ||
84+
NativeLibrary.TryLoad("libldap-2.4.so.2", out handle))
85+
{
86+
return handle;
87+
}
88+
89+
return IntPtr.Zero;
90+
};
91+
7092
// OpenLdap must be initialized on a single thread, once this is done it allows concurrent calls
7193
// By doing so in the static constructor we guarantee this is run before any other methods are called.
7294

0 commit comments

Comments
 (0)