Azure IoT SDK for .NET

repository·main·Indexed 19 days ago

https://github.com/azure/azure-iot-sdk-csharp

The Microsoft Azure IoT device SDK for .NET provides capabilities for sending event data, mapping server commands to device functions, and managing device state via Device Twins. This repository includes the SDK, automation tools for detecting breaking changes, and a stress test tool for performance testing IoT Hub communication (D2C, C2D, and Direct Methods). It provides guidance on DeviceClient initialization, connection status management, X.509 certificate authentication, and TLS 1.3 configuration.

Tokens
25.9K
Snippets
58
Records
134
Agent score
66%

What's inside azure-iot-sdk-csharp

  1. Overview of Azure IoT SDK for .NET

    main

    The Microsoft Azure IoT SDK for .NET provides four primary sets of tools for interacting with Azure IoT services:

    • IoT Hub Device SDK: Used by client devices to connect to Azure IoT Hub.
    • IoT Hub Service SDK: Used by back-end .NET applications to manage IoT Hub service instances.
    • Provisioning Device SDK: Used by devices to provision themselves to Azure IoT Hub via the Device Provisioning Service (DPS).
    • Provisioning Service SDK: Used by back-end .NET applications to manage the Provisioning service instance.
  2. IoT Plug and Play service samples overview

    main

    These C# samples demonstrate how to manage devices that follow the IoT Plug and Play conventions via Azure IoT Hub. They provide implementations for the following core operations:

    • Read read-only properties: Accessing device state that the device does not allow modification of.
    • Write read-write properties: Updating device configuration or state.
    • Invoke commands: Triggering specific actions on the device.

    The samples cover two primary device model scenarios:

    1. Thermostat Model: A single interface containing telemetry, read-only properties, read-write properties, and commands.
    2. Temperature Controller Model: A complex model using multiple components, including a top-level interface (telemetry, read-only properties, commands), two Thermostat components, and a device information component.
  3. Capabilities of the Azure IoT service SDK

    main

    The Microsoft.Azure.Devices SDK provides the following core functionalities for interacting with Azure IoT Hub from a service/backend application:

    • Device Identity Management: Create, remove, update, and list device identities within your IoT Hub.
    • Cloud-to-Device Messaging: Send messages to your devices and receive feedback/confirmation when they are delivered.
  4. IoT Plug and Play device samples overview

    main

    These C# samples demonstrate how devices following IoT Plug and Play (PnP) conventions interact with Azure IoT Hub or Azure IoT Central. The samples cover three primary interaction patterns:

    • Sending telemetry: Reporting sensor data or status.
    • Updating properties: Managing both read-only and read-write properties.
    • Command invocation: Responding to commands sent from the cloud.

    Sample Scenarios

    1. Thermostat Model: A single-interface model that implements telemetry, read-only properties, read-write properties, and commands.
    2. Temperature Controller Model: A multi-component model consisting of:
      • A top-level interface (telemetry, read-only properties, and commands).
      • Two Thermostat components.
      • A device information component.
  5. Available Azure IoT device samples

    main

    The following samples demonstrate various features of the Microsoft Azure IoT Hub service using C# code:

    • Reconnection: Illustrates handling connection issues, connection-related exceptions, managing DeviceClient lifetime, sending messages, and symmetric key failover.
    • Method: Demonstrates Direct Methods.
    • Receive message: Demonstrates receiving messages from the IoT Hub.
    • Twin: Demonstrizes Device Twin functionality.
    • File upload: Demonstrates uploading files.
    • Connect with X.509 certificate: Demonstrates authentication using X.509 certificates.
    • Plug and Play: Demonstrates PnP device capabilities.
    • Xamarin: Demonstrates usage within Xamarin applications.
    • Simulated device: Provides a simulated device implementation.
    • Hub routing sample: Demonstrates IoT Hub routing.
    • Simulated device with command: Provides a simulated device that supports commands.
  6. Explore Azure IoT Service SDK for C# samples

    main

    The iothub/service/samples directory contains .NET samples for building backend applications that interact with Azure IoT Hub. These samples demonstrate how to implement various service-side features including device management, messaging, and security. Use these samples to understand the implementation patterns for the following capabilities:

    Device Management & Registry

    • Automatic device management: Automating device lifecycle tasks.
    • Registry manager: Managing device identities in the IoT Hub registry.
    • Import/export devices: Bulk operations for device identities.
    • Import/export devices with managed identity: Performing bulk operations using Azure Managed Identities.
    • Clean up devices: Removing devices from the registry.

    Messaging & Communication

    • Read device-to-cloud messages: Consuming telemetry sent from devices to the hub.
    • Invoke device method: Sending commands to devices via Direct Methods.
    • Device streaming: Real-time streaming of device data.
    • File upload notification receiver: Handling notifications when files are uploaded to blob storage.

    Advanced Services & Security

    • Jobs: Running large-scale operations across many devices.
    • Digital twin clients: Interacting with Azure IoT Digital Twins.
    • Plug and Play services: Implementing PnP-based service logic.
    • Azure SAS credential authentication: Using Shared Access Signature credentials.
    • Role-based authentication: Implementing security using Azure RBAC.
    • Service client: Basic patterns for using the service client.
  7. Note on Preview packages and Device Streaming

    main

    When using the SDK, keep the following in mind regarding package versions:

    • Preview vs Stable: Preview builds contain preview features but may include breaking changes. It is not recommended for production.
    • Device Streaming: This feature is no longer being actively developed and is not included in newer preview releases. If you require device streaming, you must use specific older versions:
      • Microsoft.Azure.Devices.Client - 1.32.0-preview-001
      • Microsoft.Azure.Devices - 1.28.0-preview-001
    • Version Independence: Stable and preview versions are not interdependent. A feature previewed in 1.25.0-preview-001 is not guaranteed to be in the stable 1.25.0 release.
  8. How the IoT Hub transport pipeline works

    main

    The SDK uses a Chain of Responsibility pattern to process requests through a transport pipeline.

    When a client performs an operation, the request passes through a series of handlers. Most elements in the pipeline are responsible for altering or augmenting the request (e.g., adding retries, handling errors, or routing protocols). The final element in the pipeline is the TransportHandler, which is responsible for converting the IoT Hub request into the specific selected protocol and performing the actual network I/O.

  9. Understand the IoT Hub Device SDK transport pipeline

    main

    The IoT Hub Device SDK uses a 'chain of responsibility' pipeline architecture to manage communication between the application and the IoT Hub. The pipeline consists of several delegating handlers that process requests and responses in sequence before reaching the final TransportHandler (MQTT, AMQP, or HTTP).

    Key Pipeline Components:

    • DefaultDelegatingHandler: Routes messages to the next handler in the chain.
    • RetryDelegatingHandler: The central component for state management and reliability. It handles connection state (open/closed), manages subscriptions (Methods, Twin, Module Events), and implements the RetryStrategy. It performs implicit opens (e.g., calling SendEventAsync before OpenAsync) and manages reconnections.
    • ErrorDelegatingHandler: Adapts non-Azure IoT Hub errors (like SocketException) into IoTHubException types and logs application-level exceptions.
    • ProtocolRoutingDelegatingHandler: Selects and maintains the transport protocol (via ITransportSettings) for the lifetime of the client.
    • TransportHandler: The final, stateless layer that implements the specific protocol (MQTT, AMQP, or HTTP).
  10. TLS version behavior in .NET Framework 4.5.1

    main

    Clients using .NET Framework 4.5.1 do not have the 'let the OS decide' option. In this specific environment, the SDK is hard-coded to use TLS 1.2 only.

    Warning: .NET Framework 4.5.1 is no longer supported and does not receive security updates. It is highly recommended to migrate to a supported .NET version (such as .NET 6+ or an LTS version) to ensure security compliance.