azure - Azure 函数参数中的 DocumentClient 绑定(bind)

标签 azure f# azure-functions azure-webjobssdk azure-cosmosdb

我正在 F# 中编写一个 http 触发器 Azure 函数,我想绑定(bind)一个 DocumentClient 类型的参数,以便更好地控制在 Cosmos DB 中执行的查询。这是我到目前为止所拥有的:

函数.fs

namespace Functions

open System
open System.Net
open System.Net.Http
open Newtonsoft.Json
open Microsoft.Azure.Documents
open Microsoft.Azure.Documents.Client
open Microsoft.Azure.WebJobs
open Microsoft.Azure.WebJobs.Host
open Microsoft.Azure.WebJobs.Extensions
open Microsoft.Azure.WebJobs.Extensions.DocumentDB

module Function = 
  let Run(req: HttpRequestMessage, [<DocumentDB>] client: DocumentClient, log: TraceWriter) =
    log.Info(sprintf "F# HTTP trigger function processed a request.")
    req.CreateResponse(HttpStatusCode.OK)

函数.json

{
  "disabled": false,
  "scriptFile": "..\\..\\..\\build\\Debug\\Functions\\Functions.dll",
  "entryPoint": "Functions.Function.Run",
  "bindings": [
    {
      "direction": "in",
      "type": "httpTrigger",
      "authLevel": "anonymous",
      "name": "req",
      "methods": [ "get" ],
      "route": "users"
    },
    {
      "direction": "in",
      "type": "documentDB",
      "name": "client",
      "connection": "COSMOSDB_CONNECTION_STRING"
    },
    {
      "direction": "out",
      "type": "http",
      "name": "res"
    }
  ]
}

主机.json

{
  "frameworks": {
    "net46":{
      "dependencies": {
        "Dynamitey": "1.0.2",
        "FSharp.Interop.Dynamic": "3.0.0",
        "Microsoft.Azure.DocumentDB": "1.17.0",
        "Microsoft.Azure.WebJobs.Extensions.DocumentDB": "1.0.0"
      }
    }
  }
}

本地.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsDashboard": "",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "COSMOSDB_CONNECTION_STRING": "AccountEndpoint=https://localhost:8081;AccountKey=C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==;"
  }
}

我正在使用开发存储和 Cosmos DB 模拟器在本地运行该函数。我尝试将其描述的内容翻译为 F# here对于 C#。而且它也与提到的here几乎相同。 。但我只收到错误 Microsoft.Azure.WebJobs.Extensions.DocumentDB:绑定(bind)到 DocumentClient 属性时需要“Id”

最佳答案

this issue on Github 中提到了一种解决方法。它已修复在tooling中并将在下一个版本中提供

从 github 复制的解决方法

I found the issue and filed https://github.com/Azure/azure-functions-cli/issues/206. The easiest workaround until we have an update:

Go to C:\Users\{user}\appdata\local\Azure.Functions.Cli\1.0.0.

Open func.exe.config in notepad.

Find this:

<dependentAssembly>
  <assemblyIdentity name="Microsoft.Azure.Documents.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.11.0.0" newVersion="1.11.0.0" />
</dependentAssembly>

In both places, replace 1.11.0.0 with 1.13.0.0 so you end up with:

<dependentAssembly>
    <assemblyIdentity name="Microsoft.Azure.Documents.Client" publicKeyToken="31bf3856ad364e35" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-1.13.0.0" newVersion="1.13.0.0" />
  </dependentAssembly>

Save and retry.

关于azure - Azure 函数参数中的 DocumentClient 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45804576/

相关文章:

collections - list.map 和 list.collect 的区别

c# - 在 Azure Functions 中发出故障信号的最佳方式是什么?

Azure terratest - 构建约束排除所有 Go 文件

azure - 在启用 WAF 的应用程序网关后面运行应用服务时,无法发布 Kudu 站点

.net - F#:函数中有更多返回点,如何处理?

c# - 如何添加对 Azure Function C# 项目的引用?

azure - 在 Azure Function 中接收服务总线消息

c# - MVC 图像上传 - 上传方向不正确

sql-server - 在 SSMS 中使用存储过程时,使用批量插入到 Azure SQL DB 时出错

f# - F# 中的内联关键字问题