0

I'm trying to make a function that will take some input and store it in a cosmos db. I've written this function:

using Microsoft.AspNetCore.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Host;

    [FunctionName("DocumentUpdates")]
        public static void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequest req,
            [CosmosDB("Fagkveld", "ViktigeData", CreateIfNotExists = true, Id = "Id", ConnectionStringSetting = "CosmosDbConn")]out dynamic document,
            TraceWriter log)
        {
            document = new {Id="Identifier1", Title = "Some Title"};
}

But when I run this I get the following error:

[24.04.2018 07:05:15] Executed 'DocumentUpdates' (Failed, Id=ce0deab0-5ba3-4bb0-9399-96661c52ecb8)
[24.04.2018 07:05:15] System.Private.CoreLib: Exception while executing function: DocumentUpdates. Microsoft.Azure.WebJobs.Host: Exception binding parameter 'document'. Microsoft.Azure.DocumentDB.Core: Value cannot be null.
[24.04.2018 07:05:15] Parameter name: serviceEndpoint.

Can someone tell me what I'm doing wrong? I can't find any serviceEndpoint parameter?

6
  • According to the documentation below you should use DocumentDB output binding type, not CosmosDB. Did you try it? learn.microsoft.com/en-us/azure/azure-functions/… Commented Apr 24, 2018 at 8:15
  • You can use CosmosDBTrigger which is part of Microsoft.Azure.WebJobs, but you won't have CreateIfNotExists and Id flags. If you could use DocumentDB instead, you will be able to use the two flags. Commented Apr 24, 2018 at 9:34
  • @Vladislav I'm using Functions version 2, where the other bindings also where renamed. So I think I should use the CosmosDB attribute. Could not find a DocumentDb attribute either. Commented Apr 24, 2018 at 12:40
  • 2
    @HoriaToma CosmosDBTrigger is to detect changes in cosmosDb, right? I'm trying to update cosmos db with the inputvalues to a http triggered function. Commented Apr 24, 2018 at 12:41
  • 1
    @Zaphod I'm using runtime 1.x and I still periodically run into similar issues. Most often, they appear after I add/update some nuget package. In such cases I try to revert the package to an older version and test with each one whether the binding succeeds. And the other case (currently reconciled with it), is when the function project needs a "Clean" and then an immediate "Start with Debug". Every time when I want to debug it, which again in my view has something to do with some nuget... Hopefully, this helps you... Commented Apr 24, 2018 at 14:15

3 Answers 3

1

Try removing the Id parameter from your attribute. When you specify an Id, the function will attempt to query for that document. In your case, you don't want that as you're trying to do an output binding.

[CosmosDB("Fagkveld", "ViktigeData", CreateIfNotExists = true, ConnectionStringSetting = "CosmosDbConn")]out dynamic document

Sign up to request clarification or add additional context in comments.

1 Comment

Are you seeing this in VS? Without sharing any secrets, what is your "CosmosDbConn" value in your local.settings.json file? Is it using the full Cosmos connection string (with AccountEndpoint and AccountKey)?
1

I'm not sure what solved this in the end. But I did as @Vladislav suggested and cleaned the solution. I also reinstalled all packages and re-checked all connection strings. And for some reason it's now working. Thanks for all the suggestions!

Comments

0

Please refer to the following code:

using System.Net.Http;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;

namespace JayGongCosmosDB
{
    public static class Function2
    {
        [FunctionName("Function1")]
        public static void Run([HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)]HttpRequestMessage req,
             [DocumentDB("db", "coll", Id = "id", ConnectionStringSetting = "myCosmosDB")] out dynamic document)
        {
            document = new { Id = "Identifier1", Title = "Some Title" };
        }
    }
}

Based on the cosmos db output c# example, you need to use DocumentDB not CosmosDB(should not pass the compilation).

Hope it helps you.

1 Comment

I'm using Azure Functions version 2.x so the attribute is renamed to CosmosDb.. The top "Note" on this page: learn.microsoft.com/en-us/azure/azure-functions/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.