Isolated模式Durable Azure Function写入Cosmos DB遇400错误
排查Durable Isolated模式Azure Function写入Cosmos DB的400错误
问题场景
在Isolated模式下运行链式Durable Azure Function,尝试将文档写入Cosmos DB时触发BadRequest(400)错误。
相关代码与配置
Activity函数代码
[Function(nameof(ScanLinksAndInsertIntoLinksContainer))] [CosmosDBOutput(databaseName: "dpubot", containerName: "test", Connection= "CosmosConnection" , CreateIfNotExists = true, PartitionKey = "id")] public static MultiResponse ScanLinksAndInsertIntoLinksContainer ([ActivityTrigger] string name, FunctionContext executionContext) { var message = "Life is short"; return new MultiResponse() { Document = new MyDocument { id = System.Guid.NewGuid().ToString(), message = message }, }; }
错误日志
Executed 'Functions.ScanLinksAndInsertIntoLinksContainer' (Failed, Id=635107bb-afc7-46e6-ac6b-5e4454f07254, Duration=2040ms) [2023-07-17T08:26:59.443Z] System.Private.CoreLib: Exception while executing function: Functions.ScanLinksAndInsertIntoLinksContainer. Microsoft.Azure.Cosmos.Client: Response status code does not indicate success: BadRequest (400); Substatus: 0; ActivityId: 94096d64-1801-4193-a0a2-2a7723eac59f; Reason: (Message: {"Errors":["One of the specified inputs is invalid"]}
Host.json配置
"logging": { "applicationInsights": { "samplingSettings": { "isEnabled": true, "excludedTypes": "Request" } }, "extensions": { "cosmosDB": { "connectionMode": "Gateway", "userAgentSuffix": "MyDesiredUserAgentStamp" } } }
local.settings.json配置
"Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", "CosmosConnection": }
排查与解决步骤
1. 补全Cosmos DB连接字符串
local.settings.json中的CosmosConnection为空,必须填入有效的Cosmos DB账户连接字符串(可从Azure门户的Cosmos DB账户>密钥页面复制主/次连接字符串)。修正后的配置示例:
"Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", "CosmosConnection": "AccountEndpoint=https://your-cosmos-account.documents.azure.com:443/;AccountKey=your-account-key-value;" }
2. 修正PartitionKey参数格式
CosmosDBOutput属性的PartitionKey参数需要传入分区键路径(以斜杠开头),而非属性名。原代码中PartitionKey = "id"错误,应改为PartitionKey = "/id"。
3. 匹配输出绑定的返回类型
CosmosDBOutput绑定默认期望直接接收要写入的文档对象,若使用自定义MultiResponse类型,需确保绑定能正确识别要写入的文档。可通过两种方式调整:
- 直接返回
MyDocument实例(推荐):[Function(nameof(ScanLinksAndInsertIntoLinksContainer))] [CosmosDBOutput(databaseName: "dpubot", containerName: "test", Connection = "CosmosConnection", CreateIfNotExists = true, PartitionKey = "/id")] public static MyDocument ScanLinksAndInsertIntoLinksContainer( [ActivityTrigger] string name, FunctionContext executionContext) { var message = "Life is short"; return new MyDocument { id = System.Guid.NewGuid().ToString(), message = message }; } - 或使用输出参数传递文档:
[Function(nameof(ScanLinksAndInsertIntoLinksContainer))] public static MultiResponse ScanLinksAndInsertIntoLinksContainer( [ActivityTrigger] string name, [CosmosDBOutput(databaseName: "dpubot", containerName: "test", Connection = "CosmosConnection", CreateIfNotExists = true, PartitionKey = "/id")] out MyDocument outputDoc, FunctionContext executionContext) { var message = "Life is short"; outputDoc = new MyDocument { id = System.Guid.NewGuid().ToString(), message = message }; return new MultiResponse { Document = outputDoc }; }
4. 清理错误创建的容器(若存在)
由于原代码设置了CreateIfNotExists = true,错误的PartitionKey配置可能已创建了不符合预期的容器。需手动删除Cosmos DB中的dpubot数据库下的test容器,让函数重新创建正确配置的容器。
内容的提问来源于stack exchange,提问作者redar ismail
相关产品推荐
相关产品推荐

