MessageSender MessageSender Class

Definition

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

public abstract class MessageSender : ClientEntity, IMessageClientEntity, ICloseable, IMessageSenderPublic MustInherit Class MessageSender
    Inherits ClientEntity
    Implements IMessageClientEntity, ICloseable, IMessageSender
Inheritance
MessageSenderMessageSender

Inherited Members

Microsoft.ServiceBus.Messaging.ClientEntity

System.Object

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)); }

//********************************************************************************
//                           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(); }


Properties

BatchFlushInterval BatchFlushInterval

Gets the batch flush interval.

public virtual TimeSpan BatchFlushInterval { get; }Public Overridable ReadOnly Property BatchFlushInterval As TimeSpan
Value
TimeSpan TimeSpan

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

Exceptions

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

BatchingEnabled BatchingEnabled

Gets a value indicating whether the batching is enabled.

protected bool BatchingEnabled { get; }Protected ReadOnly Property BatchingEnabled As Boolean
Value
Boolean Boolean

true if batching is enabled; otherwise, false.

Path Path

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

public abstract string Path { get; }Public MustOverride ReadOnly Property Path As String
Value
String String

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

SupportsGetRuntimeEntityDescription SupportsGetRuntimeEntityDescription

protected abstract bool SupportsGetRuntimeEntityDescription { get; }Protected MustOverride ReadOnly Property SupportsGetRuntimeEntityDescription As Boolean

Methods

OnBeginCancelScheduledMessage(TrackingContext, IEnumerable<Int64>, TimeSpan, AsyncCallback, Object) OnBeginCancelScheduledMessage(TrackingContext, IEnumerable(Of Int64), 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)Protected MustOverride Function OnBeginCancelScheduledMessage(trackingContext As TrackingContext, sequenceNumbers As IEnumerable(Of Long), timeout As TimeSpan, callback As AsyncCallback, state As Object) As IAsyncResult
Parameters
trackingContext
TrackingContext TrackingContext

Tracking context to use.

sequenceNumbers
IEnumerable<Int64> IEnumerable(Of Int64)

Sequence Number of the message to cancelled.

timeout
TimeSpan TimeSpan

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

callback
AsyncCallback AsyncCallback

A user callback to be invoked when the operation completes.

state
Object Object

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

Returns

a IAsyncResult for the operation.

OnBeginScheduleMessage(TrackingContext, IEnumerable<BrokeredMessage>, TimeSpan, AsyncCallback, Object) OnBeginScheduleMessage(TrackingContext, IEnumerable(Of 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)Protected MustOverride Function OnBeginScheduleMessage(trackingContext As TrackingContext, messages As IEnumerable(Of BrokeredMessage), timeout As TimeSpan, callback As AsyncCallback, state As Object) As IAsyncResult
Parameters
trackingContext
TrackingContext TrackingContext

Tracking context to use.

messages
IEnumerable<BrokeredMessage> IEnumerable(Of BrokeredMessage)

Messages to be scheduled.

timeout
TimeSpan TimeSpan

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

callback
AsyncCallback AsyncCallback

A user callback to be invoked when the operation completes.

state
Object Object

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

Returns

a IAsyncResult for the operation.

OnBeginSend(TrackingContext, IEnumerable<BrokeredMessage>, Boolean, TimeSpan, AsyncCallback, Object) OnBeginSend(TrackingContext, IEnumerable(Of BrokeredMessage), Boolean, 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)Protected MustOverride Function OnBeginSend(trackingContext As TrackingContext, messages As IEnumerable(Of BrokeredMessage), fromSync As Boolean, timeout As TimeSpan, callback As AsyncCallback, state As Object) As IAsyncResult
Parameters
fromSync
Boolean Boolean
timeout
TimeSpan TimeSpan
state
Object Object
Returns

OnBeginSendEventData(TrackingContext, IEnumerable<EventData>, TimeSpan, AsyncCallback, Object) OnBeginSendEventData(TrackingContext, IEnumerable(Of 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)Protected MustOverride Function OnBeginSendEventData(trackingContext As TrackingContext, eventDatas As IEnumerable(Of EventData), timeout As TimeSpan, callback As AsyncCallback, state As Object) As IAsyncResult
Parameters
timeout
TimeSpan TimeSpan
state
Object Object
Returns

OnEndCancelScheduledMessage(IAsyncResult) OnEndCancelScheduledMessage(IAsyncResult)

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

protected abstract void OnEndCancelScheduledMessage(IAsyncResult result)Protected MustOverride Sub OnEndCancelScheduledMessage(result As IAsyncResult)
Parameters
result
IAsyncResult IAsyncResult

The result of the operation.

OnEndScheduleMessage(IAsyncResult) OnEndScheduleMessage(IAsyncResult)

Raises an event when ending the message schedule.

protected abstract IEnumerable<long> OnEndScheduleMessage(IAsyncResult result)Protected MustOverride Function OnEndScheduleMessage(result As IAsyncResult) As IEnumerable(Of Long)
Parameters
result
IAsyncResult IAsyncResult

The result of the operation.

Returns

The result of the operation.

OnEndSend(IAsyncResult) 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)Protected MustOverride Sub OnEndSend(result As IAsyncResult)
Parameters

OnEndSendEventData(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)Protected MustOverride Sub OnEndSendEventData(result As IAsyncResult)
Parameters
result
IAsyncResult IAsyncResult

The result of the operation.

OnSend(TrackingContext, IEnumerable<BrokeredMessage>, TimeSpan) OnSend(TrackingContext, IEnumerable(Of 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)Protected Overridable Sub OnSend(trackingContext As TrackingContext, messages As IEnumerable(Of BrokeredMessage), timeout As TimeSpan)
Parameters
trackingContext
TrackingContext TrackingContext

TrackingContext to use.

timeout
TimeSpan 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) Send(BrokeredMessage)

Sends the specified brokered message.

public void Send(BrokeredMessage message)Public Sub Send(message As BrokeredMessage)
Parameters
message
BrokeredMessage BrokeredMessage

The brokered message to send.

Exceptions

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.

Thrown when message is null.

SendAsync(BrokeredMessage) SendAsync(BrokeredMessage)

Asynchronously sends the specified brokered message.

public Task SendAsync(BrokeredMessage message)Public Function SendAsync(message As BrokeredMessage) As Task
Parameters
message
BrokeredMessage BrokeredMessage

The brokered message to send.

Returns

The asynchronous result of the operation.

SendBatch(IEnumerable<BrokeredMessage>) SendBatch(IEnumerable(Of BrokeredMessage))

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

public void SendBatch(IEnumerable<BrokeredMessage> messages)Public Sub SendBatch(messages As IEnumerable(Of BrokeredMessage))
Parameters
messages
IEnumerable<BrokeredMessage> IEnumerable(Of BrokeredMessage)

The collection of brokered messages to send.

SendBatchAsync(IEnumerable<BrokeredMessage>) SendBatchAsync(IEnumerable(Of BrokeredMessage))

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

public Task SendBatchAsync(IEnumerable<BrokeredMessage> messages)Public Function SendBatchAsync(messages As IEnumerable(Of BrokeredMessage)) As Task
Parameters
messages
IEnumerable<BrokeredMessage> IEnumerable(Of BrokeredMessage)

The collection of brokered messages to send.

Returns

The asynchronous result of the operation.