Table of Contents

Interface ISubscriptionClient

Namespace
Microsoft.Azure.ServiceBus
Assembly
Microsoft.Azure.ServiceBus.dll

SubscriptionClient can be used for all basic interactions with a Service Bus Subscription.

public interface ISubscriptionClient : IReceiverClient, IClientEntity
Inherited Members

Examples

Create a new SubscriptionClient

ISubscriptionClient subscriptionClient = new SubscriptionClient(
    namespaceConnectionString,
    topicName,
    subscriptionName,
    ReceiveMode.PeekLock,
    RetryExponential);

Register a message handler which will be invoked every time a message is received.

subscriptionClient.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 subscriptionClient is opened in ReceiveMode.PeekLock mode.
           await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
       },
       async (exceptionEvent) =>
       {
           // Process the exception
           Console.WriteLine("Exception = " + exceptionEvent.Exception);
           return Task.CompletedTask;
       });

Remarks

Use MessageReceiver for advanced set of functionality.

Properties

SubscriptionName

Gets the name of subscription.

string SubscriptionName { get; }

Property Value

string

TopicPath

Gets the path of the topic, for this subscription.

string TopicPath { get; }

Property Value

string

Methods

AddRuleAsync(RuleDescription)

Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.

Task AddRuleAsync(RuleDescription description)

Parameters

description RuleDescription

The rule description that provides the rule to add.

Returns

Task

A task instance that represents the asynchronous add rule operation.

Remarks

You can add rules to the subscription that will decide filter which messages from the topic should reach the subscription. A default TrueFilter rule named DefaultRuleName is always added while creation of the Subscription. You can add multiple rules with distinct names to the same subscription. Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the message is passed on to the subscription.

AddRuleAsync(string, Filter)

Adds a rule to the current subscription to filter the messages reaching from topic to the subscription.

Task AddRuleAsync(string ruleName, Filter filter)

Parameters

ruleName string
filter Filter

The filter expression against which messages will be matched.

Returns

Task

A task instance that represents the asynchronous add rule operation.

Remarks

You can add rules to the subscription that will decide filter which messages from the topic should reach the subscription. A default TrueFilter rule named DefaultRuleName is always added while creation of the Subscription. You can add multiple rules with distinct names to the same subscription. Multiple filters combine with each other using logical OR condition. i.e., If any filter succeeds, the message is passed on to the subscription.

GetRulesAsync()

Get all rules associated with the subscription.

Task<IEnumerable<RuleDescription>> GetRulesAsync()

Returns

Task<IEnumerable<RuleDescription>>

RegisterSessionHandler(Func<IMessageSession, Message, CancellationToken, Task>, SessionHandlerOptions)

Receive session messages continuously from the subscription. 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 subscription 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 SessionHandlerOptions

Options 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 subscription. 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 subscription 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.

RemoveRuleAsync(string)

Removes the rule on the subscription identified by ruleName.

Task RemoveRuleAsync(string ruleName)

Parameters

ruleName string

Returns

Task

A task instance that represents the asynchronous remove rule operation.