All Products
Search
Document Center

ApsaraMQ for RocketMQ:Send and receive messages by using Apache RocketMQ 5.x SDK for C#

Last Updated:Mar 11, 2026

This topic describes the sample code for sending and receiving messages by using Apache RocketMQ 5.x SDK for C#.

Prerequisites

Before you begin, make sure that you have:

Sample code overview

Message typeProducer exampleConsumer example
Normal messagesProducerNormalMessageExample.csSimpleConsumerExample.cs
Ordered messagesProducerFifoMessageExample.csSimpleConsumerExample.cs
Scheduled and delayed messagesProducerDelayMessageExample.csSimpleConsumerExample.cs
Transactional messagesProducerTransactionMessageExample.csSimpleConsumerExample.cs

Parameters

Replace the following placeholders in all sample code with your actual values:

ParameterExampleDescription
Endpointrmq-cn-xxx.{regionId}.rmq.aliyuncs.com:8080

The instance endpoint. Use the public endpoint for Internet access or the VPC endpoint for VPC access. For more information, see Obtain the endpoint of an instance.

InstanceIdrmq-cn-xxx

The ID of the ApsaraMQ for RocketMQ instance.

topicnormal_test

The topic to which messages are sent or from which messages are consumed. Create the topic on the instance in advance. For more information, see Create a topic.

consumerGroupGID_test

The consumer group for consuming messages. Create the consumer group on the instance in advance. For more information, see Create a consumer group.

AccessKey1XVg0hzgKm******

The instance username. Required for Internet access. For VPC access, required only when the instance is serverless and authentication-free in VPCs is disabled. For more information, see Obtain the username and password of an instance.

SecretKeyijSt8rEc45******

The instance password. Required under the same conditions as AccessKey. For more information, see Obtain the username and password of an instance.

Access serverless instances over the Internet

To access a serverless ApsaraMQ for RocketMQ instance over the Internet, add SetNamespace to the client configuration and set it to your instance ID:

Note

Replace InstanceId with your actual instance ID.

var clientConfig = new ClientConfig.Builder()
    .SetEndpoints(endpoints)
    .SetNamespace("<your-instance-id>")   // Replace with your actual instance ID, for example, rmq-cn-xxx.
    .SetCredentialsProvider(credentialsProvider)
    .Build();