【问题标题】:DocumentClient binding in parameter of Azure functionAzure 函数参数中的 DocumentClient 绑定
【发布时间】:2017-08-21 19:50:33
【问题描述】:

我正在 F# 中编写一个 http 触发器 Azure 函数,并且我想绑定一个 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"
    }
  ]
}

host.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"
      }
    }
  }
}

local.settings.json

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

我正在使用开发存储和 Cosmos DB 模拟器在本地运行该功能。我试图将 C# 的 here 描述的内容翻译成 F#。它也与here 提到的几乎相同。但我只收到错误Microsoft.Azure.WebJobs.Extensions.DocumentDB: 'Id' is required when binding to a DocumentClient property

【问题讨论】:

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


    【解决方案1】:

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

    从 github 复制的解决方法

    我发现了问题并提交了 https://github.com/Azure/azure-functions-cli/issues/206。最简单的 解决方法,直到我们有更新:

    转到C:\Users\{user}\appdata\local\Azure.Functions.Cli\1.0.0

    在记事本中打开func.exe.config

    找到这个:

    <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>
    

    在这两个地方,将1.11.0.0 替换为1.13.0.0,这样你最终会得到:

    <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>
    

    保存并重试。

    【讨论】:

    • 是的,这行得通。谢谢!我已经安装了 Microsoft.Azure.DocumentDB v.1.17,所以我需要降级到 v.1.13。
    • 嗯。想知道如果我们从 Portal 上的 Functions IDE 中收到此错误,我们应该怎么做...
    猜你喜欢
    • 2018-03-30
    • 1970-01-01
    • 2020-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多