Skip to content
Snippets Groups Projects
Commit a643960d authored by Jan Tattermusch's avatar Jan Tattermusch
Browse files

Merge pull request #5810 from jtattermusch/extension_loading_for_webapps

Correct handling for shadowed assemblies when loading native extension
parents 2b0f9e87 3560a439
No related branches found
No related tags found
No related merge requests found
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#endregion #endregion
using System; using System;
using System.Globalization;
using System.IO; using System.IO;
using System.Reflection; using System.Reflection;
...@@ -99,14 +100,30 @@ namespace Grpc.Core.Internal ...@@ -99,14 +100,30 @@ namespace Grpc.Core.Internal
// TODO: allow customizing path to native extension (possibly through exposing a GrpcEnvironment property). // TODO: allow customizing path to native extension (possibly through exposing a GrpcEnvironment property).
var libraryFlavor = string.Format("{0}_{1}", GetPlatformString(), GetArchitectureString()); var libraryFlavor = string.Format("{0}_{1}", GetPlatformString(), GetArchitectureString());
var fullPath = Path.Combine(GetExecutingAssemblyDirectory(), var fullPath = Path.Combine(Path.GetDirectoryName(GetAssemblyPath()),
NativeLibrariesDir, libraryFlavor, GetNativeLibraryFilename()); NativeLibrariesDir, libraryFlavor, GetNativeLibraryFilename());
return new UnmanagedLibrary(fullPath); return new UnmanagedLibrary(fullPath);
} }
private static string GetExecutingAssemblyDirectory() private static string GetAssemblyPath()
{ {
return Path.GetDirectoryName(typeof(NativeExtension).GetTypeInfo().Assembly.Location); var assembly = typeof(NativeExtension).GetTypeInfo().Assembly;
// If assembly is shadowed (e.g. in a webapp), EscapedCodeBase is pointing
// to the original location of the assembly, and Location is pointing
// to the shadow copy. We care about the original location because
// the native dlls don't get shadowed.
var escapedCodeBase = assembly.EscapedCodeBase;
if (IsFileUri(escapedCodeBase))
{
return new Uri(escapedCodeBase).LocalPath;
}
return assembly.Location;
}
private static bool IsFileUri(string uri)
{
return uri.ToLowerInvariant().StartsWith(Uri.UriSchemeFile);
} }
private static string GetPlatformString() private static string GetPlatformString()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment