Skip to content

Commit

Permalink
Database: Fix SNI.dll loading [STUD-70051]
Browse files Browse the repository at this point in the history
Use Assembly.Location instead of Assembly.CodeBase for Net Core
For UNC path add extra / if needed
  • Loading branch information
viogroza committed Sep 2, 2024
1 parent 6337d2f commit f7ea32e
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,39 @@ public static void SNILoadWorkaround(bool isWindows = true)
if(!isWindows)
return;

IntPtr Handle = LoadLibrary(Path.GetFullPath((typeof(DbWorkarounds).Assembly.CodeBase.Replace("file:///", "")) + RelativePath));
var asmLocation = GetAssemblyLocation();
var path = Path.GetFullPath(asmLocation + RelativePath);

IntPtr Handle = LoadLibrary(path);
if (Handle == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
throw new Exception(string.Format("Failed to load library (ErrorCode: {0})", errorCode));
string errorMessage = string.Format("Failed to load library {0} (ErrorCode: {1})", path, errorCode);
throw new Exception(errorMessage);
}
}

#if NETCOREAPP
private static string GetAssemblyLocation()
{
return typeof(DbWorkarounds).Assembly.Location;
}
#else
private static string GetAssemblyLocation()
{
//on legacy still use the Assembly.CodeBase
//on legacy Assembly.Location returns the shadow copy location and not the original location (so we won't find the sni dll relative to this path)
var codeBase = typeof(DbWorkarounds).Assembly.CodeBase;
bool isUNCPath = codeBase.StartsWith("file:////");
var adjustdCodeBase = codeBase.Replace("file:///", "");
//workaround: if UNC path, we should make sure that it stats with "//" (double) and not "/" (single)
//since single "/" means relative path
if (isUNCPath && !adjustdCodeBase.StartsWith("//"))
adjustdCodeBase = "/" + adjustdCodeBase;

return adjustdCodeBase;
}
#endif

}
}

0 comments on commit f7ea32e

Please sign in to comment.