Skip to content

Commit b126c2d

Browse files
committed
Add DllImportResolver for .net core 3..0+
1 parent 06fe9a4 commit b126c2d

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

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

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Reflection;
56
using System.Runtime.InteropServices;
67
using static Microsoft.ML.OnnxRuntime.NativeMethods;
78

@@ -474,6 +475,13 @@ internal static class NativeMethods
474475

475476
static NativeMethods()
476477
{
478+
#if !NETSTANDARD2_0 && !__ANDROID__ && !__IOS__
479+
// Register a custom DllImportResolver to handle platform-specific library names.
480+
// On Linux, map onnxruntime.dll -> libonnxruntime.so
481+
// On macOS, map onnxruntime.dll -> libonnxruntime.dylib
482+
NativeLibrary.SetDllImportResolver(typeof(NativeMethods).Assembly, DllImportResolver);
483+
#endif
484+
477485
#if NETSTANDARD2_0
478486
IntPtr ortApiBasePtr = OrtGetApiBase();
479487
OrtApiBase ortApiBase = (OrtApiBase)Marshal.PtrToStructure(ortApiBasePtr, typeof(OrtApiBase));
@@ -847,7 +855,7 @@ static NativeMethods()
847855
api_.CreateSyncStreamForEpDevice,
848856
typeof(DOrtCreateSyncStreamForEpDevice));
849857

850-
OrtSyncStream_GetHandle =
858+
OrtSyncStream_GetHandle =
851859
(DOrtSyncStream_GetHandle)Marshal.GetDelegateForFunctionPointer(
852860
api_.SyncStream_GetHandle,
853861
typeof(DOrtSyncStream_GetHandle));
@@ -871,12 +879,52 @@ internal class NativeLib
871879
#elif __IOS__
872880
// Define the library name required for iOS
873881
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";
874888
#else
875-
// Note: the file name in ONNX Runtime nuget package must be onnxruntime.dll instead of onnxruntime.DLL(Windows filesystem can be case sensitive)
889+
// For .NET Core 3.0+, we use explicit .dll extension for Windows case-sensitivity
890+
// and register a DllImportResolver to handle Linux/macOS.
876891
internal const string DllName = "onnxruntime.dll";
877892
#endif
878893
}
879894

895+
#if !NETSTANDARD2_0 && !__ANDROID__ && !__IOS__
896+
/// <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.
901+
/// </summary>
902+
private static IntPtr DllImportResolver(string libraryName, Assembly assembly, DllImportSearchPath? searchPath)
903+
{
904+
if (libraryName == NativeLib.DllName)
905+
{
906+
// Map to platform-specific library name
907+
string mappedName = libraryName;
908+
if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
909+
{
910+
mappedName = "libonnxruntime.so";
911+
}
912+
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
913+
{
914+
mappedName = "libonnxruntime.dylib";
915+
}
916+
917+
if (NativeLibrary.TryLoad(mappedName, assembly, searchPath, out IntPtr handle))
918+
{
919+
return handle;
920+
}
921+
}
922+
923+
// Fall back to default resolution
924+
return IntPtr.Zero;
925+
}
926+
#endif
927+
880928
[DllImport(NativeLib.DllName, CharSet = CharSet.Ansi)]
881929
#if NETSTANDARD2_0
882930
public static extern IntPtr OrtGetApiBase();
@@ -2644,7 +2692,7 @@ public delegate void DOrtAddKeyValuePair(IntPtr /* OrtKeyValuePairs* */ kvps,
26442692
byte[] /* const char* */ value);
26452693

26462694
/// <summary>
2647-
/// Get the value for the provided key.
2695+
/// Get the value for the provided key.
26482696
/// </summary>
26492697
/// <returns>Value. Returns IntPtr.Zero if key was not found.</returns>
26502698
[UnmanagedFunctionPointer(CallingConvention.Winapi)]
@@ -2767,7 +2815,7 @@ out IntPtr /* OrtSyncStream** */ stream
27672815
// Auto Selection EP registration and selection customization
27682816

27692817
/// <summary>
2770-
/// Register an execution provider library.
2818+
/// Register an execution provider library.
27712819
/// The library must implement CreateEpFactories and ReleaseEpFactory.
27722820
/// </summary>
27732821
/// <param name="env">Environment to add the EP library to.</param>

0 commit comments

Comments
 (0)