From a1c7d127ebaf73aeb02d7b32523d15ffabfa2ae6 Mon Sep 17 00:00:00 2001 From: Andrew Schwartzmeyer Date: Mon, 25 Jul 2016 13:28:16 -0700 Subject: [PATCH] Add WSManPluginWrapper to AssemblyLoadContext This function is required for PSRP on Linux, as we need to wrap the call into WSManPluginManagedEntryWrapper.InitPlugin such that it is invoked via reflection from an assembly loaded with our custom context. --- .../CoreCLR/CorePsAssemblyLoadContext.cs | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs index 073951c3c..6cf0d58b8 100644 --- a/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs +++ b/src/Microsoft.PowerShell.CoreCLR.AssemblyLoadContext/CoreCLR/CorePsAssemblyLoadContext.cs @@ -708,7 +708,33 @@ namespace System.Management.Automation PowerShellAssemblyLoadContext.InitializeSingleton(basePaths, useResolvingHandlerOnly: true); } - + + /// + /// Create a singleton of PowerShellAssemblyLoadContext. + /// Then load System.Management.Automation and call the WSManPluginManagedEntryWrapper delegate. + /// + /// + /// This method is used by the native host of the PSRP plugin. + /// + /// + /// Passed to delegate. + /// + public static int WSManPluginWrapper(IntPtr wkrPtrs) + { + string basePaths = System.IO.Path.GetDirectoryName(typeof(PowerShellAssemblyLoadContextInitializer).GetTypeInfo().Assembly.Location); + string entryAssemblyName = "System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"; + string entryTypeName = "System.Management.Automation.Remoting.WSManPluginManagedEntryWrapper"; + string entryMethodName = "InitPlugin"; + object[] args = { wkrPtrs }; + + var psLoadContext = PowerShellAssemblyLoadContext.InitializeSingleton(basePaths, useResolvingHandlerOnly: false); + var entryAssembly = psLoadContext.LoadFromAssemblyName(new AssemblyName(entryAssemblyName)); + var entryType = entryAssembly.GetType(entryTypeName, throwOnError: true, ignoreCase: true); + var methodInfo = entryType.GetMethod(entryMethodName, BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase); + + return (int)methodInfo.Invoke(null, args); + } + /// /// Create a singleton of PowerShellAssemblyLoadContext. /// Then load the assembly containing the actual entry point using it.