Table of Contents

Class MessageSender

Namespace
Microsoft.ServiceBus.Messaging
Assembly
Microsoft.ServiceBus.dll

The MessageSender class is used to send messages from the Service Bus.

public abstract class MessageSender : ClientEntity
Inheritance
MessageSender
Inherited Members

Remarks

Please note that message delivery mechanism does not provide by default all time reliable message receiving. Service Bus deletes the message once it goes out of the system. For a guaranteed delivery, you can use the PeekLock delivery mode.

//********************************************************************************
//                             Sending messages to a Queue
//********************************************************************************

                  // Create a sender
                  MessageSender myMessageSender = myQueueClient.CreateSender(SendMode.Default);

                  // Send messages
                  List<object> Issues = new List<object>();
                  foreach (var issue in Issues)
                  {
                  myMessageSender.Send(new BrokeredMessage(issue));
                  }</code></pre></example><example>
                                   <pre><code class="lang-csharp">//********************************************************************************
                                   //                           Recieving messages from a Queue
                                   //********************************************************************************

                                   // Create a receiver
                                   MessageReceiver myMessageReceiver = 
                                   myQueueClient.CreateReceiver(ReceiveMode.PeekLock);

                                   // Receive messages
                                   for (int count = 0; count < Issues.Count; count++)
                                   {
                                   var message = myMessageReceiver.Receive();
                                   message.Complete();
                                   }</code></pre></example>

Properties

BatchFlushInterval

Gets the batch flush interval.

public virtual TimeSpan BatchFlushInterval { get; }

Property Value

TimeSpan

A batch flush interval. The default value is 20 ms.

Exceptions

OperationCanceledException

Thrown when the MessageSender is in closing, closed, or faulted state.

BatchingEnabled

Gets a value indicating whether the batching is enabled.

protected bool BatchingEnabled { get; }

Property Value

bool

true if batching is enabled; otherwise, false.

Path

Gets the path of the queue or topic relative to the MessagingFactory base address.

public abstract string Path { get; }

Property Value

string

The path of the queue or topic relative to the MessagingFactory base address.

SupportsGetRuntimeEntityDescription

protected abstract bool SupportsGetRuntimeEntityDescription { get; }

Property Value

bool

Methods

OnBeginCancelScheduledMessage(TrackingContext, IEnumerable<long>, TimeSpan, AsyncCallback, object)

This allows concrete implementations to override (if needed) what should be done to cancel scheduled sends

protected abstract IAsyncResult OnBeginCancelScheduledMessage(TrackingContext trackingContext, IEnumerable<long> sequenceNumbers, TimeSpan timeout, AsyncCallback callback, object state)

Parameters

trackingContext TrackingContext

Tracking context to use.

sequenceNumbers IEnumerable<long>

Sequence Number of the message to cancelled.

timeout TimeSpan

A client side timeout value for the operation. The operation should be aborted or cancel if the duration exceeded this timeout.

callback AsyncCallback

A user callback to be invoked when the operation completes.

state object

The state to be passed to the callback when the operation completes.

Returns

IAsyncResult

a IAsyncResult for the operation.

OnBeginScheduleMessage(TrackingContext, IEnumerable<BrokeredMessage>, TimeSpan, AsyncCallback, object)

This allows concrete implementations to override (if needed) what should be done to schedule sends

protected abstract IAsyncResult OnBeginScheduleMessage(TrackingContext trackingContext, IEnumerable<BrokeredMessage> messages, TimeSpan timeout, AsyncCallback callback, object state)

Parameters

trackingContext TrackingContext

Tracking context to use.

messages IEnumerable<BrokeredMessage>

Messages to be scheduled.

timeout TimeSpan

A client side timeout value for the operation. The operation should be aborted or cancel if the duration exceeded this timeout.

callback AsyncCallback

A user callback to be invoked when the operation completes.

state object

The state to be passed to the callback when the operation completes.

Returns

IAsyncResult

a IAsyncResult for the operation.

OnBeginSend(TrackingContext, IEnumerable<BrokeredMessage>, bool, TimeSpan, AsyncCallback, object)

