简体   繁体   English

Azure 服务总线计划消息“输入”绑定未定义

[英]Azure Service Bus scheduled message "in" binding undefined

I'm using @azure/service-bus JavaScript library to publish and subscribe to messages on Azure Service Bus topic from Azure Functions.我正在使用 @azure/service-bus JavaScript 库从 Azure Functions 发布和订阅有关 Azure 服务总线主题的消息。 To receive messages, I'm using Azure Service Bus Topic trigger function created from the template without any changes.为了接收消息,我使用了从模板创建的 Azure 服务总线主题触发器函数,无需任何更改。 When I publish message using sender.send(message) I receive it fine.当我使用 sender.send(message) 发布消息时,我收到了很好的消息。

import { AzureFunction, Context, HttpRequest } from "@azure/functions"
import * as sb from "@azure/service-bus"

const PublishToServiceBus: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    const eventDoc = req.body;
    const serviceBusConnectionString = process.env["ServiceBusConnection"];
    const topicName = process.env["TopicName"]';
    const sbClient = sb.ServiceBusClient.createFromConnectionString(serviceBusConnectionString);
    const topicClient = sbClient.createTopicClient(topicName);
    const sender = topicClient.createSender();

    const message: sb.SendableMessageInfo = { body: eventDoc };

    // this works
    sender.send(message);

    // this creates message without body?
    const scheduledEnqueueTimeUtc = new Date(Date.now() + 10000);
    sender.scheduleMessages(scheduledEnqueueTimeUtc, [message]);

};
export default PublishToServiceBus;

But when I schedule message with sender.scheduleMessages(), my incoming binding variable is undefined in Azure Service Bus Topic trigger function.但是当我使用 sender.scheduleMessages() 安排消息时,我的传入绑定变量在 Azure 服务总线主题触发器函数中未定义。

import { AzureFunction, Context } from "@azure/functions"

const serviceBusTopicTrigger: AzureFunction = async function (context: Context, mySbMsg: any): Promise<void> {
    context.log('ServiceBus topic trigger function processed message', mySbMsg);
};
export default serviceBusTopicTrigger;

Output: ServiceBus topic trigger function processed message undefined输出: ServiceBus topic trigger function processed message undefined

Is this a problem with the library or I'm doing something wrong?这是图书馆的问题还是我做错了什么?

The issue is caused by a bug in the @azure/service-bus sdk.该问题是由 @azure/service-bus sdk 中的错误引起的。

Workaround解决方法

  • Import DefaultDataTransformer from "@azure/amqp-common" library."@azure/amqp-common"库导入DefaultDataTransformer
    • In typescript, import { DefaultDataTransformer } from "@azure/amqp-common";在打字稿中, import { DefaultDataTransformer } from "@azure/amqp-common";
    • In javascript, const { DefaultDataTransformer } = require("@azure/amqp-common");在javascript中, const { DefaultDataTransformer } = require("@azure/amqp-common");
  • Update the message body before calling the scheduleMessage() method to send the message as follows在调用scheduleMessage()方法发送消息之前更新消息体如下
    1. Instantiate the data transformer used by the sdk:实例化 sdk 使用的数据转换器:
      • const dt = new DefaultDataTransformer();
    2. When you need to schedule the message, encode the message body before sending:当您需要调度消息时,请在发送前对消息正文进行编码:
      • message.body = dt.encode(message.body);

More Reference and Investigation around this bug - Azure/azure-sdk-for-js#6816有关此错误的更多参考和调查 - Azure/azure-sdk-for-js#6816

in the new world (azure functions .net5) you can no longer use brokered messages.在新世界(azure 函数 .net5)中,您不能再使用代理消息。 the new libraries do not cater for it.新图书馆不能满足它。

Function app declaration is no longer [FunctionName=] but [Function= You can no longer receive 'Message' or byte.函数应用声明不再是 [FunctionName=] 而是 [Function= 你不能再接收“消息”或字节。 but only a string!!!.但只有一个字符串!!!。

example;例子;

[Function("TestFA")] public async Task Run([ServiceBusTrigger(topicName, subscriberName, Connection = ???)] string messageText, string id, FunctionContext executionContext ) [Function("TestFA")] public async Task Run([ServiceBusTrigger(topicName,subscribeName, Connection = ???)] string messageText, string id, FunctionContext executionContext )

the magic is now in FunctionContext executionContext you can get properties from this魔术现在在 FunctionContext executionContext 中,您可以从中获取属性

eg KeyValuePair<string, object> props = executionContext.BindingContext.BindingData.Where(x => x.Key == "UserProperties").FirstOrDefault();例如 KeyValuePair<string, object> props = executionContext.BindingContext.BindingData.Where(x => x.Key == "UserProperties").FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM