Skip to content

Commit e3fd198

Browse files
committed
Use ConvertFromJson/ConvertToJson APIs directly
1 parent 884c22a commit e3fd198

File tree

3 files changed

+21
-45
lines changed

3 files changed

+21
-45
lines changed

src/PowerShell/PowerShellManager.cs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -190,18 +190,6 @@ internal Hashtable InvokeFunction(
190190
}
191191
}
192192

193-
/// <summary>
194-
/// Helper method to convert the result returned from a function to JSON.
195-
/// </summary>
196-
internal string ConvertToJson(object fromObj)
197-
{
198-
return _pwsh.AddCommand("Microsoft.PowerShell.Utility\\ConvertTo-Json")
199-
.AddParameter("InputObject", fromObj)
200-
.AddParameter("Depth", 3)
201-
.AddParameter("Compress", true)
202-
.InvokeAndClearCommands<string>()[0];
203-
}
204-
205193
private void ResetRunspace(string moduleName)
206194
{
207195
// Reset the runspace to the Initial Session State

src/RequestProcessor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ private void ProcessInvocationRequestImpl(StreamingMessage request, AzFunctionIn
164164
? InvokeOrchestrationFunction(psManager, functionInfo, invocationRequest)
165165
: InvokeSingleActivityFunction(psManager, functionInfo, invocationRequest);
166166

167-
BindOutputFromResult(psManager, response.InvocationResponse, functionInfo, results);
167+
BindOutputFromResult(response.InvocationResponse, functionInfo, results);
168168
}
169169
catch (Exception e)
170170
{
@@ -239,7 +239,7 @@ private Hashtable InvokeSingleActivityFunction(PowerShellManager psManager, AzFu
239239
/// <summary>
240240
/// Set the 'ReturnValue' and 'OutputData' based on the invocation results appropriately.
241241
/// </summary>
242-
private void BindOutputFromResult(PowerShellManager psManager, InvocationResponse response, AzFunctionInfo functionInfo, Hashtable results)
242+
private void BindOutputFromResult(InvocationResponse response, AzFunctionInfo functionInfo, Hashtable results)
243243
{
244244
switch (functionInfo.Type)
245245
{
@@ -252,7 +252,7 @@ private void BindOutputFromResult(PowerShellManager psManager, InvocationRespons
252252

253253
object outValue = results[outBindingName];
254254
object transformedValue = Utils.TransformOutBindingValueAsNeeded(outBindingName, bindingInfo, outValue);
255-
TypedData dataToUse = transformedValue.ToTypedData(psManager);
255+
TypedData dataToUse = transformedValue.ToTypedData();
256256

257257
// if one of the bindings is '$return' we need to set the ReturnValue
258258
if(string.Equals(outBindingName, AzFunctionInfo.DollarReturn, StringComparison.OrdinalIgnoreCase))
@@ -273,7 +273,7 @@ private void BindOutputFromResult(PowerShellManager psManager, InvocationRespons
273273

274274
case AzFunctionType.OrchestrationFunction:
275275
case AzFunctionType.ActivityFunction:
276-
response.ReturnValue = results[AzFunctionInfo.DollarReturn].ToTypedData(psManager);
276+
response.ReturnValue = results[AzFunctionInfo.DollarReturn].ToTypedData();
277277
break;
278278

279279
default:

src/Utility/TypeExtensions.cs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
using System.IO;
99
using System.Linq;
1010
using System.Management.Automation;
11-
using System.Reflection;
1211

1312
using Google.Protobuf;
1413
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;
1514
using Microsoft.Azure.Functions.PowerShellWorker.PowerShell;
15+
using Microsoft.PowerShell.Commands;
1616
using Newtonsoft.Json.Linq;
1717

1818
namespace Microsoft.Azure.Functions.PowerShellWorker.Utility
@@ -94,30 +94,9 @@ internal static object ToObject(this TypedData data)
9494
}
9595
}
9696

97-
// PowerShell NuGet packages only have 'System.Management.Automation.dll' as the ref assembly, and thus types from other powershell assemblies
98-
// cannot be used directly in an application that reference the PowerShell NuGet packages. This is tracked by PowerShell#8121.
99-
// Here we need to use 'Microsoft.PowerShell.Commands.JsonObject' from 'Microsoft.PowerShell.Commands.Utility'. Due the above issue, we have to
100-
// use reflection to call 'JsonObject.ConvertFromJson(...)'.
101-
private static MethodInfo s_ConvertFromJson = null;
10297
private static object ConvertFromJson(string json)
10398
{
104-
const string UtilityAssemblyFullName = "Microsoft.PowerShell.Commands.Utility, Version=6.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35";
105-
106-
if (s_ConvertFromJson == null)
107-
{
108-
Assembly utilityAssembly = AppDomain.CurrentDomain.GetAssemblies().First(asm => asm.FullName == UtilityAssemblyFullName);
109-
Type jsonObjectType = utilityAssembly.GetType("Microsoft.PowerShell.Commands.JsonObject");
110-
s_ConvertFromJson = jsonObjectType.GetMethod(
111-
name: "ConvertFromJson",
112-
types: new Type[] { typeof(string), typeof(bool), typeof(ErrorRecord).MakeByRefType() },
113-
modifiers: null);
114-
}
115-
116-
object retObj = s_ConvertFromJson.Invoke(null, new object[] { json, true, null });
117-
if (retObj is PSObject psObj)
118-
{
119-
retObj = psObj.BaseObject;
120-
}
99+
object retObj = JsonObject.ConvertFromJson(json, returnHashtable: true, out _);
121100

122101
if (retObj is Hashtable hashtable)
123102
{
@@ -136,6 +115,16 @@ private static object ConvertFromJson(string json)
136115
return retObj;
137116
}
138117

118+
private static string ConvertToJson(object fromObj)
119+
{
120+
var context = new JsonObject.ConvertToJsonContext(
121+
maxDepth: 3,
122+
enumsAsStrings: false,
123+
compressOutput: true);
124+
125+
return JsonObject.ConvertToJson(fromObj, in context);
126+
}
127+
139128
internal static RpcException ToRpcException(this Exception exception)
140129
{
141130
return new RpcException
@@ -146,7 +135,7 @@ internal static RpcException ToRpcException(this Exception exception)
146135
};
147136
}
148137

149-
private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext, PowerShellManager psHelper)
138+
private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext)
150139
{
151140
var rpcHttp = new RpcHttp
152141
{
@@ -155,7 +144,7 @@ private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext, P
155144

156145
if (httpResponseContext.Body != null)
157146
{
158-
rpcHttp.Body = httpResponseContext.Body.ToTypedData(psHelper);
147+
rpcHttp.Body = httpResponseContext.Body.ToTypedData();
159148
}
160149

161150
rpcHttp.EnableContentNegotiation = httpResponseContext.EnableContentNegotiation;
@@ -178,7 +167,7 @@ private static RpcHttp ToRpcHttp(this HttpResponseContext httpResponseContext, P
178167
return rpcHttp;
179168
}
180169

181-
internal static TypedData ToTypedData(this object value, PowerShellManager psHelper)
170+
internal static TypedData ToTypedData(this object value)
182171
{
183172
if (value is TypedData self)
184173
{
@@ -218,14 +207,13 @@ internal static TypedData ToTypedData(this object value, PowerShellManager psHel
218207
typedData.Stream = ByteString.FromStream(s);
219208
break;
220209
case HttpResponseContext http:
221-
typedData.Http = http.ToRpcHttp(psHelper);
210+
typedData.Http = http.ToRpcHttp();
222211
break;
223212
case string str:
224213
if (IsValidJson(str)) { typedData.Json = str; } else { typedData.String = str; }
225214
break;
226215
default:
227-
if (psHelper == null) { throw new ArgumentNullException(nameof(psHelper)); }
228-
typedData.Json = psHelper.ConvertToJson(originalValue);
216+
typedData.Json = ConvertToJson(originalValue);
229217
break;
230218
}
231219
return typedData;

0 commit comments

Comments
 (0)