My first AMQPNetLite program

After having some problems with my Netduino 3 wifi Azure Event Hub client code (which are most probably due to the issues discussed here) I decided to have a look at AMQPNetLite which had been suggested by Paolo Patierno in a response to one of my posts in the Netduino forums.

I usually create a “minimalist” project so I can figure out how a new library works without an domain specific code getting in the way. Overall, my first experience was pretty positive, the code compiled first time, ran second time and worked third time.

The objective for my first AMQPNetLite application running on my Netduino 3 Wifi was to connect to my home wifi, wait for an IP address then upload an event to an Azure EventHub

private const int amqpPortNumber = 5671;
private const string sbNamespace = "ServiceBus Namespace";
private const string keyName = "SaS Key name";
private const string keyValue = "SaS Key value";
private const string eventHub = "EventHub name";

...

// Wait for Network address if DHCP
NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];
if (networkInterface.IsDhcpEnabled)
{
   Debug.Print(" Waiting for IP address ");

   while (NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress == IPAddress.Any.ToString())
   {
      Debug.Print(".");
   }
}

// Display network config for debugging
Debug.Print("Network configuration");
Debug.Print(" Network interface type: " + networkInterface.NetworkInterfaceType.ToString());
Debug.Print(" MAC Address: " + BytesToHexString(networkInterface.PhysicalAddress));
Debug.Print(" DHCP enabled: " + networkInterface.IsDhcpEnabled.ToString());
Debug.Print(" Dynamic DNS enabled: " + networkInterface.IsDynamicDnsEnabled.ToString());
Debug.Print(" IP Address: " + networkInterface.IPAddress.ToString());
Debug.Print(" Subnet Mask: " + networkInterface.SubnetMask.ToString());
Debug.Print(" Gateway: " + networkInterface.GatewayAddress.ToString());

foreach (string dnsAddress in networkInterface.DnsAddresses)
{
   Debug.Print(" DNS Server: " + dnsAddress.ToString());
}

string deviceId = BytesToHexString(networkInterface.PhysicalAddress);
Debug.Print("DeviceId " + deviceId.ToString());

Then I constructed the AMQP address for the event hub, started an AMQP session and sent the message. I tried sending a message with a JSON payload and also using the “type safe” application properties.

try
{
   Address address = new Address(sbNamespace, amqpPortNumber, keyName, keyValue);
   Connection connection = new Connection(address);

   Session session = new Session(connection);

   SenderLink sender = new SenderLink(session, "send-link", eventHub);

   string messageBody = @"{""DeviceId"":""" + deviceId + @""",""Time"":""" + DateTime.Now.ToString("yy-MM-dd hh:mm:ss") + @"""}";

   Message message = new Message()
   {
      //BodySection = new Data() { Binary = Encoding.UTF8.GetBytes(messageBody)},
      ApplicationProperties = new Amqp.Framing.ApplicationProperties(),
   };

   message.ApplicationProperties["Time"] = DateTime.Now;
   message.ApplicationProperties["DeviceId"] = deviceId;

   sender.Send(message);

   sender.Close();
   session.Close();
   connection.Close();
}
catch (Exception ex)
{
   Debug.Print("ERROR: Publish failed with error: " + ex.Message);
}

For my scenario I was pleasantly surprised how easy it was to get working.

AMQP has a non TLS option (only for non sensitive data) and if this is supported I could use a device like a Netduino 3 Ethernet which don’t have baked in TLS support.

3 thoughts on “My first AMQPNetLite program

  1. Pingback: My first AzureSBLite program | devMobile's blog

    • Hi,

      Works well with Azure ServiceBus queues & event hubs. I used it (+AzureSBLite) in my recent MS Ignite NZ presentation running on .NetMF Netduino 3 Wifi & Windows 10 IoT Core RPI devices

      For AMQPNetLite I use the GitHub source rather than the nuget package as it is a little bit more current.

      Have some new posts about an AMQPNetLite based field gateway coming.

      @KiwiBryn

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s