From 840590e4d840f083b7ef202a7125c7672aec6dcf Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Mon, 30 Nov 2020 11:54:04 +0100 Subject: [PATCH] simplify .NET framework loading logic --- .../Grpc.Core/Internal/NativeExtension.cs | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/csharp/Grpc.Core/Internal/NativeExtension.cs b/src/csharp/Grpc.Core/Internal/NativeExtension.cs index 50eb7f0a885..7528ffacf5b 100644 --- a/src/csharp/Grpc.Core/Internal/NativeExtension.cs +++ b/src/csharp/Grpc.Core/Internal/NativeExtension.cs @@ -80,7 +80,7 @@ namespace Grpc.Core.Internal /// /// Detects which configuration of native extension to load and load it. /// - private static UnmanagedLibrary LoadUnmanagedLibrary() + private static NativeMethods LoadNativeMethodsLegacyNetFramework() { // TODO: allow customizing path to native extension (possibly through exposing a GrpcEnvironment property). // See https://github.com/grpc/grpc/pull/7303 for one option. @@ -88,20 +88,17 @@ namespace Grpc.Core.Internal // With "classic" VS projects, the native libraries get copied using a .targets rule to the build output folder // alongside the compiled assembly. - // With dotnet cli projects targeting net45 framework, the native libraries (just the required ones) - // are similarly copied to the built output folder, through the magic of Microsoft.NETCore.Platforms. var classicPath = Path.Combine(assemblyDirectory, GetNativeLibraryFilename()); - // With dotnet cli project targeting netcoreappX.Y, projects will use Grpc.Core assembly directly in the location where it got restored - // by nuget. We locate the native libraries based on known structure of Grpc.Core nuget package. - // When "dotnet publish" is used, the runtimes directory is copied next to the published assemblies. - string runtimesDirectory = string.Format("runtimes/{0}/native", GetRuntimeIdString()); - var netCorePublishedAppStylePath = Path.Combine(assemblyDirectory, runtimesDirectory, GetNativeLibraryFilename()); - var netCoreAppStylePath = Path.Combine(assemblyDirectory, "../..", runtimesDirectory, GetNativeLibraryFilename()); - // Look for the native library in all possible locations in given order. - string[] paths = new[] { classicPath, netCorePublishedAppStylePath, netCoreAppStylePath}; - return new UnmanagedLibrary(paths); + string[] paths = new[] { classicPath }; + + // TODO(jtattermusch): the UnmanagedLibrary mechanism for loading the native extension while avoiding + // direct use of DllImport is quite complicated and is currently only needed to cover some niche scenarios + // (such legacy .NET Framework projects that use assembly shadowing) - everything else can be covered + // by using the [DllImport]. We should investigate the possibility of eliminating UnmanagedLibrary completely + // in the future. + return new NativeMethods(new UnmanagedLibrary(paths)); } /// @@ -153,7 +150,7 @@ namespace Grpc.Core.Internal return new NativeMethods(new NativeMethods.DllImportsFromSharedLib_x86()); } } - return new NativeMethods(LoadUnmanagedLibrary()); + return LoadNativeMethodsLegacyNetFramework(); } ///