Skip to content

Commit 8806d2f

Browse files
committed
Removing blocking code on NodeFunctionInvoker async path
1 parent 7582ae1 commit 8806d2f

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/WebJobs.Script/Description/Node/NodeFunctionInvoker.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ protected override async Task InvokeCore(object[] parameters, FunctionInvocation
116116
DataType dataType = _trigger.DataType ?? DataType.String;
117117

118118
var userTraceWriter = CreateUserTraceWriter(context.TraceWriter);
119-
var scriptExecutionContext = CreateScriptExecutionContext(input, dataType, userTraceWriter, context);
119+
var scriptExecutionContext = await CreateScriptExecutionContextAsync(input, dataType, userTraceWriter, context).ConfigureAwait(false);
120120
var bindingData = (Dictionary<string, object>)scriptExecutionContext["bindingData"];
121121

122122
await ProcessInputBindingsAsync(context.Binder, scriptExecutionContext, bindingData);
@@ -253,7 +253,7 @@ protected override void OnScriptFileChanged(object sender, FileSystemEventArgs e
253253
}
254254
}
255255

256-
private Dictionary<string, object> CreateScriptExecutionContext(object input, DataType dataType, TraceWriter traceWriter, FunctionInvocationContext invocationContext)
256+
private async Task<Dictionary<string, object>> CreateScriptExecutionContextAsync(object input, DataType dataType, TraceWriter traceWriter, FunctionInvocationContext invocationContext)
257257
{
258258
// create a TraceWriter wrapper that can be exposed to Node.js
259259
var log = (Func<object, Task<object>>)(p =>
@@ -303,12 +303,11 @@ private Dictionary<string, object> CreateScriptExecutionContext(object input, Da
303303
context["_entryPoint"] = _entryPoint;
304304
}
305305

306-
if (input is HttpRequestMessage)
306+
// convert the request to a json object
307+
if (input is HttpRequestMessage request)
307308
{
308-
// convert the request to a json object
309-
HttpRequestMessage request = (HttpRequestMessage)input;
310-
string rawBody = null;
311-
var requestObject = CreateRequestObject(request, out rawBody);
309+
(Dictionary<string, object> requestObject, string rawBody) = await CreateRequestObjectAsync(request).ConfigureAwait(false);
310+
312311
input = requestObject;
313312

314313
if (rawBody != null)
@@ -458,9 +457,9 @@ internal static bool IsEdgeSupportedType(Type type)
458457
return false;
459458
}
460459

461-
private static Dictionary<string, object> CreateRequestObject(HttpRequestMessage request, out string rawBody)
460+
private static async Task<(Dictionary<string, object> request, string rawBody)> CreateRequestObjectAsync(HttpRequestMessage request)
462461
{
463-
rawBody = null;
462+
string rawBody = null;
464463

465464
// TODO: need to provide access to remaining request properties
466465
Dictionary<string, object> requestObject = new Dictionary<string, object>();
@@ -483,7 +482,7 @@ private static Dictionary<string, object> CreateRequestObject(HttpRequestMessage
483482
{
484483
if (contentType.MediaType == "application/json")
485484
{
486-
body = request.Content.ReadAsStringAsync().Result;
485+
body = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
487486
if (TryConvertJson((string)body, out jsonObject))
488487
{
489488
// if the content - type of the request is json, deserialize into an object or array
@@ -493,14 +492,14 @@ private static Dictionary<string, object> CreateRequestObject(HttpRequestMessage
493492
}
494493
else if (contentType.MediaType == "application/octet-stream")
495494
{
496-
body = request.Content.ReadAsByteArrayAsync().Result;
495+
body = await request.Content.ReadAsByteArrayAsync().ConfigureAwait(false);
497496
}
498497
}
499498

500499
if (body == null)
501500
{
502501
// if we don't have a content type, default to reading as string
503-
body = rawBody = request.Content.ReadAsStringAsync().Result;
502+
body = rawBody = await request.Content.ReadAsStringAsync().ConfigureAwait(false);
504503
}
505504

506505
requestObject["body"] = body;
@@ -514,7 +513,7 @@ private static Dictionary<string, object> CreateRequestObject(HttpRequestMessage
514513
requestObject["params"] = routeData;
515514
}
516515

517-
return requestObject;
516+
return (requestObject, rawBody);
518517
}
519518

520519
/// <summary>

src/WebJobs.Script/GlobalSuppressions.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,11 @@
152152
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.ScriptHost.#Initialize()")]
153153
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.ScriptHost.#LoadCustomExtensions(Newtonsoft.Json.Linq.JObject)")]
154154
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.ScriptHost.#LoadCustomExtensions()")]
155+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1009:Closing parenthesis must be spaced correctly", Justification = "<Pending>", Scope = "member", Target = "~T:Microsoft.Azure.WebJobs.Script.Description.NodeFunctionInvoker")]
156+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Reflection.Assembly.LoadFrom", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.ScriptHost.#LoadCustomExtensions()")]
155157
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.Binding.HttpTriggerAttribute.#Methods")]
156158
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.Description.FunctionDescriptor.#InputBindings")]
157159
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.Description.FunctionDescriptor.#OutputBindings")]
158160
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Scope = "member", Target = "Microsoft.Azure.WebJobs.Script.Description.HttpTriggerBindingMetadata.#Methods")]
159161
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Scope = "member", Target = "Microsoft.Azure.WebJobs.HttpJobHostConfigurationExtensions.#UseHttp(Microsoft.Azure.WebJobs.JobHostConfiguration,Microsoft.Azure.WebJobs.Script.Binding.Http.HttpConfiguration)")]
160-
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1014:MarkAssembliesWithClsCompliant")]
162+
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1014:MarkAssembliesWithClsCompliant")]

0 commit comments

Comments
 (0)