1

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.

selbu
  • 106
  • 1
  • 8

1 Answers1

1

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

Tom Sun - MSFT
  • 24,161
  • 3
  • 30
  • 47
  • This is not working. The reference #r "Microsoft.Azure.Documents.Client" gives med the error: `Compilation service error [03/07/2018 09:39:25] Microsoft.Azure.WebJobs.Script: C# compilation service error: Could not load file or assembly 'Microsoft.Azure.Documents.Client, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. [03/07/2018 09:39:25] . System.Private.CoreLib: Could not load file or assembly 'Microsoft.Azure.Documents.Client, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.` – selbu Mar 07 '18 at 09:41
  • Still the same error. I am running this locally, do I need to run a install command or similar? – selbu Mar 07 '18 at 10:03
  • If it is run in locally, it is different from azure portal. It is a precompiled azure function. You need to install [Microsoft.Azure.DocumentDB](https://www.nuget.org/packages/Microsoft.Azure.DocumentDB/) to your function – Tom Sun - MSFT Mar 07 '18 at 10:14
  • I tried running `nuget install Microsoft.Azure.DocumentDB` inside the function folder. This downloaded the package, but my function still returns the same error. `The type or namespace name 'Documents' does not exist in the namespace 'Microsoft.Azure' (are you missing an assembly reference?)` – selbu Mar 07 '18 at 10:18
  • If there is a snapshot will be more helpful. – Tom Sun - MSFT Mar 07 '18 at 10:24
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/166386/discussion-between-lundmarkus-and-tom-sun). – selbu Mar 07 '18 at 10:25