azure - 如何在 Microsoft Azure Functions 中将 DocumentDB 数据库输入绑定(bind)与服务总线触发器绑定(bind)和 BrokeredMessage 结合使用?

标签 azure azureservicebus azure-cosmosdb azure-functions

我正在编写一个简单的函数,当新消息到达服务总线队列时会触发该函数。该函数的输入是 DocumentDB 文档。我需要访问文档中的值并将其输入到控制台日志中。

当您创建服务总线队列触发器绑定(bind)时,它会自动创建如下:

public static void Run(string myQueueItem, TraceWriter log)
{
    log.Info($"C# ServiceBus queue trigger function processed message: {myQueueItem}");
}

现在根据 this page 访问我的数据库中的正确文档可以设置绑定(bind),以便从队列消息中获取文档 ID:

id supports bindings similar to {queueTrigger}, which uses the string value of the queue message as the document Id.

这意味着您可以将消息发送到队列,然后绑定(bind)到您的文档,如下所示:

{
    "name": "inputDocument",
    "type": "documentDB",
    "databaseName": "MyDatabase",
    "collectionName": "MyCollection",
    "id" : "{myQueueItem}",//<<<
    "connection": "MyAccount_DOCUMENTDB",     
    "direction": "in"
}

到目前为止,一切似乎都按预期进行。但是,为了将消息发送到服务总线队列,您必须将其作为 BrokeredMessage 类型 ( link ) 发送。当您这样做时,您将无法再以字符串形式访问消息,从而使自动创建的函数变得毫无用处。

可以通过以下方式使用 BrokeredMessages:

项目.json:

{
    "frameworks": {
        "net46":{
            "dependencies": {
                "WindowsAzure.ServiceBus": "2.7.6"
            }
        }
    }
}

运行.csx:

using Microsoft.ServiceBus.Messaging;
....
log.Info($"C# ID: {queueItem.GetBody<string>()})

但是我一直没能找到如何通过{queueItem.GetBody<string>()}进入id输入文档绑定(bind)的属性。所以我不能再使用前面提到的通过 "id" : "{myQueueItem}" 绑定(bind)我的 documentdb 输入的方法了因此无法从任何文档中读取任何值。

  • 有没有办法将原始字符串发送到总线队列,或者 BrokeredMessage 真的是唯一的可能性吗? (据我所知,原始字符串是不可能的)

  • 如果 BrokeredMessage 确实是唯一的方法,是否仍然可以从消息中获取字符串值并将其用作 DocumentDB 文档的 ID?

  • 如果以上都不可能,请您给我指出正确的方向吗?

对我的功能的更完整解释:

应用程序定期将新文档发送到 DocumentDB 数据库中。它与文档一起(等待插入完成后)将带有新文档 ID 的消息发送到服务总线队列中。然后触发器连接到新文档并检查其中的整数值。如果该值大于 10,则会发送电子邮件通知。

我已经成功创建了它,并通过基于 Web 的编辑器手册“运行”对其进行了测试,并使用其中一个文档的字符串 id 作为测试输入。但是,当我尝试将其与自动发送文档和消息的应用程序一起使用时,我遇到了如上所述的 BrokeredMessage 问题。

下面是没有 BrokeredMessage 的工作函数的完整代码:

using System;
using System.Net;
using System.Net.Mail;

public static void Run(string queueItem, dynamic inputDocument, TraceWriter log)
{
    log.Info($"C# ID: {queueItem}, Value: {inputDocument.Value}");

    if (inputDocument.Value > 10)
    {
        var fromAddress = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b79a9a9af7d0dad6dedb99d4d8da" rel="noreferrer noopener nofollow">[email protected]</a>", "From ---");
        var toAddress = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fa2a2a2cfe8e2eee6e3a1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>", "To ---");
        const string fromPassword = "---";
        const string subject = "Notification";
        const string body = "Temperature too high!";

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }
    }
}

最佳答案

下面是一个工作示例,展示了如何以 POCO 形式接收 ServiceBus 消息并绑定(bind)到其属性。

如果您使用正确的 application/json 内容类型向队列发送消息,该函数将为您反序列化为 POCO,并且文档 DB 输入绑定(bind)将绑定(bind)到 DocumentId 属性并为您检索文档。您无需在 project.json 中添加任何内容即可使其正常工作。

function.json 文件:

{
  "bindings": [
    {
      "name": "input",
      "type": "serviceBusTrigger",
      "direction": "in",
      "queueName": "<your-queue.",
      "connection": "<your-connection>",
      "accessRights": "Manage"
    },
    {
      "type": "documentDB",
      "name": "document",
      "databaseName": "<your-db>",
      "collectionName": "<your-collection>",
      "id": "{DocumentId}",
      "connection": "<your-connection>",
      "direction": "in"
    }
  ]
}

函数代码:

using System;
using System.Threading.Tasks;

public class Input
{
    public string DocumentId { get; set; }
    public int Value { get; set; }
}

public static void Run(Input input, dynamic document, TraceWriter log)
{
    log.Info($"Message received (DocumentId: {input.DocumentId}, Value {input.Value})");
    log.Info($"Document read {document.id}");
}

对于 C# 函数,要使用绑定(bind)参数(例如 {DocumentId} 参数),触发器输入必须绑定(bind)到定义这些属性的 POCO 对象。

要发送电子邮件,您还可以查看我们的 SendGrid 输出绑定(bind)。我们在门户的“示例”下提供了完整的示例“SendGrid-CSharp”:)

关于azure - 如何在 Microsoft Azure Functions 中将 DocumentDB 数据库输入绑定(bind)与服务总线触发器绑定(bind)和 BrokeredMessage 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41039785/

相关文章:

Azure 负载均衡器

Azure DevOps 管道测试失败...但我如何轻松识别哪些测试?

Azure逻辑应用程序: Service Bus Trigger Fails withe error ""message": "Error from token exchange....."

c# - Azure 服务总线发布/订阅到多个 azure Web 应用实例

json - 查询 azure CosmosDb 中的嵌套 JSON 属性

azure - 使用无服务器框架,如何在 yml 文件中设置 Azure CosmosDB?

azure - 如何通过 Management Rest API 在 Azure Web Apps 上设置 SSL 绑定(bind)?

azure - Windows Phone 对 Azure 服务总线通知中心的支持

Azure 机器学习工作室 - 从 Cosmos Db 导入

azure - 在隔离的 Azure 函数中的中间件之间传递值