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