Table of Contents

Class MessageSender

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

The MessageSender can be used to send messages to Queues or Topics.

public class MessageSender : ClientEntity, IMessageSender, ISenderClient, IClientEntity
Inheritance
MessageSender
Implements
Inherited Members

Examples

Create a new MessageSender to send to a Queue

IMessageSender messageSender = new MessageSender(
    namespaceConnectionString,
    queueName)

Send message

byte[] data = GetData();
await messageSender.SendAsync(data);

Remarks

This uses AMQP protocol to communicate with service.

Constructors

MessageSender(ServiceBusConnection, string, RetryPolicy)

Creates a new AMQP MessageSender on a given ServiceBusConnection

public MessageSender(ServiceBusConnection serviceBusConnection, string entityPath, RetryPolicy retryPolicy = null)

Parameters

serviceBusConnection ServiceBusConnection

Connection object to the service bus namespace.

entityPath string

The path of the entity this sender should connect to.

retryPolicy RetryPolicy

The RetryPolicy that will be used when communicating with Service Bus. Defaults to Default

MessageSender(ServiceBusConnection, string, string, RetryPolicy)

Creates a ViaMessageSender. This can be used to send messages to a destination entity via another another entity.

public MessageSender(ServiceBusConnection serviceBusConnection, string entityPath, string viaEntityPath, RetryPolicy retryPolicy = null)

Parameters

serviceBusConnection ServiceBusConnection

Connection object to the service bus namespace.

entityPath string

The final destination of the message.

viaEntityPath string

The first destination of the message.

retryPolicy RetryPolicy

The RetryPolicy that will be used when communicating with Service Bus. Defaults to Default

Remarks

This is mainly to be used when sending messages in a transaction. When messages need to be sent across entities in a single transaction, this can be used to ensure all the messages land initially in the same entity/partition for local transactions, and then let service bus handle transferring the message to the actual destination.

MessageSender(ServiceBusConnectionStringBuilder, RetryPolicy)

Creates a new AMQP MessageSender.

public MessageSender(ServiceBusConnectionStringBuilder connectionStringBuilder, RetryPolicy retryPolicy = null)

Parameters

connectionStringBuilder ServiceBusConnectionStringBuilder

The ServiceBusConnectionStringBuilder having entity level connection details.

retryPolicy RetryPolicy

The RetryPolicy that will be used when communicating with Service Bus. Defaults to Default

Remarks

Creates a new connection to the entity, which is opened during the first operation.

MessageSender(string, string, ITokenProvider, TransportType, RetryPolicy)

Creates a new MessageSender

public MessageSender(string endpoint, string entityPath, ITokenProvider tokenProvider, TransportType transportType = TransportType.Amqp, RetryPolicy retryPolicy = null)

Parameters

endpoint string

Fully qualified domain name for Service Bus. Most likely, {yournamespace}.servicebus.windows.net

entityPath string

Queue path.

tokenProvider ITokenProvider

Token provider which will generate security tokens for authorization.

transportType TransportType

Transport type.

retryPolicy RetryPolicy

Retry policy for queue operations. Defaults to Default

Remarks

Creates a new connection to the entity, which is opened during the first operation.

MessageSender(string, string, RetryPolicy)

Creates a new AMQP MessageSender.

public MessageSender(string connectionString, string entityPath, RetryPolicy retryPolicy = null)

Parameters

connectionString string

Namespace connection string used to communicate with Service Bus. Must not contain Entity details.

entityPath string

The path of the entity this sender should connect to.

retryPolicy RetryPolicy

The RetryPolicy that will be used when communicating with Service Bus. Defaults to Default

Remarks

Creates a new connection to the entity, which is opened during the first operation.

Properties

OperationTimeout

Duration after which individual operations will timeout.

public override TimeSpan OperationTimeout { get; set; }

Property Value

TimeSpan

Path

Gets the entity path of the MessageSender.

public override string Path { get; }

Property Value

string

RegisteredPlugins

Gets a list of currently registered plugins for this sender.

public override IList<ServiceBusPlugin> RegisteredPlugins { get; }

Property Value

IList<ServiceBusPlugin>
See Also

ServiceBusConnection

Connection object to the service bus namespace.

public override ServiceBusConnection ServiceBusConnection { get; }

Property Value

ServiceBusConnection

Methods

CancelScheduledMessageAsync(long)

Cancels a message that was scheduled.

public Task CancelScheduledMessageAsync(long sequenceNumber)

Parameters

sequenceNumber long

The SequenceNumber of the message to be cancelled.

Returns

Task

OnClosingAsync()

Closes the connection.

protected override Task OnClosingAsync()

Returns

Task

RegisterPlugin(ServiceBusPlugin)

Registers a ServiceBusPlugin to be used with this sender.

public override void RegisterPlugin(ServiceBusPlugin serviceBusPlugin)

Parameters

serviceBusPlugin ServiceBusPlugin

The ServiceBusPlugin to register.

ScheduleMessageAsync(Message, DateTimeOffset)

Schedules a message to appear on Service Bus at a later time.

public Task<long> ScheduleMessageAsync(Message message, DateTimeOffset scheduleEnqueueTimeUtc)

Parameters

message Message

The Message that needs to be scheduled.

scheduleEnqueueTimeUtc DateTimeOffset

The UTC time at which the message should be available for processing

Returns

Task<long>

The sequence number of the message that was scheduled.

SendAsync(Message)

Sends a message to the entity as described by Path.

public Task SendAsync(Message message)

Parameters

message Message

Returns

Task

SendAsync(IList<Message>)

Sends a list of messages to the entity as described by Path.

public Task SendAsync(IList<Message> messageList)

Parameters

messageList IList<Message>

Returns

Task

UnregisterPlugin(string)

Unregisters a ServiceBusPlugin.

public override void UnregisterPlugin(string serviceBusPluginName)

Parameters

serviceBusPluginName string

The name Name to be unregistered