使用 local.settings.json 的 Azure 函数 cosmos db 输出

Dan*_*ham 2 azure azure-functions azure-cosmosdb

查看有关 azure 函数的文档,特别是这个。如何通过门户设置集成非常清楚,但本地开发却非常模糊。

我的代码结构如下:

[FunctionName("foobar")]
public static void Run([QueueTrigger("foo")]Foo myQueueItem, out object dbFoo)
{
  //do cool stuff here
}
Run Code Online (Sandbox Code Playgroud)

队列触发器与 Azure 存储模拟器配合得很好,但没有有关如何设置 local.settings.json 的说明。该文件是通过 Visual Studio 自动生成的,如下所示:

{
   "IsEncrypted": false,
   "Values": {
      "AzureWebJobsStorage": "UseDevelopmentStorage=true",
      "AzureWebJobsDashboard": ""
   }
}
Run Code Online (Sandbox Code Playgroud)

cosmos db 的连接信息位于此结构中的哪个位置以使该函数能够正确运行?

Mik*_*kov 6

它应该看起来像这样:

public static void Run(
    [QueueTrigger("foo")] Foo myQueueItem, 
    [DocumentDB("MyDB", "MyCollection", ConnectionStringSetting = "MyConnectionString")]
    out object dbFoo)
Run Code Online (Sandbox Code Playgroud)

配置将是:

{
    "IsEncrypted": false,
    "Values": {
        "MyConnectionString": "...your cosmos db string..."
    }
}
Run Code Online (Sandbox Code Playgroud)

在 Azure 中,您必须将MyConnectionString参数放入应用程序设置中。

更新:在 V2 版本中,FunctionsDocumentDB绑定属性已被CosmosDB属性替换,请参阅文档