This topic has not yet been rated - Rate this topic

MessageSender Class

The MessageSender class is used to send messages from the Service Bus. Although you can use the QueueClient class to send and receive messages without creating a MessageSender object, you can also use MessageSender and MessageReceiver to create a sender and receiver on an entity without knowing whether it is a topic or a queue.

System.Object
  Microsoft.ServiceBus.Messaging.MessageClientEntity
    Microsoft.ServiceBus.Messaging.MessageSender

Namespace:  Microsoft.ServiceBus.Messaging
Assembly:  Microsoft.ServiceBus (in Microsoft.ServiceBus.dll)
public abstract class MessageSender : MessageClientEntity

The MessageSender type exposes the following members.

  Name Description
Public property BatchFlushInterval Gets the batch flush interval.
Protected property BatchingEnabled Gets a value indicating whether the batching is enabled.
Public property IsClosed Gets or sets a value that indicates whether the message client entity is closed. (Inherited from MessageClientEntity.)
Public property Path Gets the path of the queue or topic relative to the MessagingFactory base address.
Top
  Name Description
Public method Abort Aborts the message client entity and puts its status into a closing state. (Inherited from MessageClientEntity.)
Public method BeginClose Begins an asynchronous operation to close the message client entity. (Inherited from MessageClientEntity.)
Public method BeginSend Begins an asynchronous request to send a brokered message.
Public method Close Closes the message client entity and puts its status into a closed state. (Inherited from MessageClientEntity.)
Public method EndClose Finishes an asynchronous operation to close the message client entity. (Inherited from MessageClientEntity.)
Public method EndSend Ends an asynchronous request to send a message.
Public method Equals (Inherited from Object.)
Protected method Fault Puts the message client entity into a faulted state. (Inherited from MessageClientEntity.)
Protected method Finalize (Inherited from Object.)
Public method GetHashCode (Inherited from Object.)
Public method GetType (Inherited from Object.)
Protected method MemberwiseClone (Inherited from Object.)
Protected method OnAbort Executes upon calling the Abort operation. (Inherited from MessageClientEntity.)
Protected method OnBeginClose Executes upon calling the BeginClose operation. (Inherited from MessageClientEntity.)
Protected method OnBeginOpen Executes upon calling the operation to begin to open the message client entity. (Inherited from MessageClientEntity.)
Protected method OnBeginSend Allows concrete implementations to define what should be done when sending messages. This method cannot be implemented in a concrete class by the user.
Protected method OnClose Executes upon calling the Close operation. (Inherited from MessageClientEntity.)
Protected method OnClosed Occurs when the message client entity is transitioned into a closing state. (Inherited from MessageClientEntity.)
Protected method OnEndClose Executes upon calling the EndClose operation. (Inherited from MessageClientEntity.)
Protected method OnEndOpen Executes upon calling the operation to end to open the message client entity. (Inherited from MessageClientEntity.)
Protected method OnEndSend Executes the end send action. This method cannot be implemented in a concrete class by the user.
Protected method OnFaulted Executes upon calling the Fault operation. (Inherited from MessageClientEntity.)
Protected method OnOpen Executes upon calling the operation to open the message client entity. (Inherited from MessageClientEntity.)
Protected method OnOpened Executes when the message client entity is opened. (Inherited from MessageClientEntity.)
Protected method OnSend Allows concrete implementations to override (if needed) what should be done when sending messages in a synchronous manner.
Public method Send Sends the specified brokered message.
Protected method ThrowIfClosed Throws an exception if the message client entity is closed. (Inherited from MessageClientEntity.)
Protected method ThrowIfDisposed Throws an exception if the message client entity is disposed. (Inherited from MessageClientEntity.)
Protected method ThrowIfDisposedOrImmutable Throws an exception if the client is disposed or immutable. (Inherited from MessageClientEntity.)
Protected method ThrowIfDisposedOrNotOpen Throws an exception if the client is disposed or not open. (Inherited from MessageClientEntity.)
Protected method ThrowIfFaulted Throws an exception if the client is faulted. (Inherited from MessageClientEntity.)
Public method ToString (Inherited from Object.)
Top

Note that the default message receive mode is PeekLock, which ensures reliable message receiving.

The following example sends messages to a queue.

Uri serviceUri = ServiceBusEnvironment.CreateServiceUri("sb", ServiceNamespace, string.Empty); 

MessagingFactory factory = MessagingFactory.Create(serviceUri, credentials); 

MessageSender myMessageSender = factory.CreateMessageSender(queueName); 

List<BrokeredMessage> messageList = new List<BrokeredMessage>();
messageList.Add(CreateIssueMessage("1", "Package lost"));
messageList.Add(CreateIssueMessage("2", "Package damaged"));
messageList.Add(CreateIssueMessage("3", "Package defective"));

foreach (BrokeredMessage message in messageList)
{
    myMessageSender.Send(message);
    Console.WriteLine(
    string.Format("Message sent: Id = {0}, Body = {1}", message.MessageId, message.GetBody<string>()));
}

Any public static (Shared in Visual Basic) members of this type are thread safe. Instance members are also guaranteed to be thread safe.

Did you find this helpful?
(1500 characters remaining)