1
votes

I want to query a CosmosDB collection to see if a document already exists from within a Azure Function using csx.

As well as the following code I have an implicit binding towards the cosmosDB collection to be able to create new documents. This is done using

binder.BindAsync<IAsyncCollector<string>>(new CosmosDBAttribute("test", "collection")

This a simple version of my function.

#r "System"
#r "Microsoft.Azure.WebJobs.Extensions.CosmosDB"

using System;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Client;

public static async Task<string> Run(string localityId, Binder binder, TraceWriter log)
{
    ...

    string EndpointUrl = "<your endpoint URL>";
    string PrimaryKey = "<your primary key>";
    DocumentClient client = new DocumentClient(new Uri(EndpointUrl), PrimaryKey);

    ...
}

This results in the following error message:

error CS0246: The type or namespace name 'DocumentClient' could not be found (are you missing a using directive or an assembly reference?)

I have installed the extension Microsoft.Azure.WebJobs.Extensions.CosmosDB

I am running on MacOS using the func host start command to test locally.

1

1 Answers

1
votes

error CS0246: The type or namespace name 'DocumentClient' could not be found (are you missing a using directive or an assembly reference?)

It seems that you need to reference #r "Microsoft.Azure.Documents.Client". You also could get demo code from Azure Cosmos DB bindings for Azure Functions

 #r "Microsoft.Azure.Documents.Client"

    using System;
    using Microsoft.Azure.Documents;
    using System.Collections.Generic;


    public static void Run(IReadOnlyList<Document> documents, TraceWriter log)
    {
      log.Verbose("Documents modified " + documents.Count);
      log.Verbose("First document Id " + documents[0].Id);
    }

Update:

To use NuGet packages in a C# function, upload a project.json file to the function's folder in the function app's file system. Here is an example project.json file that adds a reference

enter image description here