Skip to content

Commit 8983624

Browse files
committed
Added cert and Entra auth steps
1 parent ef5b9a8 commit 8983624

5 files changed

+292
-121
lines changed

articles/iot-hub/how-to-cloud-to-device-messaging.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ author: kgremban
66
ms.author: kgremban
77
ms.service: azure-iot-hub
88
ms.topic: how-to
9-
ms.date: 06/20/2024
9+
ms.date: 12/19/2024
1010
zone_pivot_groups: iot-hub-howto-c2d-1
1111
ms.custom: [amqp, mqtt, "Role: Cloud Development", "Role: IoT Device"]
1212
---

includes/iot-hub-howto-cloud-to-device-messaging-dotnet.md

Lines changed: 81 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,51 @@ ms.author: kgremban
44
ms.service: azure-iot-hub
55
ms.devlang: csharp
66
ms.topic: include
7-
ms.date: 06/20/2024
7+
ms.date: 12/19/2024
88
ms.custom: [amqp, mqtt, "Role: Cloud Development", "Role: IoT Device", devx-track-csharp, devx-track-dotnet]
99
---
1010

11-
## Receive cloud-to-device messages
11+
## Overview
1212

13-
This section describes how to receive cloud-to-device messages using the [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class in the Azure IoT SDK for .NET.
13+
This article describes how to use the [Azure IoT SDK for .NET](https://github.com/Azure/azure-iot-sdk-csharp/blob/main/readme.md) to create device and backend service application code to receive and send cloud-to-device messages.
14+
15+
## Create a device application
16+
17+
This section describes how to receive cloud-to-device messages.
1418

1519
There are two options that a device client application can use to receive messages:
1620

17-
* **Polling**: The device application checks for new IoT Hub messages using a code loop (for example, a `while` or `for` loop). The loop executes continually, checking for messages.
1821
* **Callback**: The device application sets up an asynchronous message handler method that is called immediately when a message arrives.
22+
* **Polling**: The device application checks for new IoT Hub messages using a code loop (for example, a `while` or `for` loop). The loop executes continually, checking for messages.
1923

20-
### Declare a DeviceClient object
24+
[!INCLUDE [iot-authentication-device-connection-string.md](iot-authentication-device-connection-string.md)]
2125

22-
[DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) includes methods and properties necessary to receive messages from IoT Hub.
26+
### Required device NuGet Package
2327

24-
For example:
28+
Device client applications written in C# require the **Microsoft.Azure.Devices.Client** NuGet package.
29+
30+
Add these `using` statements to use the device library.
2531

2632
```csharp
27-
static DeviceClient deviceClient;
33+
using Microsoft.Azure.Devices.Client;
34+
using Microsoft.Azure.Devices.Shared;
2835
```
2936

37+
### Connect a device to IoT Hub
38+
39+
A device app can authenticate with IoT Hub using the following methods:
40+
41+
* X.509 certificate
42+
* Shared access key
43+
44+
#### Authenticate using an X.509 certificate
45+
46+
[!INCLUDE [iot-hub-howto-auth-device-cert-dotnet](iot-hub-howto-auth-device-cert-dotnet.md)]
47+
48+
#### Authenticate using a shared access key
49+
50+
The [DeviceClient](/dotnet/api/microsoft.azure.devices.client.deviceclient) class exposes all the methods required to receive messages on the device.
51+
3052
### Supply the connection parameters
3153

3254
Supply the IoT Hub primary connection string and Device ID to `DeviceClient` using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.client.deviceclient.createfromconnectionstring) method. In addition to the required IoT Hub primary connection string, the `CreateFromConnectionString` method can be overloaded to include these *optional* parameters:
@@ -35,12 +57,38 @@ Supply the IoT Hub primary connection string and Device ID to `DeviceClient` usi
3557
* `transportSettings` - Interface used to define various transport-specific settings for `DeviceClient` and `ModuleClient`. For more information, see [ITransportSettings Interface](/dotnet/api/microsoft.azure.devices.client.itransportsettings).
3658
* `ClientOptions` - Options that allow configuration of the device or module client instance during initialization.
3759

38-
This example calls `CreateFromConnectionString` to define the `DeviceClient` connection IoT hub and device ID settings.
60+
This example connects to a device using the `Mqtt` transport protocol.
3961

40-
``` csharp
41-
static string connectionString = "{your IoT hub connection string}";
42-
static string deviceID = "{your device ID}";
43-
deviceClient = DeviceClient.CreateFromConnectionString(connectionString,deviceID);
62+
```csharp
63+
static string DeviceConnectionString = "{IoT hub device connection string}";
64+
static deviceClient = null;
65+
deviceClient = DeviceClient.CreateFromConnectionString(DeviceConnectionString,
66+
TransportType.Mqtt);
67+
```
68+
69+
### Callback
70+
71+
To receive callback cloud-to-device messages in the device application, the application must connect to the IoT Hub and set up a callback listener to process incoming messages. Incoming messages to the device are received from the IoT Hub message queue.
72+
73+
Using callback, the device application sets up a message handler method using [SetReceiveMessageHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setreceivemessagehandlerasync). The message handler is called then a message is received. Creating a callback method to receive messages removes the need to continuously poll for received messages.
74+
75+
Callback is available only using these protocols:
76+
77+
* `Mqtt`
78+
* `Mqtt_WebSocket_Only`
79+
* `Mqtt_Tcp_Only`
80+
* `Amqp`
81+
* `Amqp_WebSocket_Only`
82+
* `Amqp_Tcp_only`
83+
84+
The `Http1` protocol option does not support callbacks since the SDK methods would need to poll for received messages anyway, which defeats the callback principle.
85+
86+
In this example, `SetReceiveMessageHandlerAsync` sets up a callback handler method named `OnC2dMessageReceivedAsync`, which is called each time a message is received.
87+
88+
```csharp
89+
// Subscribe to receive C2D messages through a callback (which isn't supported over HTTP).
90+
await deviceClient.SetReceiveMessageHandlerAsync(OnC2dMessageReceivedAsync, deviceClient);
91+
Console.WriteLine($"\n{DateTime.Now}> Subscribed to receive C2D messages over callback.");
4492
```
4593

4694
### Polling
@@ -109,31 +157,6 @@ while (!stopPolling)
109157
}
110158
```
111159

112-
### Callback
113-
114-
To receive callback cloud-to-device messages in the device application, the application must connect to the IoT Hub and set up a callback listener to process incoming messages. Incoming messages to the device are received from the IoT Hub message queue.
115-
116-
Using callback, the device application sets up a message handler method using [SetReceiveMessageHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setreceivemessagehandlerasync). The message handler is called then a message is received. Creating a callback method to receive messages removes the need to continuously poll for received messages.
117-
118-
Callback is available only using these protocols:
119-
120-
* `Mqtt`
121-
* `Mqtt_WebSocket_Only`
122-
* `Mqtt_Tcp_Only`
123-
* `Amqp`
124-
* `Amqp_WebSocket_Only`
125-
* `Amqp_Tcp_only`
126-
127-
The `Http1` protocol option does not support callbacks since the SDK methods would need to poll for received messages anyway, which defeats the callback principle.
128-
129-
In this example, `SetReceiveMessageHandlerAsync` sets up a callback handler method named `OnC2dMessageReceivedAsync`, which is called each time a message is received.
130-
131-
```csharp
132-
// Subscribe to receive C2D messages through a callback (which isn't supported over HTTP).
133-
await deviceClient.SetReceiveMessageHandlerAsync(OnC2dMessageReceivedAsync, deviceClient);
134-
Console.WriteLine($"\n{DateTime.Now}> Subscribed to receive C2D messages over callback.");
135-
```
136-
137160
### Receive message retry policy
138161

139162
The device client message retry policy can be defined using [DeviceClient.SetRetryPolicy](/dotnet/api/microsoft.azure.devices.client.deviceclient.setretrypolicy).
@@ -144,35 +167,46 @@ The message retry timeout is stored in the [DeviceClient.OperationTimeoutInMilli
144167

145168
The .NET/C# SDK includes a [Message Receive](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/device/samples/getting%20started/MessageReceiveSample) sample that includes the receive message methods described in this section.
146169

147-
## Send cloud-to-device messages
170+
## Create a backend application
148171

149172
This section describes essential code to send a message from a solution backend application to an IoT device using the [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class in the Azure IoT SDK for .NET. As discussed previously, a solution backend application connects to an IoT Hub and messages are sent to IoT Hub encoded with a destination device. IoT Hub stores incoming messages to its message queue, and messages are delivered from the IoT Hub message queue to the target device.
150173

151174
A solution backend application can also request and receive delivery feedback for a message sent to IoT Hub that is destined for device delivery via the message queue.
152175

153-
### Declare a ServiceClient object
176+
### Add service NuGet Package
154177

155-
[ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) includes methods and properties necessary to send messages from an application through IoT Hub to a device.
178+
Backend service applications require the **Microsoft.Azure.Devices** NuGet package.
156179

157-
``` csharp
158-
static ServiceClient serviceClient;
159-
```
180+
### Connect to IoT hub
181+
182+
You can connect a backend service to IoT Hub using the following methods:
183+
184+
* Shared access policy
185+
* Microsoft Entra
186+
187+
[!INCLUDE [iot-authentication-service-connection-string.md](iot-authentication-service-connection-string.md)]
188+
189+
#### Connect using a shared access policy
160190

161191
### Supply the connection string
162192

163-
Supply the IoT Hub primary connection string to `ServiceClient` using the [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.serviceclient.createfromconnectionstring) method. In addition to the required IoT Hub primary connection string, the `CreateFromConnectionString` method can be overloaded to include these *optional* parameters:
193+
Connect a backend application to a device using [CreateFromConnectionString](/dotnet/api/microsoft.azure.devices.serviceclient.createfromconnectionstring). In addition to the required IoT Hub primary connection string, the `CreateFromConnectionString` method can be overloaded to include these *optional* parameters:
164194

165195
* `transportType` - `Amqp` or `Amqp_WebSocket_Only`.
166196
* `transportSettings` - The AMQP and HTTP proxy settings for Service Client.
167197
* `ServiceClientOptions` - Options that allow configuration of the service client instance during initialization. For more information, see [ServiceClientOptions](/dotnet/api/microsoft.azure.devices.serviceclientoptions).
168198

169-
This example creates the `ServiceClient` object using the IoT Hub connection string.
199+
This example creates the `ServiceClient` object using the IoT Hub connection string and default `Amqp` transport.
170200

171201
``` csharp
172-
static string connectionString = "{your iot hub connection string}";
202+
static string connectionString = "{your IoT hub connection string}";
173203
serviceClient = ServiceClient.CreateFromConnectionString(connectionString);
174204
```
175205

206+
#### Connect using Microsoft Entra
207+
208+
[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-dotnet](iot-hub-howto-connect-service-iothub-entra-dotnet.md)]
209+
176210
### Send an asynchronous cloud-to-device message
177211

178212
Use [sendAsync](/dotnet/api/microsoft.azure.devices.serviceclient.sendasync) to send an asynchronous message from an application through the cloud (IoT Hub) to the device. The call is made using the AMQP protocol.

includes/iot-hub-howto-cloud-to-device-messaging-java.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ ms.author: kgremban
44
ms.service: azure-iot-hub
55
ms.devlang: java
66
ms.topic: include
7-
ms.date: 06/20/2024
7+
ms.date: 12/19/2024
88
ms.custom: [amqp, mqtt, devx-track-java, devx-track-extended-java]
99
---
1010

11-
## Receive cloud-to-device messages
11+
## Overview
12+
13+
This how-to contains two sections:
14+
15+
* Receive cloud-to-device messages on a device
16+
* Send cloud-to-device messages from a backend application
17+
18+
## Receive cloud-to-device messages on a device
1219

1320
This section describes how to receive cloud-to-device messages using the [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient) class from the Azure IoT SDK for Java.
1421

@@ -24,7 +31,18 @@ import com.microsoft.azure.sdk.iot.device.exceptions.IotHubClientException;
2431
import com.microsoft.azure.sdk.iot.device.transport.IotHubConnectionStatus;
2532
```
2633

27-
### Declare a DeviceClient object
34+
### Connect a device to IoT Hub
35+
36+
A device app can authenticate with IoT Hub using the following methods:
37+
38+
* X.509 certificate
39+
* Shared access key
40+
41+
#### Authenticate using an X.509 certificate
42+
43+
[!INCLUDE [iot-hub-howto-auth-device-cert-java](iot-hub-howto-auth-device-cert-java.md)]
44+
45+
#### Authenticate using a shared access key
2846

2947
The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#com-microsoft-azure-sdk-iot-device-deviceclient-deviceclient(java-lang-string-com-microsoft-azure-sdk-iot-device-iothubclientprotocol)) object instantiation requires these parameters:
3048

@@ -39,7 +57,7 @@ The [DeviceClient](/java/api/com.microsoft.azure.sdk.iot.device.deviceclient?#co
3957
For example:
4058

4159
```java
42-
static string connectionString = "{a device connection string}";
60+
static string connectionString = "{IOT hub device connection string}";
4361
static protocol = IotHubClientProtocol.AMQPS;
4462
DeviceClient client = new DeviceClient(connectionString, protocol);
4563
```
@@ -146,7 +164,7 @@ client.open(true);
146164

147165
**HandleMessages**: a sample device app included with the [Microsoft Azure IoT SDK for Java](https://github.com/Azure/azure-iot-sdk-java/tree/main/iothub/device/iot-device-samples), which connects to your IoT hub and receives cloud-to-device messages.
148166

149-
## Send cloud-to-device messages
167+
## Send cloud-to-device messages from a backend application
150168

151169
This section describes how to send a cloud-to-device message using the [ServiceClient](/java/api/com.azure.core.annotation.serviceclient) class from the Azure IoT SDK for Java. A solution backend application connects to an IoT Hub and messages are sent to IoT Hub encoded with a destination device. IoT Hub stores incoming messages to its message queue, and messages are delivered from the IoT Hub message queue to the target device.
152170

@@ -174,24 +192,34 @@ import java.io.IOException;
174192
import java.net.URISyntaxException;
175193
```
176194

177-
### Define the connection protocol
195+
### Connect to the IoT Hub
196+
197+
You can connect a backend service to IoT Hub using the following methods:
198+
199+
* Shared access policy
200+
* Microsoft Entra
201+
202+
[!INCLUDE [iot-authentication-service-connection-string.md](iot-authentication-service-connection-string.md)]
203+
204+
#### Connect using a shared access policy
205+
206+
#### Define the connection protocol
178207

179208
Use [IotHubServiceClientProtocol](/java/api/com.microsoft.azure.sdk.iot.service.iothubserviceclientprotocol) to define the application-layer protocol used by the service client to communicate with an IoT Hub.
180209

181210
`IotHubServiceClientProtocol` only accepts the `AMQPS` or `AMQPS_WS` enum.
182211

183212
```java
184-
private static final IotHubServiceClientProtocol protocol =
185-
IotHubServiceClientProtocol.AMQPS;
213+
IotHubServiceClientProtocol protocol = IotHubServiceClientProtocol.AMQPS;
186214
```
187215

188-
### Create the ServiceClient object
216+
#### Create the ServiceClient object
189217

190218
Create the [ServiceClient](/java/api/com.azure.core.annotation.serviceclient) object, supplying the Iot Hub connection string and protocol.
191219

192220
```java
193-
private static final String connectionString = "{yourhubconnectionstring}";
194-
private static final ServiceClient serviceClient (connectionString, protocol);
221+
String connectionString = "{yourhubconnectionstring}";
222+
ServiceClient serviceClient (connectionString, protocol);
195223
```
196224

197225
### Open the connection between application and IoT Hub
@@ -202,6 +230,10 @@ private static final ServiceClient serviceClient (connectionString, protocol);
202230
serviceClient.open();
203231
```
204232

233+
#### Connect using Microsoft Entra
234+
235+
[!INCLUDE [iot-hub-howto-connect-service-iothub-entra-java](iot-hub-howto-connect-service-iothub-entra-java.md)]
236+
205237
### Open a feedback receiver for message delivery feedback
206238

207239
You can use a [FeedbackReceiver](/java/api/com.microsoft.azure.sdk.iot.service.feedbackreceiver) to get sent message delivery to IoT Hub feedback. A `FeedbackReceiver` is a specialized receiver whose `Receive` method returns a `FeedbackBatch` instead of a `Message`.
@@ -254,6 +286,7 @@ if (feedbackBatch != null) {
254286

255287
### SDK send message samples
256288

257-
* [Service client sample](/java/api/overview/azure/iot?example) - Send message example, #1.
289+
There are two send message samples:
258290

291+
* [Service client sample](/java/api/overview/azure/iot?example) - Send message example, #1.
259292
* [Service client sample](https://github.com/Azure/azure-iot-sdk-csharp/tree/main/iothub/service/samples/getting%20started/ServiceClientSample) - Send message example, #2.

0 commit comments

Comments
 (0)