0
votes

I'm using @azure/service-bus JavaScript library to publish and subscribe to messages on Azure Service Bus topic from Azure Functions. To receive messages, I'm using Azure Service Bus Topic trigger function created from the template without any changes. When I publish message using sender.send(message) I receive it fine.

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.

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

Is this a problem with the library or I'm doing something wrong?

2
This seems to be a bug with Azure Service Bus github.com/Azure/azure-webjobs-sdk/issues/2171Alexander Zhidkov

2 Answers

1
votes

The issue is caused by a bug in the @azure/service-bus sdk.

Workaround

  • Import DefaultDataTransformer from "@azure/amqp-common" library.
    • In typescript, import { DefaultDataTransformer } from "@azure/amqp-common";
    • In javascript, const { DefaultDataTransformer } = require("@azure/amqp-common");
  • Update the message body before calling the scheduleMessage() method to send the message as follows
    1. Instantiate the data transformer used by the 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

1
votes

in the new world (azure functions .net5) you can no longer use brokered messages. 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. but only a string!!!.

example;

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

the magic is now in FunctionContext executionContext you can get properties from this

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