You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function通过门户配置CosmosDB输出绑定运行报错的问题求助

Azure Function通过门户配置CosmosDB输出绑定运行报错的问题求助

各位大佬好,我最近遇到了一个Azure Function配置CosmosDB输出绑定的棘手问题,折腾了半天没找到头绪,想请教下大家:

我通过Azure门户创建了一个Http触发的Azure Function,核心需求是向CosmosDB插入文档。配置过程中全程用的默认选项,CosmosDB的输出绑定也是通过门户的集成页面自动生成的,完全没手动修改过绑定参数。

我的Function代码如下:

#r "Newtonsoft.Json"

using System.Net;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json;
using System;
using System.IO;

public static IActionResult Run(HttpRequest req, out dynamic outputDocument, ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = new StreamReader(req.Body).ReadToEnd();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    outputDocument = new { Description = name, id = Guid.NewGuid() };

    string responseMessage = string.IsNullOrEmpty(name)
        ? "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response."
        : $"Hello, {name}. This HTTP triggered function executed successfully.";

    return new OkObjectResult(responseMessage);
}

门户自动生成的function.json内容是这样的:

{
  "bindings": [
    {
      "authLevel": "function",
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    },
    {
      "name": "outputDocument",
      "databaseName": "outDatabase",
      "collectionName": "MyCollection",
      "createIfNotExists": true,
      "connectionStringSetting": "johncosmosgorter_DOCUMENTDB",
      "partitionKey": "id",
      "direction": "out",
      "type": "cosmosDB"
    }
  ]
}

现在运行函数直接报500错误,日志里的错误信息是:

Cosmos DB connection configuration 'CosmosDB' does not exist. Make sure that it is a defined App Setting.

这就很费解了——毕竟绑定参数都是门户自动生成的,我根本没手动改过。我怀疑是不是Azure Functions v3和v4的绑定参数不兼容?于是试着把connectionStringSetting改成connection,结果错误变了,但还是不对:

Value cannot be null. (Parameter 'databaseId')

明明绑定里已经写了databaseName,我试着改成databaseId也没用。

想请教下大家:为什么这么基础的CosmosDB输出绑定样板代码会出问题?为什么现在找正确的配置指导这么难?麻烦各位大佬给点思路,谢谢了!

备注:内容来源于stack exchange,提问作者John Gorter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.21 12:54:33