Connect Azure Functions to Azure Cosmos DB using Visual Studio Code

Angelo Peca 75 Reputation points
2023-05-17T02:16:26.3366667+00:00

Hello,

I'm trying to connect an Azure function to an Azure CosmosDB instance and I'm following this guide: https://learn.microsoft.com/en-us/azure/azure-functions/functions-add-output-binding-cosmos-db-vs-code?pivots=programming-language-csharp&tabs=in-process%2Cv1

When I hit F5 to try and run the code on my local machine though I get this error:

C:\Users\XXX\code\AzureFunctionTest\HttpExample.cs(22,17): error CS0246: The type or namespace name 'ConnectionStringSetting' could not be found (are you missing a using directive or an assembly reference?) [C:\Users\XXX\code\AzureFunctionTest\AzureFunctionTest.csproj]
C:\Users\XXX\code\AzureFunctionTest\HttpExample.cs(21,17): error CS1739: The best overload for 'CosmosDBAttribute' does not have a parameter named 'collectionName' [C:\Users\XXX\code\AzureFunctionTest\AzureFunctionTest.csproj]

This is what the AzureFunctionTest.csproj file looks like:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <AzureFunctionsVersion>v4</AzureFunctionsVersion>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="4.2.0" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.1" />
  </ItemGroup>
  <ItemGroup>
    <None Update="host.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="local.settings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>Never</CopyToPublishDirectory>
    </None>
  </ItemGroup>
</Project>

Any help with that error would be appreciated, thanks.

Angelo

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,901 questions
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2023-05-17T19:18:33.24+00:00

    Angelo Peca Thanks for posting your question in Microsoft Q&A. Based on your project reference, you are using version 4.2.0 of Microsoft.Azure.WebJobs.Extensions.CosmosDB. However, the attributes collectionName, ConnectionStringSetting you mentioned are not available in this version and only available in older version (v3).

    Here is doc reference Attributes for Functions 2.x+

    User's image

    Newer version: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2-output?pivots=programming-language-csharp&tabs=python-v2%2Cin-process%2Cextensionv4#attributes
    (Refer CosmosDBTriggerAttribute.cs)

    User's image

    You can refer code snippet Queue trigger, write one doc (v4 extension) and make the changes accordingly. Sorry for the confusion and please feel free to submit the feedback to doc team via This page at the bottom of the doc.

    User's image

    I hope this helps and let me know if you have any questions.


1 additional answer

Sort by: Most helpful
  1. AirGordon 7,150 Reputation points
    2023-05-17T06:29:33.56+00:00

    In the VsCode terminal, run this command to restore any package dependencies;

    dotnet restore
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.