0

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?

starscream
  • 741
  • 2
  • 11
  • 23
  • what was the error message – HariHaran Jun 19 '19 at 04:21
  • I don't think `BackendConfiguration.DB_CONNECTION_STRING` is supported for Function binding attributes, have you tried with a string and see if it works? – Matias Quaranta Jun 19 '19 at 15:34
  • @MatiasQuaranta you are right, there are some issues with my solution. I removed the connection string from my class and placed that into App Function's configuration. Thank you for the hint! – starscream Jun 22 '19 at 12:16

1 Answers1

3

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.

silent
  • 14,494
  • 4
  • 46
  • 86
  • I know that I should not aswer further questions, but I there a way to return an OkObjectResult once the insert is completed? – starscream Jun 22 '19 at 12:18
  • 1
    no, when you use the binding the actual writing into the database is abstracted away from you. When you need control (or the response) about this, you need to use the DocumentClient: https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-cosmosdb-v2#http-trigger-get-multiple-docs-using-documentclient-c – silent Jun 24 '19 at 07:09