CosmosDB HttpTrigger VS Code Azure Function Could not load assembly

1

Trying to create an HttpTrigger v2 Azure Function with CosmosDB bidning that will look up ID from route data using SqlQuery. This is very similar to example provided by Microsoft here. I am developing this function using VS Code. Here is the code:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

namespace some_namespace
{
    public static class GetUser
    {
        [FunctionName("GetUser")]
        public static IActionResult Run(
            [HttpTrigger(
                AuthorizationLevel.Anonymous, 
                "get", 
                Route = "user/{id}")] HttpRequest req,
            [CosmosDB(
                "DbName", 
                "Users",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from Users u where u.id = {id}")]
                IEnumerable<User> users,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            foreach (User user in users)
            {
                log.LogInformation(user.id);
            }
            return new OkResult();
        }
    }
}

User class is defined in another file. All compiles fine and I can upload it to Azure but when I navigate to the function, I get the following error:

Function (.../GetUser) Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'GetUser'. System.Private.CoreLib: Could not load file or assembly 'Microsoft.Azure.WebJobs.Extensions.CosmosDB, Version=3.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. Could not find or load a specific file. (Exception from HRESULT: 0x80131621). System.Private.CoreLib: Could not load file or assembly 'Microsoft.Azure.WebJobs.Extensions.CosmosDB, Version=3.0.4.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

I have modified my host.json file to include ExtensionBundle as per this article and it currently looks like this:

{
    "version": "2.0",
    "extensionBundle": {
        "id": "Microsoft.Azure.Functions.ExtensionBundle",
        "version": "[1.*, 2.0.0)"
    }
}

The above was supposed to automagically include the reference to CosmosDB extension with my function but it seems that it did not or I am missing something else but don't know what.

visual-studio-code
azure-functions
asked on Stack Overflow Sep 12, 2019 by Igor

2 Answers

0

Try installing Microsoft.Azure.WebJobs.Extensions.CosmosDB as a NuGet package instead.

The ExtensionBundle seems to be for local development.

answered on Stack Overflow Sep 12, 2019 by Paul Lorica
0

I am not sure what fixed the above function to make it work. At one point in time, I have added a redundant import for using Microsoft.Azure.WebJobs.Extensions.CosmosDB but I have subsequently removed it and the function still works. The above code will not return found objects - below is a modified, working function (logging of each returned user is not required but I left it there for debugging purposes):

using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;

namespace some_namespace
{
    public static class GetUser
    {
        [FunctionName("GetUser")]
        public static IActionResult Run(
            [HttpTrigger(
                AuthorizationLevel.Anonymous, 
                "get", 
                Route = "user/{id}")] HttpRequest req,
            [CosmosDB(
                "DbName", 
                "Users",
                ConnectionStringSetting = "CosmosDBConnection",
                SqlQuery = "select * from Users u where u.id = {id}")]
                IEnumerable<User> users,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            foreach (User user in users)
            {
                log.LogInformation(user.id);
            }
            return new OkObjectResult(users);
        }
    }
}
answered on Stack Overflow Sep 13, 2019 by Igor

User contributions licensed under CC BY-SA 3.0