Quick Start with Amqp.Net Lite

Create a topic and a subscription on the topic through the Azure portal or through the Service Bus client SDK (using NamespaceManager) (refer to the Service Bus documentation for details).

Send a message to a Azure Service Bus topic:
var amqpConnection = new Connection(new Address 
      ("amqps://owner:<key>=@<namespace>servicebus.windows.net"));  
amqpConnection.OnClosed += this.ConnectionClosed;  
 
var amqpSession = new Session(this.AmqpConnection);  
amqpSession.OnClosed += this.SessionClosed;  
  
SenderLink amqpSender = new SenderLink(amqpSession, 
       "send-link/in/t0000", // unique name for all links from this client 
       "/in/t0000"); // Service Bus entity name 
amqpSender.OnClosed += this.SenderLinkClosed;  
  
var message = new Message();  
message.Properties = new Properties();  
message.Properties.Subject = "mymessagetype";  
message.ApplicationProperties = new ApplicationProperties();  
message.ApplicationProperties["MyProperty"] = "Hello World!";  
  
amqpSender.Send(message, MessageOutcomeCallback, null);  


Receive messages from a Azure Service Bus subscription of the topic:
// Open connection and session as per above (or reuse the same connection/session as for send)
var amqpReceiver = new ReceiverLink(this.AmqpSession, 
    "receive-linkout/t0000/Subscriptions/s0000", // unique name for all links from this client
    "out/t0000/Subscriptions/s0000"); // Service Bus entity name
amqpReceiver.OnClosed += this.ReceiverLinkClosed;
amqpReceiver.Start(5, OnMessageCallback);

...

void OnMessageCallback(ReceiverLink receiver, Message message)
{
    var value = message.ApplicationProperties["MyProperty"];
    // Do something with the value
    receiver.Accept(message);
    receiver.SetCredit(5);
}


The OBD Recorder sample http://code.msdn.microsoft.com/OBD-Recorder-for-Net-Micro-b927ce94 further illustrates how to use this library in an end-to-end scenario.

FAQ:
  1. Reserved characters (i.e. '/') in connection strings must be URL encoded. See Issue 1.

Last edited Jul 29, 2014 at 4:46 AM by xinchen, version 1