starscream
starscream

Reputation: 741

Azure App Function (Functions 2.x): Create new document in CosmosDB via HTTP function

I'm trying to implement an Azure HTTP function which gets some JSON data and creates a new object in my CosmoDB database.

I've read the following questions, on Stackoverflow:

  1. Azure function C#: Create or replace document in cosmos db on HTTP request
  2. Azure function inserting but not updating cosmosDB

But they're using Function 1.x version, therefore I searched for some guidelines on Microsoft side and found the following:

  1. Output - examples

Based on this article, I've writtem my C# class in Visual Studio Community and I want to publish it on my Azure App Function resource:

using System;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Azure.WebJobs.Extensions.CosmosDB;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using BackendFunctions.Utils;

namespace BackendFunctions.Http.Business
{
    public static class A_Dummy_Function
    {
        [FunctionName("A_Dummy_Function")]
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest request,
            [CosmosDB(
                databaseName: "DB-NAME-VALUE",
                collectionName: "A-COLLECTION",
                ConnectionStringSetting = BackendConfiguration.DB_CONNECTION_STRING)] out dynamic document,
            ILogger log)
        {

            document = new { Description = "BLA BLA", id = Guid.NewGuid() };

            ActionResult toReturn = (ActionResult) new OkObjectResult($"Hello world, this creates a new object!");

            return toReturn;
        }
    }
}

As you can see CosmosDB connection (binding) is managed by Function 2.x version (indeed I installed Microsoft.Azure.WebJobs.Extensions.CosmosDB NuGet package), and there is the following parameter in the function:

[CosmosDB(
     databaseName: "DB-NAME-VALUE",
     collectionName: "A-COLLECTION",
     ConnectionStringSetting = BackendConfiguration.DB_CONNECTION_STRING)] out dynamic document

When I try to publish the function on Azure App Function resource, I get an error.

It seems that it is not possible to convert my C# class into function.json.

Do you have any suggestion on why I cannot publish such a function remotely?

Upvotes: 0

Views: 212

Answers (1)

silent
silent

Reputation: 16138

I would rather use the IAsyncCollector instead of out. See the example from here.

    [FunctionName("WriteDocsIAsyncCollector")]
    public static async Task Run(
        [QueueTrigger("todoqueueforwritemulti")] ToDoItem[] toDoItemsIn,
        [CosmosDB(
            databaseName: "ToDoItems",
            collectionName: "Items",
            ConnectionStringSetting = "CosmosDBConnection")]
            IAsyncCollector<ToDoItem> toDoItemsOut,
        ILogger log)
    {
        log.LogInformation($"C# Queue trigger function processed {toDoItemsIn?.Length} items");

        foreach (ToDoItem toDoItem in toDoItemsIn)
        {
            log.LogInformation($"Description={toDoItem.Description}");
            await toDoItemsOut.AddAsync(toDoItem);
        }
    }

Just exchange QueueTrigger with your HttpTrigger.

Upvotes: 3

Related Questions