我在F#中编写了一个http触发器Azure函数,我希望绑定一个DocumentClient
类型的参数,以便更好地控制在Cosmos DB中完成的查询。这就是我到目前为止所做的:
Function.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)
function.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模拟器在本地运行该函数。我试图将F#描述为C#所描述的here。它也与提到的here几乎相同。但我只收到错误Microsoft.Azure.WebJobs.Extensions.DocumentDB: 'Id' is required when binding to a DocumentClient property
。
答案 0 :(得分:5)
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>
保存并重试。