Executes the begin send action. This method is intended for internal consumption and cannot be implemented in a concrete class by the user.

protected abstract IAsyncResult OnBeginSend(TrackingContext trackingContext, IEnumerable<BrokeredMessage> messages, bool fromSync, TimeSpan timeout, AsyncCallback callback, object state)

Parameters

trackingContext TrackingContext
messages IEnumerable<BrokeredMessage>
fromSync bool
timeout TimeSpan
callback AsyncCallback
state object

Returns

IAsyncResult

OnBeginSendEventData(TrackingContext, IEnumerable<EventData>, TimeSpan, AsyncCallback, object)

Executes the begin send event data action. This method is intended for internal consumption and cannot be implemented in a concrete class by the user.

protected abstract IAsyncResult OnBeginSendEventData(TrackingContext trackingContext, IEnumerable<EventData> eventDatas, TimeSpan timeout, AsyncCallback callback, object state)

Parameters

trackingContext TrackingContext
eventDatas IEnumerable<EventData>
timeout TimeSpan
callback AsyncCallback
state object

Returns

IAsyncResult

OnEndCancelScheduledMessage(IAsyncResult)

Raises an event when ending the cancellation of the scheduled message.

protected abstract void OnEndCancelScheduledMessage(IAsyncResult result)

Parameters

result IAsyncResult

The result of the operation.

OnEndScheduleMessage(IAsyncResult)

Raises an event when ending the message schedule.

protected abstract IEnumerable<long> OnEndScheduleMessage(IAsyncResult result)

Parameters

result IAsyncResult

The result of the operation.

Returns

IEnumerable<long>

The result of the operation.

OnEndSend(IAsyncResult)

Executes the end send action. This method is intended for internal consumption and cannot be implemented in a concrete class by the user.

protected abstract void OnEndSend(IAsyncResult result)

Parameters

result IAsyncResult

OnEndSendEventData(IAsyncResult)

Executes the end send event data action. This method is intended for internal consumption and cannot be implemented in a concrete class by the user.

protected abstract void OnEndSendEventData(IAsyncResult result)

Parameters

result IAsyncResult

The result of the operation.

OnSend(TrackingContext, IEnumerable<BrokeredMessage>, TimeSpan)

This allows concrete implementations to override (if needed) what should be done when sending messages in a synchronous manner.

protected virtual void OnSend(TrackingContext trackingContext, IEnumerable<BrokeredMessage> messages, TimeSpan timeout)

Parameters

trackingContext TrackingContext

TrackingContext to use.

messages IEnumerable<BrokeredMessage>

A list of BrokeredMessage to be sent.

timeout TimeSpan

A client side timeout value for the operation. The operation should be aborted or cancel if the duration exceeded this timeout.

Remarks

Typically the timeout comes from OperationTimeout.

OnSend is equal to this.OnEndSend(this.OnBeginSend(messages, timeout, null, null));.

Send(BrokeredMessage)

Sends the specified brokered message.

public void Send(BrokeredMessage message)

Parameters

message BrokeredMessage

The brokered message to send.

Exceptions

TimeoutException

Thrown when operation times out. Timeout period is initialized through the MessagingFactorySettings. You may need to increase the value of OperationTimeout to avoid this exception if the timeout value is relatively low.

ArgumentException

Thrown when message is null.

SendAsync(BrokeredMessage)

Asynchronously sends the specified brokered message.

public Task SendAsync(BrokeredMessage message)

Parameters

message BrokeredMessage

The brokered message to send.

Returns

Task

The asynchronous result of the operation.

SendBatch(IEnumerable<BrokeredMessage>)

Sends a set of brokered messages (for batch processing).

public void SendBatch(IEnumerable<BrokeredMessage> messages)

Parameters

messages IEnumerable<BrokeredMessage>

The collection of brokered messages to send.

SendBatchAsync(IEnumerable<BrokeredMessage>)

Asynchronously sends a set of brokered messages (for batch processing).

public Task SendBatchAsync(IEnumerable<BrokeredMessage> messages)

Parameters

messages IEnumerable<BrokeredMessage>

The collection of brokered messages to send.

Returns

Task

The asynchronous result of the operation.