【发布时间】:2018-08-31 12:35:01
【问题描述】:
我是第一次使用 Azure Functions。我正在尝试编写一个简单的函数来响应更改或添加到 CosmosDb 集合的文档。我写的函数是这样的:
[FunctionName("ChangeLog")]
public static void Run([CosmosDBTrigger(
databaseName: "Recaptcha",
collectionName: "Rules",
ConnectionStringSetting = "CosmosDBConnection",
LeaseCollectionName = null)]IReadOnlyList<RuleConfigCollection> documents)
{
if (documents != null && documents.Count > 0)
{
ApplicationEventLogger.Write(
Diagnostics.ApplicationEvents.RecaptchaRulesChanged,
new Dictionary<string, object>()
{
{ "SomeEnrichment", documents[0].Rules.ToList().Count.ToString() }
});
}
}
据我了解,当多个函数指向同一个 CosmosDb 时,租赁集合是必要的,但在我的情况下,这无关紧要。这就是为什么我将租约集合设置为null。
我已将其从 Visual Studio 发布到 Azure,并且可以看到该函数是使用以下 function.json 创建的:
{
"generatedBy": "Microsoft.NET.Sdk.Functions-1.0.12",
"configurationSource": "attributes",
"bindings": [
{
"type": "cosmosDBTrigger",
"connectionStringSetting": "CosmosDBConnection",
"collectionName": "Rules",
"databaseName": "Recaptcha",
"leaseDatabaseName": "Recaptcha",
"createLeaseCollectionIfNotExists": false,
"name": "documents"
}
],
"disabled": false,
"scriptFile": "../bin/My.Namespace.Functions.App.dll",
"entryPoint": "My.Namespace.Functions.App.ChangeLogFunction.Run"
}
我还添加了一个名为CosmosDBConnection 的应用程序设置,其值为AccountEndpoint=https://my-cosmosdb.documents.azure.com:443;AccountKey=myAccountKey;。
我运行该函数,然后将一个文档添加到集合中,但日志一直显示No new trace in the past n min(s),并且我希望看到的应用程序事件没有被写入。
我在这个设置中遗漏了什么吗?
【问题讨论】:
-
好的,我现在实际上从我自己的一个依赖项中抛出了一个异常,所以我的函数正在被调用。不确定这是否是因为我根据@mikhail 在下面的评论添加了租约集合。但看起来这现在是我这边的应用程序问题。
-
我更改的另一件事是将
RuleConfigCollection(我的强类型文档模型)替换为通用Microsoft.Azure.Documents.Document。稍后我将不得不研究强类型。
标签: c# asp.net azure azure-functions azure-cosmosdb