1
votes

I want to connect my Azure Function with my CosmosDB collection using DocumentDB output binding.

My Function:

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()
        };
    }
}

My local.settings.json

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

However I get every time the same error:

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.

How can I fix that?

1
It looks like you're using a Mongo connection string. I'm not sure that this is supported yet for Azure functions - Jesse Carter
You might need to change the attribute as below. This sample is to read when a new doc is added [FunctionName("FunctionName")] publicstaticasync Task Run( [CosmosDBTrigger("dbname", "collection", ConnectionStringSetting = "Cosmos")] IReadOnlyList<Document> changeList, TraceWriter log) You of course also need this NuGet package: Microsoft.Azure.WebJobs.Extensions.DocumentDB More updates in the below link docs.microsoft.com/en-us/azure/azure-functions/… - Baskar Rao
@Baskar The CosmosDBTrigger attribute must be used when I want to trigger the function in response to database changes. I want to do the opposite: I want to bind the function to CosmosDB so that I can modify the database within the function assigning the new document to outputDoc variable - Alessio Innocenzi

1 Answers

2
votes

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.

According to the exception, it indicates that it has no access to Documentdb. According to your local.settings.json that you use the MongoDb connection string.

So please use the documentdb connection string.

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

And I also do a demo on my side, it works correctly.

 [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()
    };
  }

enter image description here