【问题标题】:How can I set CosmosDBTrigger parameters with values from the app values stored in the Function App?如何使用存储在函数应用程序中的应用程序值的值设置 CosmosDBTrigger 参数?
【发布时间】:2019-11-26 19:37:16
【问题描述】:

我正在开发一个 Azure 函数,每次在 CosmosDB 中更新文档时都会触发该函数。我已经成功完成了它,但现在我想避免在 CosmosDBTrigger 装饰器中硬编码值。

正如我在另一篇 SO 帖子中看到的那样,我已经尝试使用“%”符号来引用存储的应用程序值。但是,它似乎不起作用。帖子是:Is it possible to configure Azure C# function DocumentDB attribute arguments?

这是我正在尝试的解决方案:

 public static void Run([CosmosDBTrigger(
            databaseName: "%LogLevelsCachingDatabaseName%",
            collectionName: "%LogLevelsCachingCollectionName%",
            ConnectionStringSetting = "%LogLevelsCachingDatabaseSetting%",
            LeaseCollectionName = "%LogLevelsCachingLeaseCollectionName%",
            StartFromBeginning = true
            )]IReadOnlyList<Document> input, ILogger log)
        {
            if (input == null || input.Count <= 0) return;

工作解决方案完全相同,但 '%' 之间的值是硬编码值。

这是我将 Azure 函数发布到 Azure DevOps 时的输出。请注意,ConnectionStringSetting 值是有效的,因为我已经能够运行 Azure 函数,而硬编码值没有任何问题。

错误:

函数 (CacheLogLevels) 错误:Microsoft.Azure.WebJobs.Host:错误索引方法“CacheLogLevels”。 Microsoft.Azure.WebJobs.Extensions.CosmosDB:无法使用数据库 %LogLevelsCachingDatabaseName% 中的租约 %LogLevelsCachingLeaseCollectionName% 为数据库 %LogLevelsCachingDatabaseName% 中的 %LogLevelsCachingCollectionName% 创建集合信息:无法解析属性“CosmosDBTriggerAttribute.ConnectionStringSetting”的应用程序设置。确保应用设置存在并且具有有效值。 Microsoft.Azure.WebJobs.Extensions.CosmosDB:无法解析属性“CosmosDBTriggerAttribute.ConnectionStringSetting”的应用设置。确保应用设置存在并且具有有效值。 会话 ID:34dd30479d6a440caf063493bd1abc3d

时间戳:2019-07-17T22:35:01.376Z

我想使用来自 Azure Function App 配置的值:

Azure 函数应用配置

非常感谢您阅读我的问题,祝您有美好的一天!

维托里奥

【问题讨论】:

    标签: c# azure azure-functions azure-cosmosdb decorator


    【解决方案1】:

    ConnectionStringSettingsLeaseConnectionStringSetting 参数自动将值解析为应用设置,不带百分号(请参阅trigger configuration):

    public static void Run([CosmosDBTrigger(
                databaseName: "%LogLevelsCachingDatabaseName%",
                collectionName: "%LogLevelsCachingCollectionName%",
                ConnectionStringSetting = "LogLevelsCachingDatabaseSetting", // <-- remove %-signs
                LeaseCollectionName = "%LogLevelsCachingLeaseCollectionName%",
                StartFromBeginning = true
                )]IReadOnlyList<Document> input, ILogger log)
    

    【讨论】:

    • 解决了我的问题!所以我从 ConnectionStringSetting 中删除了“%”,在 LeaseCollectionName 周围保留了“%”,并添加了 LeaseDatabaseName 属性,使其与 databaseName 的值相同。我还必须进入 azure 的 appsettings 并将 LogLevelsCachingDatabaseSettings 的值替换为完整的连接字符串,而不仅仅是设置名称!非常感谢!最诚挚的问候,维托里奥
    【解决方案2】:

    同意@vladimir

    ConnectionStringSettings 和 LeaseConnectionStringSetting 参数自动将值解析为应用设置

    有两种方法可以做到这一点:

    1. 通过 Azure 门户手动更新值

    2. 通过 Azure 门户上的发布管道设置它

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2016-02-01
      • 2011-01-14
      • 1970-01-01
      • 2010-12-18
      • 2011-07-05
      • 1970-01-01
      • 2012-02-01
      • 2018-03-29
      相关资源
      最近更新 更多