From 1f9f3c7eae08761e57edf0a9226a60bb4e9d6ec4 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Fri, 13 Nov 2020 17:05:19 +0100 Subject: [PATCH] fix native extension loading in .NET5 single-file deployments --- .../Grpc.Core/Internal/NativeExtension.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/NativeExtension.cs b/src/csharp/Grpc.Core/Internal/NativeExtension.cs index 2a2bea363f9..84c12f4fccd 100644 --- a/src/csharp/Grpc.Core/Internal/NativeExtension.cs +++ b/src/csharp/Grpc.Core/Internal/NativeExtension.cs @@ -84,7 +84,7 @@ namespace Grpc.Core.Internal { // TODO: allow customizing path to native extension (possibly through exposing a GrpcEnvironment property). // See https://github.com/grpc/grpc/pull/7303 for one option. - var assemblyDirectory = Path.GetDirectoryName(GetAssemblyPath()); + var assemblyDirectory = GetAssemblyDirectory(); // With "classic" VS projects, the native libraries get copied using a .targets rule to the build output folder // alongside the compiled assembly. @@ -151,13 +151,20 @@ namespace Grpc.Core.Internal return new NativeMethods(new NativeMethods.DllImportsFromStaticLib()); } - private static string GetAssemblyPath() + private static string GetAssemblyDirectory() { var assembly = typeof(NativeExtension).GetTypeInfo().Assembly; #if NETSTANDARD // Assembly.EscapedCodeBase does not exist under CoreCLR, but assemblies imported from a nuget package // don't seem to be shadowed by DNX-based projects at all. - return assembly.Location; + var assemblyLocation = assembly.Location; + if (!string.IsNullOrEmpty(assemblyLocation)) + { + return Path.GetDirectoryName(assemblyLocation); + } + // In .NET5 single-file deployments, assembly.Location won't be available + // Also see https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file#other-considerations + return AppContext.BaseDirectory; #else // If assembly is shadowed (e.g. in a webapp), EscapedCodeBase is pointing // to the original location of the assembly, and Location is pointing @@ -167,9 +174,9 @@ namespace Grpc.Core.Internal var escapedCodeBase = assembly.EscapedCodeBase; if (IsFileUri(escapedCodeBase)) { - return new Uri(escapedCodeBase).LocalPath; + return Path.GetDirectoryName(new Uri(escapedCodeBase).LocalPath); } - return assembly.Location; + return Path.GetDirectoryName(assembly.Location); #endif }