System.InvalidOperationException 尝试使用绑定名称作为 azure 函数中的参数时
System.InvalidOperationException when trying use binding name as parameter in an azure function
按照本教程进行操作 chain-azure-functions-data-using-bindings,
使用 JavaScript 时它可以工作,但是使用 .net 创建了一个新的函数应用程序作为 运行 时间堆栈,添加了所需的 cosmos db 映射,当发送带有查询参数的 GET 请求时
https://azurefuncurl?code=abc&id=docs
appinsights 显示 azure function/host 不会启动,因为 System.InvalidOperationException
尝试查看官方文档:azure-functions/configInput-Usage,没有成功
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");
}
异常信息:
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.).
只需将{id}
替换为{Query.id}
,看看csx sample。
供将来参考
基于
,
我不得不将 id
替换为 function.json 中的 {Query.id}
,就像键 id 和 partitionKey
中的注释值一样
{
"type": "cosmosDB",
"name": "bookmark",
"databaseName": "func-io-learn-db",
"collectionName": "Bookmarks",
"connectionStringSetting": "chainazurefunctions_DOCUMENTDB",
"id": "{Query.id}",
"partitionKey": "{Query.id}",
"direction": "in"
}
在 run.csx
创建一个 POCO 模型 class 并将其用作 运行 方法中的参数,下面是整个 run.csx 的样子
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Text;
public class Bookmark
{
[JsonIgnore]
public string id {get; set;}
[JsonProperty(PropertyName ="url")]
public string URL {get;set;}
}
public static HttpResponseMessage Run(HttpRequest req, ILogger log, Bookmark bookmark)
{
log.LogInformation("C# HTTP trigger function processed a request.");
if(bookmark == null)
{
string id = req.Query["id"];
log.LogInformation($"Bookmark item {id} not found");
return new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent($"{id} not found", Encoding.UTF8, "application/json")
};
}
else
{
log.LogInformation($"Found item {bookmark.URL}");
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonConvert.SerializeObject(bookmark), Encoding.UTF8, "application/json")
};
}
}
按照本教程进行操作 chain-azure-functions-data-using-bindings, 使用 JavaScript 时它可以工作,但是使用 .net 创建了一个新的函数应用程序作为 运行 时间堆栈,添加了所需的 cosmos db 映射,当发送带有查询参数的 GET 请求时 https://azurefuncurl?code=abc&id=docs appinsights 显示 azure function/host 不会启动,因为 System.InvalidOperationException
尝试查看官方文档:azure-functions/configInput-Usage,没有成功
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");
}
异常信息:
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.).
只需将{id}
替换为{Query.id}
,看看csx sample。
供将来参考
基于 id
替换为 function.json 中的 {Query.id}
,就像键 id 和 partitionKey
{
"type": "cosmosDB",
"name": "bookmark",
"databaseName": "func-io-learn-db",
"collectionName": "Bookmarks",
"connectionStringSetting": "chainazurefunctions_DOCUMENTDB",
"id": "{Query.id}",
"partitionKey": "{Query.id}",
"direction": "in"
}
在 run.csx
创建一个 POCO 模型 class 并将其用作 运行 方法中的参数,下面是整个 run.csx 的样子
#r "Newtonsoft.Json"
using System.Net;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System.Text;
public class Bookmark
{
[JsonIgnore]
public string id {get; set;}
[JsonProperty(PropertyName ="url")]
public string URL {get;set;}
}
public static HttpResponseMessage Run(HttpRequest req, ILogger log, Bookmark bookmark)
{
log.LogInformation("C# HTTP trigger function processed a request.");
if(bookmark == null)
{
string id = req.Query["id"];
log.LogInformation($"Bookmark item {id} not found");
return new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent($"{id} not found", Encoding.UTF8, "application/json")
};
}
else
{
log.LogInformation($"Found item {bookmark.URL}");
return new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(JsonConvert.SerializeObject(bookmark), Encoding.UTF8, "application/json")
};
}
}