The Azure Code Samples are currently available in English
Consuming the Azure Cosmos DB Change Feed with Change Feed Processor
- An active Azure Cosmos account or the Azure Cosmos DB Emulator - If you don't have an account, refer to the Create a database account article.
- NET Core SDK or Visual Studio 2017 (or higher)
This sample will create two containers, using the names defined in appSettings.json (
LeasesContainerName). One of them will be used to store state (
LeasesContainerName), and the other will be the container that we'll be listening for changes on (
The code creates an instance of a Change Feed Processor and defines a delegate that will process the changes:
ChangeFeedProcessor changeFeedProcessor = cosmosClient.GetContainer(databaseName, sourceContainerName) .GetChangeFeedProcessorBuilder<ToDoItem>("<name-for-the-workflow>", HandleChangesAsync) .WithInstanceName("<name-for-the-host-instance>") .WithLeaseContainer(leaseContainer) .Build();
HandleChangesAsync is a delegate with a signature of:
async Task HandleChangesAsync(IReadOnlyCollection<ToDoItem> changes, CancellationToken cancellationToken)
The Change Feed Processor works as a push model. Whenever there are new changes in
SourceContainerName, the HandleChangesAsync delegate will be called and its code will be able to process the list of changes. As more changes keep happening, new invocations will occur.
Running this sample
- Clone this repository or download the zip file.
- Retrieve the Connection String value from the Keys blade of your Azure Cosmos account in the Azure portal. For more information on obtaining the Connection String for your Azure Cosmos account refer to View, copy, and regenerate access keys and passwords.
- If you are working with the Azure Cosmos DB Emulator, refer to Develop locally with the Azure Cosmos Emulator.
- In the appSettings.json file, located in the project root, find ConnectionString and replace the placeholder value with the value obtained for your account.
dotnet runor press F5 from within Visual Studio.
- Type an amount of random records to be generated in
SourceContainerName(for example, 10), and press ENTER. The sample will write those records in the container.
HandleChangesAsyncdelegate will get called and handle those changes as they are inserted, independently.
- Repeat step 5 with different numbers or type
About the code
The code included in this sample is intended to get you going with consuming the Change Feed and react to changes. It is not intended to be a set of best practices on how to build scalable enterprise grade applications. This is beyond the scope of this quick start sample.