This post has been republished via RSS; it originally appeared at: Microsoft Developer Blogs - Feed.
Creating event sourcing solutions with Azure Cosmos DB is easy with Azure Functions Triggers, where you can leverage the Change Feed Processor's powerful scaling and reliable event detection functionality, without the need to maintain any worker infrastructure. You can just focus on your Azure Function's logic without worrying about the rest of the event-sourcing pipeline. In this blog, we have some quick how-to videos to get you up and running with .NET Azure Cosmos DB Functions Triggers! If you want to follow along, there are some pre-requisites you should have in place:- Ideally a Windows 10 Machine.
- Access to an Azure subscription, and an Azure Cosmos DB account - instructions here.
- .NET Core installed - instructions here.
- Visual Studio Code installed - instructions here.
- Azure Storage Emulator installed (make sure this is running before trying to follow the demo) - instructions here.
- Azure Functions Core Tools installed - instructions here (we recommend v3.x - note that this requires installing Node.js).
- Azure Functions Extension installed - you should have Azure Functions Core Tools and VS code already installed, and access to an Azure subscription for sign-in, before attempting to install this extension) - instructions here.
using System; using System.Threading.Tasks; using System.Collections.Generic; using Microsoft.Azure.Documents; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Host; using Microsoft.Extensions.Logging; using Microsoft.Azure.Cosmos; namespace Company.Function { public static class CosmosDBTriggerCSharp1 { private static readonly string _endpointUrl = System.Environment.GetEnvironmentVariable("endpointUrl"); private static readonly string _primaryKey = System.Environment.GetEnvironmentVariable("primaryKey"); private static readonly string _databaseId = "database"; private static readonly string _containerId = "collection2"; private static CosmosClient cosmosClient = new CosmosClient(_endpointUrl, _primaryKey); [FunctionName("CosmosDBTriggerCSharp1")] public static async Task Run([CosmosDBTrigger( databaseName: "database", collectionName: "collection1", ConnectionStringSetting = "cosmosdbtvk_DOCUMENTDB", LeaseCollectionName = "leases", CreateLeaseCollectionIfNotExists = true)]IReadOnlyList<Document> input, ILogger log) { var container2 = cosmosClient.GetContainer(_databaseId, _containerId); foreach(Document doc in input){ log.LogInformation("pushed doc into container 2"); log.LogInformation("doc: "+doc); try{ await container2.CreateItemAsync<Document>(doc); } catch (Exception e) { log.LogInformation("Exception pushing doc into container 2: "+e); } } } } }Make sure you install the latest Azure Cosmos DB .NET SDK. In terminal, run the following:
dotnet add package Microsoft.Azure.CosmosYour local.settings.json should also be updated to look something like this (note the "endpointUrl" and "primaryKey" added for the target collection):
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet", "cosmosdbtvk_DOCUMENTDB": "<PRIMARY CONNECTION STRING OF SOURCE COLLECTION>", "endpointUrl":"<URI OF TARGET COLLECTION>", "primaryKey": "<PRIMARY KEY OF TARGET COLLECTION>" } }Watch the video below to see how to deploy to Azure! To follow along with this video, you should have the pre-requisites from the above video already installed, plus the following:
- Azure CLI - instructions here.
- The Azure Cosmos DB Core (SQL) API .NET SDK at version 3.6.0 or higher
- NuGet Package Manager for VS code - instructions here (you would need this to install the latest version of the Azure Cosmos DB .NET SDK - this is illustrated but not installed during the video).