Cross Platform IoT: Developing a .NET Core based simulator for Azure IoT Hub using VS for Mac!
Cross Platform IoT: Developing a .NET Core based simulator for Azure IoT Hub using VS for Mac!
This post is part of a Cross-Platform IoT series, to see other posts in the series refer here.
The source for the solution is available on GitHub here.
March 7th was a significant milestone for the Microsoft Visual Studio team. With Visual Studio completing 20 years and the launch of Visual Studio 2017, the team demonstrated that Visual Studio continues to lead the path for .NET development on Windows. While the Visual Studio 2017 is not cross-platform and does not work on other OS like MacOS (yet ;)), Microsoft, for some time, has also launched a preview version for another cousin of Visual Studio — The Visual Studio for Mac!!
VS for Mac at first impression seems like a cosmetic redo for the Xamarin Studio (post acquisition of Xamarin by Microsoft) but as the new updates keep coming it seems to be bringing all the goodies of Visual Studio to the native MacOS platform. In this post, I will show how to use Visual Studio for Mac to build a .NET Core solution that will act as a simple device emulator and will send and receive messages commands to Azure IoT Hub.
Wait, don’t we already have VS Code? Yeah, that’s correct, VS Code is there and provides some awesome cross-platform development support. I am a big fan of its simplicity, speed and extension eco-system. #LoveVSCode. However, VS for Mac includes some additional features such as Project templates, Build and Run from IDE support, Native Xamarin App development experience, and now Visual Studio Test Framework support which makes it a step closer to the Visual Studio IDE available in Windows. Which is better? Well, time will tell, for the purpose of this post, however, we will use VS for Mac running on MacOS Sierra (10.12.3).
Getting VS for Mac
VS for Mac is in preview right now, and you should use it for development scenarios and with caution. At the time of writing, VS team had released Preview 5 builds, and that is what we used for this post.
You can download the .dmg
from the Visual Studio website here to install the application or use homebrew for the installation:
brew cask install visual-studio
The setup process is fairly straightforward. Once installed, launch the Visual Studio app, and you should be presented with a welcome screen similar to below:
VS for Mac (current build) does not install .NET Core SDK on your machine by default and you will need to manually install it. To install .NET Core SDK on MacOS we will again use homebrew. The VS team provides step by step instructions on how to do this here.
Note that the installation asks for installing openssl
which is a dependency for .NET Core, it mainly uses the libcrypto
and libssl
libraries. In some cases, you may see a warning like this. Warning: openssl is a keg-only and another version is linked to opt.
To continue installation use the following command:
brew install --force openssl
Also, ignore the warning Warning: openssl-1.0.2k already installed, it's just not linked.
or use brew unlink openssl
before executing the above command
Writing our simulator app for IoT Hub
The source for this project is available at the GitHub repo here.
Now that we have VS for Mac and .NET Core SDK installed and setup, we are going to build a .NET Core Simulator app which will be performing the following operations:
- Send a message (ingress) using a .NET Core simulator to Azure IoT Hub using AMQP as the protocol.
- Receive messages from Azure IoT Hub (egress) using a .NET Core consumer.
- Send a command to a device and receive an acknowledgment from the device (coming soon).
The process to create the project is very similar to the experience of Visual Studio on Windows.
- Click New Project from the welcome screen to open the New Project dialog. We will select a .NET Core App project of type Console Application. At the time of writing C# and F# are supported applications for .NET Core projects in VS for Mac.
- In the next screen, we provide a project and solution name and configure our project for git.
- VS for Mac will now setup the project and solution. This first thing it does is to restore any required packages. Since VS for Mac has support for NuGet, it just uses Nuget package restore internally to get all the required dependencies installed. Once all dependencies are restored, you should see a screen similar to below. We have our .NET Core project created in VS for Mac!
- Before we start working on the simulator code, let’s ensure we have Source Control enabled for our project. In VS for Mac, the
Version Control
menu allows you to configure your SVN of Git repo. Note that this functionality is derived from Xamarin Studio, you can use the guidance here to set up version control for your project. We will be using a git repo and GitHub for our project.
Now that we have our project and version control sorted out, let’s start working on the code for our IoT Simulator. As noted earlier, the simulator will perform the following actions:
- Send a message to IoT Hub using Device credentials
- Act as a Consumer of the message from IoT Hub
- Receive Commands and send response acknowledgment
The solution has multiple projects and is described in the GitHub repo here. The repo. also discusses how to use the sample. Instead of talking through all the projects, I will talk about the logic to send a message to a device when using .NET Core assemblies, this should give an idea of how to use VS for Mac and .NET Core with IoT Hub.
Sending messages over AMQP
The easiest option to work with IoT Hub is the Device and Service SDKs. Unfortunately, at the time of writing this post, we do not have a .NET Core version of these SDKs. If you try to add the Nuget packages you should get incompatibility errors like below:
Package Microsoft.Azure.Devices.Client 1.2.5 is **not compatible** with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Microsoft.Azure.Devices.Client 1.2.5 supports:
net45 (.NETFramework,Version=v4.5)
portable-monoandroid10+monotouch10+net45+uap10+win8+wp8+wpa81+xamarinios10 (.NETPortable,Version=v0.0,Profile=net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10)
uap10.0 (UAP,Version=v10.0)
So what are our options here:
- If you want to use the HTTPS protocol, you can build an HTTP Client and run it through the REST API model.
- If you want to use AMQP as the protocol, you can use the AMQP Lite library available on Nuget. AMQP Lite has support for .NET Core today and provides underlying functionality to send and receive packets to IoT Hub using AMQP as the protocol. We will be using this for your sample.
- If you want to use MQTT, there are few Nuget packages like M2Mqtt that you can try to use. I have not tried them yet.
When the IoT Hub releases the .NET Core versions, they should be the recommended way to process messages with IoT Hub
Adding the Nuget package
Adding NuGet packages in VS for Mac is straightforward. Simply right click on your project -> Add
-> Add Nuget package
. It opens the NuGet dialog box that allows searching for all available packages. In our case, we search for the AMQP Lite package and add it to our project.
Note that currently all packages are shown including full .NET Framework packages. VS for Mac, however, validates if the package can be added to a .NET Core project, in case the package has binaries or dependencies that rely on full .NET Framework, you should see an error in the package console
**Checking compatibility** for Microsoft.Azure.Devices.Client 1.2.5 with .NETCoreApp,Version=v1.1.
Package Microsoft.Azure.Devices.Client 1.2.5 is not compatible with netcoreapp1.1 (.NETCoreApp,Version=v1.1). Package Microsoft.Azure.Devices.Client 1.2.5 supports:
net45 (.NETFramework,Version=v4.5)
portable-monoandroid10+monotouch10+net45+uap10+win8+wp8+wpa81+xamarinios10 (.NETPortable,Version=v0.0,Profile=net45+wp8+wpa81+win8+MonoAndroid10+MonoTouch10+Xamarin.iOS10+UAP10)
uap10.0 (UAP,Version=v10.0)
Setting up the environment variables
Once we have the AMQP Lite assemblies, we now need our IoT Hub details. You can use the Azure CLI tools for IoT mentioned in my previous post to fetch this information. In the sample, I leverage a JSON configuration handler to dump the configuration in a JSON file and read from it during runtime.
{
"settings":{
"connectionStrings":[
{
"name":"youriothubname",
"connectionString":"youriothub.azure-devices.net",
"sasKey":"yourdevicekey",
"sasKeyName":"device"
}
],
"deviceId":"D1234"
}
}
Sending the message to IoT Hub
The final part of the puzzle is to write code that will open a connection and send a message to IoT Hub. We leverage the AMQP Lite library to perform these actions. In the sample, I follow a Strategy pattern to execute methods on the underlying library, it gives us the flexibility to change underlying implementations to a different library (for example when the IoT Hub SDK become .NET Core compliant) without making a lot of code changes.
// Create a connection using device context
Connection connection = await Connection.Factory.CreateAsync(new Address(iothubHostName, deviceContext.Port));
Session session = new Session(connection);string audience = Fx.Format("{0}/devices/{1}", iothubHostName, deviceId);
string resourceUri = Fx.Format("{0}/devices/{1}", iothubHostName, deviceId);
// Generate the SAS token
string sasToken = TokenGenerator.GetSharedAccessSignature(null, deviceContext.DeviceKey, resourceUri, new TimeSpan(1, 0, 0));
bool cbs = TokenGenerator.PutCbsToken(connection, iothubHostName, sasToken, audience);
if (cbs)
{
// create a session and send a telemetry message
session = new Session(connection);
byte[] messageAsBytes = default(byte[]);
if (typeof(T) == typeof(byte[]))
{
messageAsBytes = message as byte[];
}
else
{
// convert object to byte[]
}// Get byte[] from
await SendEventAsync(deviceId, messageAsBytes, session);
await session.CloseAsync();
await connection.CloseAsync();
}
The above code first opens a connection using the AMQPLite Connection type. It then establishes an AMQP session using the Session type and generates the SAS token based on the Device credentials. The sample also uses a protobuf serializer to encode the payload as a byte [] and finally send it to IoT Hub using SendEventAsync.If you run the samples.iot.simulator.sender .NET Core console app in the sample, it calls the ExecuteOperationAsync to send the payload and the required environment variables to the AMQP Lite library. The results of a successful message sent would be displayed in a console window.
Consuming messages from IoT HubThe sample also demonstrates the other scenarios such as consuming messages however you can also use an out of box Azure Service like Azure Stream Analytics to process these messages. Here is an example of using Stream Analytics for event processing.Phew! This was a long post, it demonstrates some of the capabilities as well as challenges of developing .NET Core solutions with VS for Mac. I think VS for Mac is a great addition to the IDE toolkit especially for developers who are used to the Visual Studio Windows environment. There are few rough edges that need here and there but remember this is just a preview! … Happy Coding :)The source for the solution is available on GitHub here.This post is part of a Cross-Platform IoT series, to see other posts in the series refer here.