【问题标题】:NodeJS - @azure/service-bus : How to pass `userProperties` in message to C# backendNodeJS - @azure/service-bus:如何将消息中的 `userProperties` 传递给 C# 后端
【发布时间】:2021-01-26 23:56:13
【问题描述】:

之前,我使用azure-sb 包处理NodeJS 中的服务总线消息,示例代码如下:

let message = {
    body: JSON.stringify(body),
    customProperties: {
        userId: userId
    }
};

然而,在改成使用包@azure/service-bus后,我需要稍作改动以获取C#代码中的body,如下所示:

let signMessage = {
    body: body,
    customProperties: { // tried to use userProperties but not okay
        userId: userId
    }
};

但是,我仍然无法在 C# 或 ServiceBus Explorer 中成功获取 userProperties

【问题讨论】:

标签: node.js azure azureservicebus servicebus custom-properties


【解决方案1】:

简单代码:

const { ServiceBusClient } = require("@azure/service-bus");

const connectionString = "Endpoint=sb://bowman1012.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=xxxxxx"
const topicName = "test";

const messages = [
    { body: "Albert Einstein",
      applicationProperties: {
        userId: 'userId'
      }
    }
 ];

 async function main() {
    // create a Service Bus client using the connection string to the Service Bus namespace
    const sbClient = new ServiceBusClient(connectionString);

    // createSender() can also be used to create a sender for a queue.
    const sender = sbClient.createSender(topicName);

    try {
        // Tries to send all messages in a single batch.
        // Will fail if the messages cannot fit in a batch.
        // await sender.sendMessages(messages);

        // create a batch object
        let batch = await sender.createMessageBatch(); 
        for (let i = 0; i < messages.length; i++) {
            // for each message in the arry         

            // try to add the message to the batch
            if (!batch.tryAddMessage(messages[i])) {            
                // if it fails to add the message to the current batch
                // send the current batch as it is full
                await sender.sendMessages(batch);

                // then, create a new batch 
                batch = await sender.createBatch();

                // now, add the message failed to be added to the previous batch to this batch
                if (!batch.tryAddMessage(messages[i])) {
                    // if it still can't be added to the batch, the message is probably too big to fit in a batch
                    throw new Error("Message too big to fit in a batch");
                }
            }
        }

        // Send the last created batch of messages to the topic
        await sender.sendMessages(batch);

        console.log(`Sent a batch of messages to the topic: ${topicName}`);

        // Close the sender
        await sender.close();
    } finally {
        await sbClient.close();
    }
}

// call the main function
main().catch((err) => {
    console.log("Error occurred: ", err);
    process.exit(1);
 });

对我来说效果很好。

这是 API 参考:

https://docs.microsoft.com/en-us/javascript/api/@azure/service-bus/servicebusmessage?view=azure-node-latest

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-18
    • 2022-08-17
    • 2020-09-18
    • 1970-01-01
    • 2021-02-04
    • 2022-10-08
    • 1970-01-01
    • 2023-02-09
    相关资源
    最近更新 更多