
Requirements
- You must have created an IoT Hub in Azure.
- You must have created a device in your created Azure IoT Hub .
- You need the connection string your IoT Hub device (
IoT Hub -> Devices -> Select Device -> Configurations)
NuGet
To use the DeviceClient , which opens the connection to your Azure IoT Hub , you need the NuGet package Microsoft.Azure.Devices.Client
1<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.21.1" />
Snippet
The following snippet shows a simple class that sends serialized data (class instances) to the Azure IoT Hub.
The connection string and the device id are required to guarantee uniqueness. With the Azure IoT Hub, the connection string is always at the device level. In this way, the Azure IoT Hub guarantees the identity of the device.
The example uses an interface (IIoTHubMessage) for generic constraints
.
So to the send method cannot be passed every kind of instance, but only classes that implement this specific interface.
1// Interface to provide generic constraints
2public interface IIoTHubMessage { }
3
4// Message class for temperature values
5public class TemperatureStatusMessage : IIoTHubMessage
6{
7 public double Value {get;set;}
8}
9
10// Service class to communicate with the Azure IoT Hub
11public class IoTHubServices
12{
13 private readonly DeviceClient _deviceClient;
14 private readonly string _deviceId;
15
16 // Creates the service class instance with your IoTHub connection string and your unique device id
17 public IoTHubService(string connectionString, string deviceId)
18 {
19 _deviceClient = DeviceClient.CreateFromConnectionString(connectionString, TransportType.Amqp); // use AMQP
20 _deviceId = deviceId;
21 }
22
23 // sends messages to IoT Hub - generic constraint to send only specific message types
24 public async Task SendAsync<TMessage>(TMessage message) where IIoTHubMessage, class
25 {
26 string serializedMessage = JsonConvert.SerializeObject(message);
27 Message iotHubMessage = new Message(Encoding.UTF8.GetBytes(serializedMessage));
28
29 await _deviceClient.SendEventAsync(iotHubMessage);
30 }
31}
The message is then sent via AMQP as Json body to the Azure IoT Hub.
Consume Messages
The message can now be consumed on Azure IoT Hub in different ways:
- An Azure Function is triggered that processes the message.
- With the help of Stream Analaytics the message can be evaluated.
- The Azure IoT Hub offers (a rather expensive) possibility to store the message directly in AVRO or JSON format on an Azure storage . This can also be easily implemented with an Azure Function.
- The message can be forwarded to an Azure Event Hub to be forwarded to different brokers or consumers.
Related articles

Aug 17, 2026 - 17 min read
Automatic TTL in Azure Cosmos DB with .NET
Temporary data is easy to create and surprisingly difficult to remove reliably. Sessions, idempotency keys, import buffers, transient …

Mar 13, 2020 - 4 min read
Store Messages of Azure IoT Hub to Azure Blob Storage
In most cases, Azure IoT Hub messages should be stored for a longer period of time. In fact, there are several ways to achieve this goal - …

Aug 02, 2019 - 2 min read
Setting up an Azure IoT Hub
The Azure IoT Hub is a highly scalable, secure platform for the bidirectional communication of devices and software components in the sense …
Let's Work Together
Looking for an experienced Platform Architect or Engineer for your next project? Whether it's cloud migration, platform modernization or building new solutions from scratch - I'm here to help you succeed.
