public void TryCreateReturnValueParameterDescriptor_ReturnBindingPresent_ReturnsExpectedValue()
        {
            JObject json = new JObject
            {
                { "type", "blob" },
                { "name", ScriptConstants.SystemReturnParameterBindingName },
                { "direction", "out" },
                { "path", "foo/bar" }
            };
            FunctionBinding functionBinding = TestHelpers.CreateTestBinding(json);
            FunctionBinding[] bindings = new FunctionBinding[] { functionBinding };

            ParameterDescriptor descriptor = null;
            var result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(string), bindings, out descriptor);
            Assert.True(result);
            Assert.Equal(ScriptConstants.SystemReturnParameterName, descriptor.Name);
            Assert.True((descriptor.Attributes & ParameterAttributes.Out) != 0);
            Assert.Equal(typeof(string).MakeByRefType(), descriptor.Type);
            Assert.Equal(1, descriptor.CustomAttributes.Count);

            result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(Task<string>), bindings, out descriptor);
            Assert.True(result);
            Assert.Equal(ScriptConstants.SystemReturnParameterName, descriptor.Name);
            Assert.True((descriptor.Attributes & ParameterAttributes.Out) != 0);
            Assert.Equal(typeof(string).MakeByRefType(), descriptor.Type);
            Assert.Equal(1, descriptor.CustomAttributes.Count);

            var ex = Assert.Throws<InvalidOperationException>(() =>
            {
                DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(Task), bindings, out descriptor);
            });
            Assert.Equal($"{ScriptConstants.SystemReturnParameterBindingName} cannot be bound to return type {typeof(Task).Name}.", ex.Message);
        }
        public void TryCreateReturnValueParameterDescriptor_NoReturnBinding_ReturnsExpectedValue()
        {
            JObject json = new JObject
            {
                { "type", "blob" },
                { "name", "myOutput" },
                { "direction", "out" },
                { "path", "foo/bar" }
            };
            FunctionBinding functionBinding = TestHelpers.CreateTestBinding(json);
            FunctionBinding[] bindings = new FunctionBinding[] { functionBinding };

            ParameterDescriptor descriptor = null;
            var result = DotNetFunctionDescriptorProvider.TryCreateReturnValueParameterDescriptor(typeof(string), bindings, out descriptor);
            Assert.False(result);
        }
コード例 #3
0
        internal static void SetResponse(HttpRequest request, object result)
        {
            // use the existing response if already set (IBinder model)
            if (request.HttpContext.Items.TryGetValue(ScriptConstants.AzureFunctionsHttpResponseKey, out object existing) && existing is IActionResult)
            {
                return;
            }

            IActionResult actionResult = result as IActionResult;

            if (actionResult == null)
            {
                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)
                {
                    actionResult = CreateResult(request, result);
                }
                else if (isActionResultHandlingEnabled && result is IConvertToActionResult convertable)
                {
                    // Convert ActionResult<T> to ActionResult
                    actionResult = convertable.Convert();
                }
                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;
                }
            }

            request.HttpContext.Items[ScriptConstants.AzureFunctionsHttpResponseKey] = actionResult;
        }
コード例 #4
0
        internal static Collection <FunctionBinding> GetBindings(ScriptHostConfiguration config, IEnumerable <BindingMetadata> bindingMetadatas, FileAccess fileAccess)
        {
            Collection <FunctionBinding> bindings = new Collection <FunctionBinding>();

            if (bindings != null)
            {
                foreach (var bindingMetadata in bindingMetadatas)
                {
                    string type = bindingMetadata.Type.ToLowerInvariant();
                    switch (type)
                    {
                    case "table":
                        TableBindingMetadata tableBindingMetadata = (TableBindingMetadata)bindingMetadata;
                        bindings.Add(new TableBinding(config, tableBindingMetadata, fileAccess));
                        break;

                    case "http":
                        if (fileAccess != FileAccess.Write)
                        {
                            throw new InvalidOperationException("Http binding can only be used for output.");
                        }
                        bindings.Add(new HttpBinding(config, bindingMetadata, FileAccess.Write));
                        break;

                    case "httptrigger":
                        bindings.Add(new HttpBinding(config, bindingMetadata, FileAccess.Read));
                        break;

                    default:
                        FunctionBinding binding = null;
                        if (TryParseFunctionBinding(config, bindingMetadata.Raw, out binding))
                        {
                            bindings.Add(binding);
                        }
                        break;
                    }
                }
            }

            return(bindings);
        }
コード例 #5
0
        private static bool TryParseFunctionBinding(ScriptJobHostOptions config, IEnumerable <IScriptBindingProvider> bindingProviders, JObject metadata, out FunctionBinding functionBinding)
        {
            functionBinding = null;

            ScriptBindingContext bindingContext = new ScriptBindingContext(metadata);
            ScriptBinding        scriptBinding  = null;

            foreach (var provider in bindingProviders)
            {
                if (provider.TryCreate(bindingContext, out scriptBinding))
                {
                    break;
                }
            }

            if (scriptBinding == null)
            {
                return(false);
            }

            BindingMetadata bindingMetadata = BindingMetadata.Create(metadata);

            functionBinding = new ExtensionBinding(config, scriptBinding, bindingMetadata);

            return(true);
        }
コード例 #6
0
        private static bool TryParseFunctionBinding(ScriptHostConfiguration config, Newtonsoft.Json.Linq.JObject metadata, out FunctionBinding functionBinding)
        {
            functionBinding = null;

            ScriptBindingContext bindingContext = new ScriptBindingContext(metadata);
            ScriptBinding        scriptBinding  = null;

            foreach (var provider in config.BindingProviders)
            {
                if (provider.TryCreate(bindingContext, out scriptBinding))
                {
                    break;
                }
            }

            if (scriptBinding == null)
            {
                return(false);
            }

            BindingMetadata bindingMetadata = BindingMetadata.Create(metadata);

            functionBinding = new ExtensionBinding(config, scriptBinding, bindingMetadata);

            return(true);
        }