首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NodeJS -@azure/服务总线:如何将消息中的`userProperties`传入C#后台

NodeJS -@azure/服务总线:如何将消息中的`userProperties`传入C#后台
EN

Stack Overflow用户
提问于 2020-12-02 16:00:18
回答 1查看 1K关注 0票数 2

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

代码语言:javascript
复制
let message = {
    body: JSON.stringify(body),
    customProperties: {
        userId: userId
    }
};

但是,在更改为使用包@azure/service-bus之后,我需要稍微更改一下以获得C#代码中的正文,如下所示:

代码语言:javascript
复制
let signMessage = {
    body: body,
    customProperties: { // tried to use userProperties but not okay
        userId: userId
    }
};

但是,我仍然无法在C#或ServiceBus资源管理器中成功获取userProperties

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-12-03 10:47:49

简单的代码:

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65104213

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档