我从microsoft learning开始了一个演练,内容涉及如何使用CosmosDb为azure函数添加输入绑定,但是,在调用该函数时,它始终返回内部服务器错误(500 http代码)。
来自function.json的azure函数的配置为:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "Request",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "Response"
},
{
"name": "bookmark",
"direction": "in",
"type": "cosmosDB",
"databaseName": "func-io-learn-db",
"collectionName": "Bookmarks",
"connectionStringSetting": "learn_DOCUMENTDB",
"id": "{id}",
"partitionKey": "{id}",
"sqlQuery": ""
}
]
}
应用程序服务中有一个 learn_DOCUMENTDB 配置设置,该配置设置具有一个与cosmos数据库实例的有效连接字符串(已自动创建)。
错误日志条目显示:
无法将CosmosDB绑定为“ System.String”类型。可能的原因:1)尝试绑定到“ Microsoft.Azure.Documents.Client.DocumentClient,Microsoft.Azure.DocumentDB.Core,Version = 2.9.2.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35”,但用户类型程序集为“ System.String ,System.Private.CoreLib,版本= 4.0.0.0,文化=中性,PublicKeyToken = 7cec85d7bea7798e。
我在做错什么吗?
答案 0 :(得分:28)
对于仍在寻找此问题的任何人,如果上述解决方案不能解决问题。
我遇到了同样的问题,无法检索到数据。按照上面的建议检查了 function.json。问题不在于“sqlQuery”参数。
根据 Microsoft Learn 上的教程,在创建输入数据库绑定时,我已将绑定和分区 ID 值设置为“id”和“/id”。这在 function.JSON 文件中按原样显示。所以我的 JSON 文件看起来像: "id": "id", "partitionKey": "/id"
查看上述答案后,我将数据库的 function.JSON Bindings 更改为:
"id": "{id}",
"partitionKey": "{id}"
只有在此更改之后它才开始工作。
答案 1 :(得分:20)
我遇到了同样的问题,原来新的UI生成的绑定与旧的UI不同。
新界面:
{
"name": "bookmark",
"direction": "in",
"type": "cosmosDB",
"databaseName": "func-io-learn-db",
"collectionName": "Bookmarks",
"connectionStringSetting": "learn-0088a129-899f-4d18-b4db-5fa74daf1cc3_DOCUMENTDB",
"id": "{id}",
"partitionKey": "{id}",
"sqlQuery": ""
}
旧版用户界面:
{
"type": "cosmosDB",
"name": "bookmark",
"databaseName": "func-io-learn-db",
"collectionName": "Bookmarks",
"connectionStringSetting": "learn-0088a129-899f-4d18-b4db-5fa74daf1cc3_DOCUMENTDB",
"id": "{id}",
"partitionKey": "{id}",
"direction": "in"
}
删除
"sqlQuery": ""
绑定的一部分为我修复了它。
您可以通过点击应用服务概述页面上的“是否有问题?单击以返回到经典的功能应用管理经验”来切换回旧的UI,如您所见here。
答案 2 :(得分:8)
手动将 function.json 更新为:
"id": "{id}",
"partitionKey": "{id}",
对我有用,但是一旦调用或保存该函数,它就会将更改恢复为:
"id": "id",
"partitionKey": "/id",
并再次停止工作。我可以让它保留更改的唯一方法是像这样编辑 Integration 部分: settings for Input Binding 然后设置起作用了。
答案 3 :(得分:1)
Class1.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace FunctionApp52
{
public class Class1
{
public string Id { get; set; }
}
}
Function1.cs:
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
namespace FunctionApp52
{
public static class Function1
{
[FunctionName("DocByIdFromRouteData")]
public static IActionResult Run(
[HttpTrigger(AuthorizationLevel.Anonymous, "get", "post",
Route = "todoitems/{partitionKey}/{id}")]HttpRequest req,
[CosmosDB(
databaseName: "testbowman",
collectionName: "testbowman",
ConnectionStringSetting = "CosmosDBConnection",
Id = "{id}",
PartitionKey = "{partitionKey}")] Class1 item,
ILogger log)
{
log.LogInformation("C# HTTP trigger function processed a request.");
log.LogInformation( "Id is: "+item.Id);
if (item == null)
{
log.LogInformation($"ToDo item not found");
}
else
{
log.LogInformation($"Found ToDo item");
}
return new OkObjectResult("!!!!!!!!!!!!!!!!!!");
}
}
}
这是cosmos db中的项目:
{
"id": "bowmanid2",
"PartitionKey": "bowmankey",
"Description": "bowmandes",
"testbowman": " "
}
(顺便说一下,您需要设置patitionkey的值。在我这一边,是testbowman。)
然后我向http://localhost:7071/api/todoitems/ /bowmanid2
我可以得到:
答案 4 :(得分:1)
我没有足够的声誉来发表评论而不是回答,但是我遇到了同样的问题,而simandibalazs的回答却为我作了一些调整。似乎新的UI还在配置中添加了"id":""
的字段,如果您没有指定ID,则会发生相同的错误。就我而言,我只想使用sqlquery检索一组项目,而且这样做,我仍然必须删除"id":""
行。如果要检索所有项目,则必须从文件中删除以下两行。
"sqlQuery": ""
"id": ""
答案 5 :(得分:1)
没有足够的声誉来评论但: 我在使用UI时遇到了一些问题。两者都在绑定刀片中,如代码+测试页中所示。 因此,我去了功能应用程序本身上的应用程序服务编辑器来更改这些文件。
答案 6 :(得分:1)
我正在通过此处描述的练习之后进行练习,该练习需要将输出绑定到CosmosDB。我使用的是Javascript(node.js),即使按照上述建议,它也无法正常工作-直到我将Function Runtime v3切换回v2。 可以在您的功能->配置->功能运行时设置下找到。
答案 7 :(得分:0)
答案 8 :(得分:0)
作为@arno-peters replied in this question,使用 javascript 环境存在问题。但是,正如他所建议的,运行时 v2 不再可用(或者至少对我来说不在沙箱中)。无论如何,我只是将我的环境从 Node.js v14 更改为 v12,现在它可以使用 {id}
。
MS 肯定需要升级其文档...
答案 9 :(得分:0)
就我而言,它所需要的只是在分区键中的 id 之前加上“/”。 分区键:/id。
此外,如果不需要,请将 SQLQuery 保留为空。
我在函数应用中创建了一个新函数。添加了输入绑定,但我遇到了同样的错误,所以我意识到绑定有问题。