Azure Cosmos DB trigger and bindings for Azure Functions 2.x and higher overview
Članak
This set of articles explains how to work with Azure Cosmos DB bindings in Azure Functions 2.x and higher. Azure Functions supports trigger, input, and output bindings for Azure Cosmos DB.
Action
Type
Run a function when an Azure Cosmos DB document is created or modified
This binding was originally named DocumentDB. In Azure Functions version 2.x and higher, the trigger, bindings, and package are all named Azure Cosmos DB.
In a variation of this model, Functions can be run using C# scripting, which is supported primarily for C# portal editing. To update existing binding extensions for C# script apps running in the portal without having to republish your function app, see Update your extensions.
The process for installing the extension varies depending on the extension version:
This version also changes the types that you can bind to, replacing the types from the v2 SDK Microsoft.Azure.DocumentDB with newer types from the v3 SDK Microsoft.Azure.Cosmos. Learn more about how these new types are different and how to migrate to them from the SDK migration guide, trigger, input binding, and output binding examples.
This extension version is available as a NuGet package, version 4.x.
Add the extension to your project by installing the NuGet package, version 4.x.
If you're writing your application using F#, you must also configure this extension as part of the app's startup configuration. In the call to ConfigureFunctionsWorkerDefaults() or ConfigureFunctionsWebApplication(), add a delegate that takes an IFunctionsWorkerApplication parameter. Then within the body of that delegate, call ConfigureCosmosDBExtension() on the object:
F#
let hostBuilder = new HostBuilder()
hostBuilder.ConfigureFunctionsWorkerDefaults(fun (context: HostBuilderContext) (appBuilder: IFunctionsWorkerApplicationBuilder) ->
appBuilder.ConfigureCosmosDBExtension() |> ignore
) |> ignore
Add the extension to your project by installing the NuGet package, version 3.x.
Install bundle
The Azure Cosmos DB bindings extension is part of an extension bundle, which is specified in your host.json project file. You may need to modify this bundle to change the version of the binding, or if bundles aren't already installed. To learn more, see extension bundle.
Because of schema changes in the Azure Cosmos DB SDK, version 4.x of the Azure Cosmos DB extension requires azure-functions-java-library V3.0.0 for Java functions.
The Azure Cosmos DB extension supports parameter types according to the table below.
Binding scenario
Parameter types
Cosmos DB trigger (single document)
JSON serializable types1
Cosmos DB trigger (batch of documents)
IEnumerable<T>where T is a JSON serializable type1
Cosmos DB input (single document)
JSON serializable types1
Cosmos DB input (query returning multiple documents)
CosmosClient IEnumerable<T> where T is a JSON serializable type1
Cosmos DB output (single document)
JSON serializable types1
Cosmos DB output (multiple documents)
ICollector<T> or IAsyncCollector<T> where T is a JSON serializable type1
1 Documents containing JSON data can be deserialized into known plain-old CLR object (POCO) types.
Earlier versions of the extension exposed types from the now deprecated Microsoft.Azure.Documents namespace. Newer types from Microsoft.Azure.Cosmos are exclusive to extension 4.x and higher.
The isolated worker process supports parameter types according to the tables below. Support for binding to types from Microsoft.Azure.Cosmosis in preview.
Cosmos DB trigger
When you want the function to process a single document, the Cosmos DB trigger can bind to the following types:
Type
Description
JSON serializable types
Functions tries to deserialize the JSON data of the document from the Cosmos DB change feed into a plain-old CLR object (POCO) type.
When you want the function to process a batch of documents, the Cosmos DB trigger can bind to the following types:
Type
Description
IEnumerable<T>where T is a JSON serializable type
An enumeration of entities included in the batch. Each entry represents one document from the Cosmos DB change feed.
Cosmos DB input binding
When you want the function to process a single document, the Cosmos DB input binding can bind to the following types:
Type
Description
JSON serializable types
Functions attempts to deserialize the JSON data of the document into a plain-old CLR object (POCO) type.
When you want the function to process multiple documents from a query, the Cosmos DB input binding can bind to the following types:
Type
Description
IEnumerable<T>where T is a JSON serializable type
An enumeration of entities returned by the query. Each entry represents one document.
When you want the function to write to a single document, the Cosmos DB output binding can bind to the following types:
Type
Description
JSON serializable types
An object representing the JSON content of a document. Functions attempts to serialize a plain-old CLR object (POCO) type into JSON data.
When you want the function to write to multiple documents, the Cosmos DB output binding can bind to the following types:
Type
Description
T[] where T is JSON serializable type
An array containing multiple documents. Each entry represents one document.
For other output scenarios, create and use a CosmosClient with other types from Microsoft.Azure.Cosmos directly. See Register Azure clients for an example of using dependency injection to create a client type from the Azure SDK.
Earlier versions of extensions in the isolated worker process only support binding to JSON serializable types. Additional options are available to extension 4.x and higher.
This section describes the configuration settings available for this binding in versions 2.x and higher. Settings in the host.json file apply to all functions in a function app instance. The example host.json file below contains only the version 2.x+ settings for this binding. For more information about function app configuration settings in versions 2.x and later versions, see host.json reference for Azure Functions.
The connection mode used by the function when connecting to the Azure Cosmos DB service. Options are Direct and Gateway
userAgentSuffix
n/a
Adds the specified string value to all requests made by the trigger or binding to the service. This makes it easier for you to track the activity in Azure Monitor, based on a specific function app and filtering by User Agent.
Pridružite se seriji susreta kako biste s kolegama programerima i stručnjacima izgradili skalabilna rješenja umjetne inteligencije temeljena na stvarnim slučajevima upotrebe.