
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

Mar 17, 2026 · 15 min read
GitHub Copilot - Custom Agents for Full-Stack Teams: A Practical Operating Model for .NET, React and Azure
GitHub Copilot custom agents allow teams to define specialized AI assistants, each with its own role, tool access and behavioral boundaries. …

Feb 26, 2026 · 6 min read
Run Azure Cosmos DB locally with .NET Aspire and make emulator endpoints visible in the dashboard
When building cloud-native .NET applications, two goals often matter at the same time: a fast local development loop and a clean path to …

Sep 24, 2025 · 9 min read
Automatically discover tools for Azure OpenAI Realtime API
Azure now provides a unified Realtime API for low‑latency, multimodal conversations over WebRTC or WebSockets. If you’ve used the earlier …
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.

Comments