Skip to content

Commit

Permalink
Merge pull request Azure#2049 from kashimiz/feature/node-return-values
Browse files Browse the repository at this point in the history
Enable passing return values from non-C# functions.
  • Loading branch information
kashimiz authored Nov 20, 2017
2 parents 9aa7572 + d24e152 commit 7945f7a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/WebJobs.Script.WebHost/WebJobs.Script.WebHost.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<PackageReference Include="Microsoft.Azure.AppService.Proxy.Client.Contract" Version="0.3.1.1">
<NoWarn>NU1701</NoWarn>
</PackageReference>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3-11077" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0-beta3" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.0-beta3">
<NoWarn>NU1701</NoWarn>
Expand Down
34 changes: 23 additions & 11 deletions src/WebJobs.Script/Binding/Http/HttpBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,21 @@
using System.Dynamic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection.Emit;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.WebApiCompatShim;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Net.Http.Headers;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.WebJobs.Script.Binding
{
public class HttpBinding : FunctionBinding
{
public HttpBinding(ScriptHostConfiguration config, BindingMetadata metadata, FileAccess access)
public HttpBinding(ScriptHostConfiguration config, BindingMetadata metadata, FileAccess access)
: base(config, metadata, access)
{
}
Expand Down Expand Up @@ -188,17 +186,31 @@ internal static void SetResponse(HttpRequest request, object result)
IActionResult actionResult = result as IActionResult;
if (actionResult == null)
{
var objectResult = new ObjectResult(result);

if (result is System.Net.Http.HttpResponseMessage)
if (result is Stream)
{
// for script language functions (e.g. PowerShell, BAT, etc.) the value
// will be a Stream which we need to convert to string
FunctionBinding.ConvertStreamToValue((Stream)result, DataType.String, ref result);
actionResult = CreateResult(request, result);
}
else if (result is JObject)
{
// To maintain backwards compatibility, if the type returned is an
// instance of an HttpResponseMessage, add the appropriate formatter to
// handle the response
objectResult.Formatters.Add(new HttpResponseMessageOutputFormatter());
actionResult = CreateResult(request, result);
}
else
{
var objectResult = new ObjectResult(result);

if (result is System.Net.Http.HttpResponseMessage)
{
// To maintain backwards compatibility, if the type returned is an
// instance of an HttpResponseMessage, add the appropriate formatter to
// handle the response
objectResult.Formatters.Add(new HttpResponseMessageOutputFormatter());
}

actionResult = objectResult;
actionResult = objectResult;
}
}

request.HttpContext.Items[ScriptConstants.AzureFunctionsHttpResponseKey] = actionResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading.Tasks.Dataflow;
using Microsoft.Azure.WebJobs.Description;
using Microsoft.Azure.WebJobs.Script.Binding;
using Microsoft.Azure.WebJobs.Script.Rpc;

Expand Down Expand Up @@ -40,5 +44,47 @@ protected override IFunctionInvoker CreateFunctionInvoker(string scriptFilePath,
});
return new WorkerLanguageInvoker(Host, triggerMetadata, functionMetadata, inputBindings, outputBindings, inputBuffer);
}

protected override Collection<ParameterDescriptor> GetFunctionParameters(IFunctionInvoker functionInvoker, FunctionMetadata functionMetadata,
BindingMetadata triggerMetadata, Collection<CustomAttributeBuilder> methodAttributes, Collection<FunctionBinding> inputBindings, Collection<FunctionBinding> outputBindings)
{
var parameters = base.GetFunctionParameters(functionInvoker, functionMetadata, triggerMetadata, methodAttributes, inputBindings, outputBindings);

var bindings = inputBindings.Union(outputBindings);

try
{
var triggerHandlesReturnValueBinding = bindings.SingleOrDefault(b =>
b.Metadata.IsTrigger &&
(b as ExtensionBinding)?.Attributes.SingleOrDefault(a =>
(a.GetType().GetCustomAttribute(typeof(BindingAttribute)) as BindingAttribute)?.TriggerHandlesReturnValue == true)
!= null);

if (triggerHandlesReturnValueBinding != null)
{
var byRefType = typeof(object).MakeByRefType();

ParameterDescriptor returnDescriptor = new ParameterDescriptor(ScriptConstants.SystemReturnParameterName, byRefType);
returnDescriptor.Attributes |= ParameterAttributes.Out;

Collection<CustomAttributeBuilder> customAttributes = triggerHandlesReturnValueBinding.GetCustomAttributes(byRefType);
if (customAttributes != null)
{
foreach (var customAttribute in customAttributes)
{
returnDescriptor.CustomAttributes.Add(customAttribute);
}
}

parameters.Add(returnDescriptor);
}
}
catch (InvalidOperationException ex)
{
throw new InvalidOperationException("Multiple bindings cannot be designated as HandlesReturnValue.", ex);
}

return parameters;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected override async Task<object> InvokeCore(object[] parameters, FunctionIn
result = await invocationContext.ResultSource.Task;

await BindOutputsAsync(triggerValue, context.Binder, result);
return null;
return result.Return;
}

private async Task<(string name, DataType type, object value)[]> BindInputsAsync(Binder binder)
Expand Down
2 changes: 1 addition & 1 deletion src/WebJobs.Script/WebJobs.Script.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PackageReference Include="Microsoft.Azure.Functions.NodeJsWorker" Version="1.0.0-beta1-10022">
<PrivateAssets>None</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3" />
<PackageReference Include="Microsoft.Azure.WebJobs" Version="3.0.0-beta3-11077" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions" Version="3.0.0-beta3" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.Http" Version="3.0.0-beta3">
<NoWarn>NU1701</NoWarn>
Expand Down

0 comments on commit 7945f7a

Please sign in to comment.