none
Azure Functions and Cosmos DB RRS feed

  • Question

  • Hello

    I am trying to create an Azure function in VS code. I need to return a list of documents in a Cosmos DB. However I am getting this error in Azure portal after publish, and the function does not work. 

    
    Error:
    
    Function (GetDocumentList) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'GetDocumentList'. Microsoft.Azure.WebJobs.Host: Unable to resolve the value for property 'CosmosDBAttribute.ConnectionStringSetting'. Make sure the setting exists and has a valid value.
    This is my code:
    using System;
    using System.IO;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Extensions.Http;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.Logging;
    using Newtonsoft.Json;
    using System.Net;
    using System.Collections.Generic;
    using Microsoft.Azure.WebJobs.Extensions.CosmosDB;
    
    namespace DocumentHub
    {
        public static class GetDocumentList
        {
            [FunctionName("GetDocumentList")]
            public static IActionResult Run(
                [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
                 [CosmosDB(
                        databaseName: "mytestdb",
                        collectionName: "Customer1",
                        ConnectionStringSetting = "MyCosmosDBConnection",
                        SqlQuery = "SELECT * FROM c")] IEnumerable<dynamic> documents,
                ILogger log
               )
            {
                return (ActionResult)new OkObjectResult(documents);
            }
        }
    }

    I did add MyCoscosDBConnection to local.settings.json. 

    I have included the following in my .csproj:

    <PackageReference Include="Microsoft.Azure.WebJobs.Extensions.CosmosDB" Version="3.0.3" />
    <PackageReference Include="Microsoft.NET.Sdk.Functions" Version="1.0.27" />

    This should be pretty simple, but I seems to be missing something. 

    Any good ideas?

    I did try in the portal and here it works perfect. 


    • Edited by MoHaJe Wednesday, May 15, 2019 12:45 PM
    Wednesday, May 15, 2019 12:43 PM

Answers

  • Did you add this Configuration in Azure function?

    Go to your Azure function

    Click in Application Settings

    Add a new Application Settings (Name as "CosmosDBAttribute.ConnectionStringSetting")

    • Marked as answer by MoHaJe Wednesday, May 15, 2019 1:08 PM
    Wednesday, May 15, 2019 12:53 PM

All replies

  • Did you add this Configuration in Azure function?

    Go to your Azure function

    Click in Application Settings

    Add a new Application Settings (Name as "CosmosDBAttribute.ConnectionStringSetting")

    • Marked as answer by MoHaJe Wednesday, May 15, 2019 1:08 PM
    Wednesday, May 15, 2019 12:53 PM
  • So simple :)

    Thanks. 

    Wednesday, May 15, 2019 1:08 PM
  • Shouldn't  the application setting MyCosmosDBConnection map for CosmosDBAttribute.ConnectionStringSetting?

    If I create the function from the Portal a new app setting with the connection is made and mapped int function.json. 

    However if I'm deploying the function with arm template and code from build artifact from devops release pipeline I get same error described here. I added the appsetting CosmosDBAttribute.ConnectionStringSetting besides Product:CosmosDB:ConnectionString but still doesn't work.

    Here is the function.json that we get on portal.

    {
    	"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.26",
    	"configurationSource": "attributes",
    	"bindings": [
    		{
    			"type": "cosmosDBTrigger",
    			"connectionStringSetting": "Product:CosmosDB:ConnectionString",
    			"leaseCollectionName": "audit-leases",
    			"createLeaseCollectionIfNotExists": true,
    			"collectionName": "events",
    			"databaseName": "db-shared",
    			"leaseDatabaseName": "db-shared",
    			"startFromBeginning": false,
    			"name": "input"
    		}
    	],
    	"disabled": false,
    	"scriptFile": "../bin/Product.Services.AuditGeneration.dll",
    	"entryPoint": "Product.Services.AuditGeneration.GenerateAudit.GenerateAuditFromEvent"
    }


    Wednesday, July 17, 2019 11:06 AM