azureazure-functionscsx

System.InvalidOperationException when trying use binding name as parameter in an azure function


follow this tutorial chain-azure-functions-data-using-bindings, when using JavaScript it works, however created a new function app with .net as run time stack, added required cosmos db mappings, when sending GET request with query param like https://azurefuncurl?code=abc&id=docs appinsights reveals azure function/host wont start because of System.InvalidOperationException

tried going through official documentation: azure-functions/configInput-Usage, no luck

function.json

{
 "bindings": [
{
  "authLevel": "function",
  "name": "req",
  "type": "httpTrigger",
  "direction": "in",
  "methods": [
    "get",
    "post"
  ]
},
{
  "name": "$return",
  "type": "http",
  "direction": "out"
},
{
  "type": "cosmosDB",
  "name": "bookmark",
  "databaseName": "func-io-learn-db",
  "collectionName": "Bookmarks",
  "connectionStringSetting": "chainazurefunctions_DOCUMENTDB",
  "id": "{id}",
  "partitionKey": "{id}",
  "direction": "in"
 }]
}

run.csx

#r "Newtonsoft.Json"
using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;

public static async Task<IActionResult> Run(HttpRequest req, ILogger log, dynamic bookmark)
{

log.LogInformation("C# HTTP trigger function processed a request.");

string name = req.Query["id"];

string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
dynamic data = JsonConvert.DeserializeObject(requestBody);
name = name ?? data?.name;

return name != null
    ? (ActionResult)new OkObjectResult($"Hello, {name}")
    : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

exception message:

Error indexing method 'Functions.find-bookmark' Unable to resolve binding parameter 'id'. Binding expressions must map to either a value provided by the trigger or a property of the value the trigger is bound to, or must be a system binding expression (e.g. sys.randguid, sys.utcnow, etc.).


Solution

  • Simply replace {id} with {Query.id}, have a look at the csx sample.