This sample demonstrates how to send and receive session messages from a session-enabled Service Bus queue.
Receiving from sessions is performed using the ServiceBusSessionReceiver
. This
type derives from ServiceBusReceiver
and exposes session-related functionality.
string connectionString = "<connection_string>";
string queueName = "<queue_name>";
// since ServiceBusClient implements IAsyncDisposable we create it with "await using"
await using var client = new ServiceBusClient(connectionString);
// create the sender
ServiceBusSender sender = client.CreateSender(queueName);
// create a session message that we can send
ServiceBusMessage message = new ServiceBusMessage(Encoding.UTF8.GetBytes("Hello world!"))
{
SessionId = "mySessionId"
};
// send the message
await sender.SendAsync(message);
// create a session receiver that we can use to receive the message. Since we don't specify a
// particular session, we will get the next available session from the service.
ServiceBusSessionReceiver receiver = await client.CreateSessionReceiverAsync(queueName);
// the received message is a different type as it contains some service set properties
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveAsync();
Console.WriteLine(receivedMessage.SessionId);
// we can also set arbitrary session state using this receiver
// the state is specific to the session, and not any particular message
await receiver.SetSessionStateAsync(Encoding.UTF8.GetBytes("some state"));
// the state can be retrieved for the session as well
byte[] state = await receiver.GetSessionStateAsync();
// create a receiver specifying a particular session
ServiceBusSessionReceiver receiver = await client.CreateSessionReceiverAsync(
queueName,
new ServiceBusSessionReceiverOptions
{
SessionId = "Session2"
});
// the received message is a different type as it contains some service set properties
ServiceBusReceivedMessage receivedMessage = await receiver.ReceiveAsync();
Console.WriteLine(receivedMessage.SessionId);
To see the full example source, see: