@@ -234,34 +234,15 @@ public static extern void fdb_transaction_clear_range(
234234
235235 static FdbNative ( )
236236 {
237- // Impact of NativeLibPath:
238- // - If null, don't preload the library, and let the CLR find the file using the default P/Invoke behavior
239- // - If String.Empty, call win32 LoadLibrary(FDB_C_DLL) and let the os find the file (using the standard OS behavior)
240- // - If path is folder, append the FDB_C_DLL
241- // Afterwards - call LoadLibrary with the resulting (relative or absolute) path
242-
243- var libraryPath = Fdb . Options . NativeLibPath ;
237+ var libraryPath = GetPreloadPath ( ) ;
244238
245239 if ( libraryPath == null )
246- {
240+ { // PInvoke will load
247241 return ;
248242 }
249-
243+
250244 try
251245 {
252- if ( libraryPath . Length == 0 )
253- { // CLR will handle the search
254- libraryPath = FDB_C_DLL ;
255- }
256- else
257- {
258- var fileName = Path . GetFileName ( libraryPath ) ;
259- if ( String . IsNullOrEmpty ( fileName ) )
260- {
261- libraryPath = Path . Combine ( libraryPath , FDB_C_DLL ) ;
262- }
263- }
264-
265246 FdbCLib = UnmanagedLibrary . Load ( libraryPath ) ;
266247 }
267248 catch ( Exception e )
@@ -274,13 +255,53 @@ static FdbNative()
274255 }
275256 else
276257 {
277- e = new InvalidOperationException ( "An error occurred while loading the native FoundationDB library" , e ) ;
258+ e = new InvalidOperationException ( $ "An error occurred while loading the native FoundationDB library: ' { libraryPath } '. ", e ) ;
278259 }
279260 LibraryLoadError = ExceptionDispatchInfo . Capture ( e ) ;
280261 }
281262
282263 }
283264
265+ private static string GetPreloadPath ( )
266+ {
267+ // we need to provide sensible defaults for loading the native library
268+ // if this method returns null we'll let PInvoke deal
269+ // otherwise - use explicit platform-specific dll loading
270+ var libraryPath = Fdb . Options . NativeLibPath ;
271+
272+ // on non-windows, library loading by convention just works.
273+ // unless override is provided, just let PInvoke do the work
274+ if ( ! RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
275+ {
276+ if ( string . IsNullOrEmpty ( libraryPath ) )
277+ {
278+ return null ;
279+ }
280+ // otherwise just use the provided path
281+ return libraryPath ;
282+ }
283+
284+ // Impact of NativeLibPath on windows:
285+ // - If null, don't preload the library, and let the CLR find the file using the default P/Invoke behavior
286+ // - If String.Empty, call win32 LoadLibrary(FDB_C_DLL + ".dll") and let the os find the file (using the standard OS behavior)
287+ // - If path is folder, append the FDB_C_DLL
288+ var winDllWithExtension = FDB_C_DLL + ".dll"
289+ if ( libraryPath == null )
290+ {
291+ return null ;
292+ }
293+ if ( libraryPath . Length = = 0 )
294+ {
295+ return winDllWithExtension ;
296+ }
297+ var fileName = Path . GetFileName ( libraryPath ) ;
298+ if ( String . IsNullOrEmpty ( fileName ) )
299+ {
300+ libraryPath = Path . Combine ( libraryPath , winDllWithExtension ) ;
301+ }
302+ return libraryPath ;
303+ }
304+
284305 private static void EnsureLibraryIsLoaded ( )
285306 {
286307 // should be inlined
0 commit comments