Interface IQueueClient
- Namespace
- Microsoft.Azure.ServiceBus
- Assembly
- Microsoft.Azure.ServiceBus.dll
QueueClient can be used for all basic interactions with a Service Bus Queue.
public interface IQueueClient : IReceiverClient, ISenderClient, IClientEntity
- Inherited Members
Examples
Create a new QueueClient
IQueueClient queueClient = new QueueClient(
namespaceConnectionString,
queueName,
ReceiveMode.PeekLock,
RetryExponential);
Send a message to the queue:
byte[] data = GetData();
await queueClient.SendAsync(data);
Register a message handler which will be invoked every time a message is received.
queueClient.RegisterMessageHandler(
async (message, token) =>
{
// Process the message
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
// Complete the message so that it is not received again.
// This can be done only if the queueClient is opened in ReceiveMode.PeekLock mode.
await queueClient.CompleteAsync(message.SystemProperties.LockToken);
},
async (exceptionEvent) =>
{
// Process the exception
Console.WriteLine("Exception = " + exceptionEvent.Exception);
return Task.CompletedTask;
});
Remarks
Use IMessageSender or IMessageReceiver for advanced set of functionality.
Properties
QueueName
Gets the name of the queue.
string QueueName { get; }
Property Value
Methods
RegisterSessionHandler(Func<IMessageSession, Message, CancellationToken, Task>, SessionHandlerOptions)
Receive session messages continuously from the queue. Registers a message handler and begins a new thread to receive session-messages. This handler(Func<T1, T2, T3, TResult>) is awaited on every time a new message is received by the queue client.
void RegisterSessionHandler(Func<IMessageSession, Message, CancellationToken, Task> handler, SessionHandlerOptions sessionHandlerOptions)
Parameters
handler
Func<IMessageSession, Message, CancellationToken, Task>A Func<T1, T2, T3, TResult> that processes messages. IMessageSession contains the session information, and must be used to perform Complete/Abandon/Deadletter or other such operations on the Message
sessionHandlerOptions
SessionHandlerOptionsOptions used to configure the settings of the session pump.
Remarks
Enable prefetch to speed up the receive rate.
RegisterSessionHandler(Func<IMessageSession, Message, CancellationToken, Task>, Func<ExceptionReceivedEventArgs, Task>)
Receive session messages continuously from the queue. Registers a message handler and begins a new thread to receive session-messages. This handler(Func<T1, T2, T3, TResult>) is awaited on every time a new message is received by the queue client.
void RegisterSessionHandler(Func<IMessageSession, Message, CancellationToken, Task> handler, Func<ExceptionReceivedEventArgs, Task> exceptionReceivedHandler)
Parameters
handler
Func<IMessageSession, Message, CancellationToken, Task>A Func<T1, T2, T3, TResult> that processes messages. IMessageSession contains the session information, and must be used to perform Complete/Abandon/Deadletter or other such operations on the Message
exceptionReceivedHandler
Func<ExceptionReceivedEventArgs, Task>A Func<T, TResult> that is invoked during exceptions. ExceptionReceivedEventArgs contains contextual information regarding the exception.
Remarks
Enable prefetch to speed up the receive rate. Use RegisterSessionHandler(Func<IMessageSession, Message, CancellationToken, Task>, SessionHandlerOptions) to configure the settings of the pump.