locked
Cosmo DB With C# Function In Visual Studio RRS feed

  • Question

  • Hello, I was excited to see that support for Functions was added to Cosmo DB.  The example (https://docs.microsoft.com/en-us/azure/azure-functions/functions-create-cosmos-db-triggered-function) shows everything done within the editor on the Azure Portal.  I was hoping to be able to build this in C# within Visual Studio.  Does anyone know if this is possible?  It doesn't appear in the list of available options when you create the function in VS.

    Thanks.

    Thursday, October 5, 2017 6:52 PM

Answers

  • Deleted
    • Marked as answer by Ryan_Ha Thursday, October 5, 2017 8:03 PM
    Thursday, October 5, 2017 7:57 PM
  • An update to add cosmos db trigger to the template list is in process. Unfortunately we don't have the exact ETA.

    Here are the steps to create a cosmos db trigger vs VS in the meantime

    1. Create a new Function App in visual studio
    2. update the Microsoft.NET.Sdk.Functions to 1.0.5
    3. Install Microsoft.Azure.WebJobs.Extensions.DocumentDB, version 1.1.0-beta4
    4. Create a new class, replace the contents with the following code.
    using System.Collections.Generic;
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    
    namespace YourNameSpace
    {
        public static class CosmosDBTriggerCSharp
        {
            [FunctionName("CosmosDBTriggerCSharp")]
            public static void Run([CosmosDBTrigger(
                databaseName: "DatabaseValue",
                collectionName: "CollectionValue",
                ConnectionStringSetting = "ConnectionValue",
                LeaseCollectionName = "leases")]IReadOnlyList<Document> input, TraceWriter log)
            {
                if (input != null && input.Count > 0)
                {
                    log.Verbose("Documents modified " + input.Count);
                    log.Verbose("First document Id " + input[0].Id);
                }
            }
        }
    }




    • Edited by Naren Soni Thursday, October 5, 2017 8:18 PM
    • Proposed as answer by Naren Soni Thursday, October 5, 2017 8:18 PM
    • Marked as answer by Ryan_Ha Thursday, October 5, 2017 8:20 PM
    Thursday, October 5, 2017 8:17 PM

All replies

  • Deleted
    Thursday, October 5, 2017 7:01 PM
  • Thanks Andres.

    This is more of a newbe question but do I need to create an attribute file from: https://github.com/Azure/azure-webjobs-sdk-extensions/blob/master/src/WebJobs.Extensions.DocumentDB/Trigger/CosmosDBTriggerAttribute.cs

    in my project?

    Thanks.

    Thursday, October 5, 2017 7:21 PM
  • Deleted
    • Marked as answer by Ryan_Ha Thursday, October 5, 2017 8:03 PM
    Thursday, October 5, 2017 7:57 PM
  • Awesome, thanks Andres, it wasn't finding that when I was compiling it but I think it was because I wasn't on the pre-release nuget package.  I think it's working now.  

    Thanks again for your help.

    Thursday, October 5, 2017 8:03 PM
  • An update to add cosmos db trigger to the template list is in process. Unfortunately we don't have the exact ETA.

    Here are the steps to create a cosmos db trigger vs VS in the meantime

    1. Create a new Function App in visual studio
    2. update the Microsoft.NET.Sdk.Functions to 1.0.5
    3. Install Microsoft.Azure.WebJobs.Extensions.DocumentDB, version 1.1.0-beta4
    4. Create a new class, replace the contents with the following code.
    using System.Collections.Generic;
    using Microsoft.Azure.Documents;
    using Microsoft.Azure.WebJobs;
    using Microsoft.Azure.WebJobs.Host;
    
    namespace YourNameSpace
    {
        public static class CosmosDBTriggerCSharp
        {
            [FunctionName("CosmosDBTriggerCSharp")]
            public static void Run([CosmosDBTrigger(
                databaseName: "DatabaseValue",
                collectionName: "CollectionValue",
                ConnectionStringSetting = "ConnectionValue",
                LeaseCollectionName = "leases")]IReadOnlyList<Document> input, TraceWriter log)
            {
                if (input != null && input.Count > 0)
                {
                    log.Verbose("Documents modified " + input.Count);
                    log.Verbose("First document Id " + input[0].Id);
                }
            }
        }
    }




    • Edited by Naren Soni Thursday, October 5, 2017 8:18 PM
    • Proposed as answer by Naren Soni Thursday, October 5, 2017 8:18 PM
    • Marked as answer by Ryan_Ha Thursday, October 5, 2017 8:20 PM
    Thursday, October 5, 2017 8:17 PM
  • Thank you so much, this is really helpful.
    Thursday, October 5, 2017 8:21 PM
  • I am not able to use LeaseOption. While overriding value it show error as follows, "LeaseOption" not a attributetype.
    Wednesday, November 29, 2017 6:35 AM