【问题标题】:Unable to run cosmosDB trigger locally with Azure Function Core Tools无法使用 Azure 函数核心工具在本地运行 cosmosDB 触发器
【发布时间】:2018-07-05 21:08:10
【问题描述】:

我正在尝试使用 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”类型的绑定“文档”。这可能表明 function.json 属性无效。无法确定要调用哪个 ctor。

我错过了什么?我应该通过 http POST 触发该功能:

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

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

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

非常感谢。

【问题讨论】:

  • 注意我也运行过:func extensions install beforestart the function.
  • 请注意,如果我按照相同的过程创建 HTTP 触发器,它可以正常工作。
  • 这是 v1 还是 v2 函数应用?
  • 这是一个V2函数应用

标签: azure azure-functions azure-cosmosdb serverless azure-functions-core-tools


【解决方案1】:

问题是您的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
        }
    ]
}

local.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 Trigger无需注册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

然后一切都应该工作。

【讨论】:

  • 只是添加到 Jerry 的答案中,您不需要执行 POST 来触发函数,Cosmos DB 触发器的全部意义在于,当您插入或更新文档时,函数会触发源集合。
  • @MatiasQuaranta 谢谢,很好的补充。看来 OP 试图完全按照tutorial 来测试它是否有效。
  • 谢谢大家。现在完美运行!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-18
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
  • 1970-01-01
  • 2019-11-01
  • 1970-01-01
相关资源
最近更新 更多