【问题标题】:Error while trying to bind Azure Function to CosmosDB尝试将 Azure 函数绑定到 CosmosDB 时出错
【发布时间】:2017-11-09 15:10:08
【问题描述】:

我想使用 DocumentDB 输出绑定将我的 Azure Function 与我的 CosmosDB 集合连接起来。

我的功能:

public static class HttpTriggerSave
{
    [FunctionName("HttpTriggerSave")]
    public static void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequestMessage req, [DocumentDB("dbName", "collectionName", Id = "id")] dynamic outputDoc, TraceWriter log)
    {
        outputDoc = new
        {
            Text = "text",
            id = Guid.NewGuid()
        };
    }
}

我的 local.settings.json

{
"IsEncrypted": false,
"Values": {
    "AzureWebJobsStorage": "",
    "AzureWebJobsDashboard": "",
    "AzureWebJobsServiceBus": "Endpoint=sb://<namespace>/;SharedAccessKeyName=<keyname>;SharedAccessKey=<key>",
    "AzureWebJobsDocumentDBConnectionString": "mongodb://..."
  }
}

但是我每次都会遇到同样的错误:

mscorlib: Exception while executing function: HttpTriggerSave. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'outputDoc'. Microsoft.Azure.Documents.Client: Value cannot be null.
Parameter name: authKeyOrResourceToken.

我该如何解决这个问题?

【问题讨论】:

  • 看起来您正在使用 Mongo 连接字符串。我不确定 Azure 功能是否支持此功能
  • 您可能需要如下更改属性。此示例用于在添加新文档时读取 [FunctionName("FunctionName")] publicstaticasync Task Run( [CosmosDBTrigger("dbname", "collection", ConnectionStringSetting = "Cosmos")] IReadOnlyList changeList, TraceWriter log)你当然还需要这个 NuGet 包:Microsoft.Azure.WebJobs.Extensions.DocumentDB 更多更新在下面的链接docs.microsoft.com/en-us/azure/azure-functions/…
  • @Baskar CosmosDBTrigger 属性在我想触发函数以响应数据库更改时必须使用。我想做相反的事情:我想将函数绑定到 CosmosDB,以便我可以在将新文档分配给 outputDoc 变量的函数中修改数据库

标签: azure azure-cosmosdb azure-functions


【解决方案1】:

mscorlib:执行函数时出现异常:HttpTriggerSave。 Microsoft.Azure.WebJobs.Host:异常绑定参数“outputDoc”。 Microsoft.Azure.Documents.Client:值不能为空。 参数名称:authKeyOrResourceToken。

根据异常,表示它无权访问Documentdb。根据您的 local.settings.json 使用 MongoDb 连接字符串。

所以请使用documentdb连接字符串。

AccountEndpoint=https://{documentDbName}.documents.azure.com:443/;AccountKey=xxxxx;

我也做了一个演示,它工作正常。

 [FunctionName("HttpTriggerSave")]
 public static void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,[DocumentDB("tomdb", "collectionId")] out dynamic outputDoc, TraceWriter log)
 {
    outputDoc = new
    {
          Text = "text",
          Id= Guid.NewGuid()
    };
  }

【讨论】:

  • 在此之后我摆脱了错误,但现在我得到“输入不是有效的 Base-64 字符串,因为它包含非 base 64 字符,...”我详细说明了在这里提问stackoverflow.com/questions/52822472/…如果你能帮忙请
猜你喜欢
  • 2021-08-16
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多