Reputation: 581
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?
Upvotes: 0
Views: 850
Reputation: 71
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();
Upvotes: 1
Reputation: 141
The issue is caused by a bug in the @azure/service-bus sdk.
Workaround
DefaultDataTransformer
from "@azure/amqp-common"
library.
import { DefaultDataTransformer } from "@azure/amqp-common";
const { DefaultDataTransformer } = require("@azure/amqp-common");
scheduleMessage()
method to send the message as follows
const dt = new DefaultDataTransformer();
message.body = dt.encode(message.body);
More Reference and Investigation around this bug - Azure/azure-sdk-for-js#6816
Upvotes: 1