尝试将 Azure 函数绑定到 CosmosDB 时出错

Error while trying to bind Azure Function to CosmosDB

我想使用 DocumentDB 输出绑定将我的 Azure 函数与我的 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.

我该如何解决?

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.

根据异常,说明没有访问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()
    };
  }