gpt4 book ai didi

azure - 无法使用 Azure Function Core Tools 在本地运行 cosmosDB 触发器

转载 作者:行者123 更新时间:2023-12-02 07:08:11 36 4
gpt4 key购买 nike

我正在尝试使用 Azure Functions Core Tools 在我的笔记本电脑上本地运行 azure 函数。请注意,此函数被配置为 cosmosDB 触发器

我的部分灵感来自于 tutorial 中的说明

我首先使用以下命令创建一个名为 MyFirstFunction 的函数(并在出现提示时插入所需的输入):

func init
func start

我生成的 javascript 函数是(与 Azure 门户为同类模板函数创建的函数相同):

module.exports = function (context, documents) {
if (!!documents && documents.length > 0) {
context.log('Document Id: ', documents[0].id);
}
context.done();
}

我生成的function.json是:

{
"bindings":
[
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases"
}
]
}

我生成的local.settings.json

{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": "UseDevelopmentStorage=true"
}
}

鉴于此设置,我尝试通过执行来运行该函数:

func host start

控制台输出一切正常,直到出现错误消息:无法配置“cosmosDBTrigger”类型的绑定(bind)“文档”。这可能表明 function.json 属性无效。无法弄清楚该调用哪个 Actor 。

我错过了什么?我应该通过 http POST 触发该函数:

http://localhost:{port}/admin/functions/{function_name}

如上面链接的教程中所述(此函数是 cosmosDB 触发器),但此错误后甚至无法加载该函数。

在本地运行 cosmosDB 触发器时我缺少什么?

非常感谢。

最佳答案

问题是您的 local.settings.jsonfunction.json 缺少必要的 cosmosdb 连接字符串配置。

参见cosmosdb trigger document .

函数.json

{
"bindings":
[
{
"type": "cosmosDBTrigger",
"name": "documents",
"direction": "in",
"leaseCollectionName": "leases",

// Missed in your code
"connectionStringSetting": "CosmosDBConnectionString",
"databaseName": "<Get db name>",
"collectionName": "<Get coll name>",
"createLeaseCollectionIfNotExists": true
}
]
}

本地.settings.json

 {
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"AzureWebJobsStorage": "UseDevelopmentStorage=true",

//Missed in your code
"CosmosDBConnectionString":"<Get connection string from Azure portal>"
}
}

请注意,在最新版本的函数核心工具(2.0.1-beta.31)中,如果使用 func new 创建 CosmosDB 触发器,则无需注册 CosmosDB 扩展,工具会自动安装它.

此问题解决后,如果您遇到有关 Microsoft.Azure.Documents.ServiceInterop.dll 的另一个错误

The listener for function 'Functions.CosmosDb' was unable to start.
The listener for function 'Functions.CosmosDb' was unable to start. System.Private.CoreLib: One or more errors occurred. (Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E)).
Microsoft.Azure.DocumentDB.Core: Unable to load DLL 'Microsoft.Azure.Documents.ServiceInterop.dll' or one of its dependencies: The specified module could not be found. (Exception from HRESULT: 0x8007007E).

只需安装一个软件包即可修复(请参阅此 issue )

func extensions install -p Microsoft.Azure.DocumentDB.Core -v 2.0.0-preview

然后一切都应该正常。

关于azure - 无法使用 Azure Function Core Tools 在本地运行 cosmosDB 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51199933/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com