Skip to content

Commit 484d8dd

Browse files
committed
update
1 parent 5b9d360 commit 484d8dd

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.shared.cs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -879,44 +879,35 @@ internal class NativeLib
879879
#elif __IOS__
880880
// Define the library name required for iOS
881881
internal const string DllName = "__Internal";
882-
#elif NETSTANDARD2_0
883-
// For .NET Standard 2.0, use the library name without extension
884-
// to allow .NET's automatic platform-specific resolution.
885-
// Note: This may have issues on case-sensitive Windows filesystems
886-
// where LoadLibrary appends ".DLL" (uppercase).
887-
internal const string DllName = "onnxruntime";
888882
#else
889-
// For .NET Core 3.0+, we use explicit .dll extension for Windows case-sensitivity
890-
// and register a DllImportResolver to handle Linux/macOS.
891-
internal const string DllName = "onnxruntime.dll";
883+
// For desktop platforms (including .NET Standard 2.0), we use the simple name
884+
// to allow .NET's automatic platform-specific resolution (lib*.so, lib*.dylib, *.dll).
885+
// For .NET Core 3.0+, case-sensitivity on Windows is handled by DllImportResolver.
886+
internal const string DllName = "onnxruntime";
892887
#endif
893888
}
894889

895890
#if !NETSTANDARD2_0 && !__ANDROID__ && !__IOS__
896891
/// <summary>
897-
/// Custom DllImportResolver to handle platform-specific library names.
898-
/// On Windows, the library is named onnxruntime.dll.
899-
/// On Linux, the library is named libonnxruntime.so.
900-
/// On macOS, the library is named libonnxruntime.dylib.
892+
/// Custom DllImportResolver to handle platform-specific library loading.
893+
/// On Windows, it explicitly loads the library with a lowercase .dll extension to handle
894+
/// case-sensitive filesystems. On other platforms (Linux/macOS), it returns IntPtr.Zero
895+
/// to allow .NET's default, NuGet-aware resolution logic to find the library.
901896
/// </summary>
902897
private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
903898
{
904-
if (libraryName == NativeLib.DllName)
899+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && (libraryName == NativeLib.DllName || libraryName == OrtExtensionsNativeMethods.ExtensionsDllName))
905900
{
906-
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
907-
{
908-
return IntPtr.Zero;
909-
}
910-
911-
// On Linux/macOS, .NET's NativeLibrary.TryLoad works best with the simple library name
912-
// to find assets in the runtimes folder of a NuGet package.
913-
if (NativeLibrary.TryLoad("onnxruntime", assembly, searchPath, out IntPtr handle))
901+
// Explicitly load with .dll extension to avoid issues where the OS might try .DLL
902+
string fileName = libraryName == NativeLib.DllName ? "onnxruntime.dll" : "ortextensions.dll";
903+
if (NativeLibrary.TryLoad(fileName, assembly, searchPath, out IntPtr handle))
914904
{
915905
return handle;
916906
}
917907
}
918908

919-
// Fall back to default resolution
909+
// For Linux and macOS, returning IntPtr.Zero allows .NET to use its default
910+
// resolution logic, which correctly finds libraries in the runtimes folder of NuGet.
920911
return IntPtr.Zero;
921912
}
922913
#endif

0 commit comments

Comments
 (0)