Skip to content
  • English Deutsch

benjamin-abt.com

Performance is a feature | Cloud - AI - Azure - .NET - Sustainability

  • Home
  • Blog
  • Portfolio
    • Overview
    • Workshops & Training
    • Publications
  • Contact
  • Home
  • Blog

Azure IoT: send messages with C#

Azure IoT: send messages with C#
August 8, 2019 2 min read BEN ABT
Azure IoT IoT Hub .NET Csharp

This article was written in 2019. Versions, tools and recommendations may have changed since then. Newer posts are collected under Azure.

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

XML
<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.

C#
 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

Automatic TTL in Azure Cosmos DB with .NET

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 …

Automatically discover tools for Azure OpenAI Realtime API

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 …

Store Messages of Azure IoT Hub to Azure Blob Storage

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 - …


BEN ABT

BEN ABT

Ben is Chief Engineering Officer and Principal Software Engineer at Medialesson.de, with a strong focus on .NET and Microsoft Azure. In his professional role, he designs and builds highly scalable platforms for hybrid and cloud-based scenarios. He also advises companies and C-levels on migrating to the sovereign cloud.

He runs myCSharp.de, the largest and most active German-speaking C# community forum, actively contributes to both his own and external open-source projects and regularly shares his knowledge through his blog, at community events, conferences and as a writer for technical magazines.

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.

New Platforms

Modernization

Training & Consulting

Contact Me
X Facebook LinkedIn WhatsApp
Back Next Blog Post

Open for work!

Contact Me
BEN ABT BEN ABT Software Engineer · Microsoft MVP Passionate about .NET, Azure, performance optimization and sustainable software engineering.

Medialesson GmbH Logo

Microsoft MVP Logo

Recent posts

View all
Production Rate Limiting in ASP.NET Core

Production Rate Limiting in ASP.NET Core

Nov 3, 2026
The Big Decimal Bugfix with .NET 11

The Big Decimal Bugfix with .NET 11

Nov 2, 2026
Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Distroless, Chiseled or Alpine: Choosing a .NET Base Image

Oct 27, 2026
Stop Using nginx as Your Default Static File Server in Docker

Stop Using nginx as Your Default Static File Server in …

Oct 24, 2026
Containerizing .NET in 2026 Without a Dockerfile

Containerizing .NET in 2026 Without a Dockerfile

Oct 20, 2026

Tags

.NET (195) Csharp (99) ASP.NET Core (49) Azure (41) DevOps (17) Docker (15) Microsoft (14) EF Core (11) Microsoft MVP (11) Security (11) AI (10) GitHub (10) GitHub Copilot (10) ASP.NET (9) Synology (9) .NET 11 (8) Architecture (8) Raspberry (8) Azure Functions (7) Cloudflare (7)
benjamin-abt.com

About BEN

Benjamin Abt is a Software Engineer, Platform Architect and Microsoft MVP specializing in .NET, Azure, cloud architecture, performance optimization and sustainable software development.

Connect

  • Follow me on GitHub
  • Follow me on LinkedIn
  • Follow me on Mastodon
  • Microsoft MVP profile
  • Follow SchwabenCode on GitHub
  • Follow me on X

Quick Links

  • Portfolio
  • Blog
  • Workshops & Training
  • Publications
  • Get in Touch
  • Sitemap
Open for Work

© Copyright Benjamin Abt & SchwabenCode | 2011 - 2026

Content licensed under CC BY 4.0 unless otherwise noted.

  • Impressum
  • Privacy
  • Contact