You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -39,29 +38,9 @@ This article shows you how to develop two types of applications:
39
38
40
39
## Prerequisites
41
40
42
-
***An IoT hub**. Some SDK calls require the IoT Hub primary connection string, so make a note of the connection string.
41
+
***An IoT hub**
43
42
44
-
***A registered device**. Some SDK calls require the device primary connection string, so make a note of the connection string.
45
-
46
-
***IoT Hub service connection string**
47
-
48
-
In this article, you create a back-end service that adds desired properties to a device twin and then queries the identity registry to find all devices with reported properties that have been updated accordingly. Your service needs the **service connect** permission to modify desired properties of a device twin, and it needs the **registry read** permission to query the identity registry. There is no default shared access policy that contains only these two permissions, so you need to create one.
49
-
50
-
To create a shared access policy that grants **service connect** and **registry read** permissions and get a connection string for this policy, follow these steps:
51
-
52
-
1. In the Azure portal, select **Resource groups**. Select the resource group where your hub is located, and then select your hub from the list of resources.
53
-
54
-
1. On the left-side pane of your hub, select **Shared access policies**.
55
-
56
-
1. From the top menu above the list of policies, select **Add shared policy access policy**.
57
-
58
-
1. In the **Add shared access policy** pane on the right, enter a descriptive name for your policy, such as "serviceAndRegistryRead". Under **Permissions**, select **Registry Read** and **Service Connect**, and then select **Add**.
59
-
60
-
1. Select your new policy from the list of policies.
61
-
62
-
1. Select the copy icon for the **Primary connection string** and save the value.
63
-
64
-
For more information about IoT Hub shared access policies and permissions, see [Control access to IoT Hub with shared access signatures](/azure/iot-hub/authenticate-authorize-sas).
43
+
***A registered device**
65
44
66
45
* If your application uses the MQTT protocol, make sure that **port 8883** is open in your firewall. The MQTT protocol communicates over port 8883. This port may be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see [Connecting to IoT Hub (MQTT)](../iot/iot-mqtt-connect-to-iot-hub.md#connecting-to-iot-hub).
Use [SetMethodHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setmethodhandlerasync) to initialize a callback listenermethod.
63
+
Use [SetMethodHandlerAsync](/dotnet/api/microsoft.azure.devices.client.deviceclient.setmethodhandlerasync) to initialize a direct method callback listener. The listener is associated with a method name keyword, such as "reboot". The method name can be used in an IoT Hub or backend application to trigger the callback method on the device.
65
64
66
-
This example sets up a callback listener named `onReboot`.
65
+
This example sets up a callback listener named `onReboot` that will trigger when the "reboot" direct method name is called.
67
66
68
67
```csharp
69
68
try
@@ -86,7 +85,7 @@ catch (Exception ex)
86
85
}
87
86
```
88
87
89
-
In this example, the `onReboot` callback method implements the direct method on the device:
88
+
In this example, the `onReboot` callback method implements the direct method on the device. This code updates reported properties related to a simulated device reboot. The reported properties can be read and verified by an IoT Hub or backend application, as demonstrated in the **Create a backend application** section of this article.
This section describes how to initiate a remote reboot on a device using a direct method. The app uses device twin queries to discover the last reboot time for that device.
142
137
143
-
The [ServiceManager](/dotnet/api/microsoft.azure.devices.serviceclient) class exposes all methods required to create a backend application to send messages to devices.
138
+
The [ServiceClient](/dotnet/api/microsoft.azure.devices.serviceclient) class exposes all methods required to create a backend application to send messages to devices.
144
139
145
140
### Required service NuGet package
146
141
@@ -179,7 +174,7 @@ To invoke a method on a device:
179
174
1. Create a [CloudToDeviceMethod](/dotnet/api/microsoft.azure.devices.cloudtodevicemethod) object. Pass the device direct method name as a parameter.
180
175
1. Call [InvokeDeviceMethodAsync](/dotnet/api/microsoft.azure.devices.serviceclient.invokedevicemethodasync?#microsoft-azure-devices-serviceclient-invokedevicemethodasync(system-string-microsoft-azure-devices-cloudtodevicemethod-system-threading-cancellationtoken)) to invoke the method on the device.
181
176
182
-
This example calls the "reboot" method to initiate a reboot on the device. The "reboot" method is mapped to a listener on the device as described in the **Create a direct method callback** section above.
177
+
This example calls the "reboot" method to initiate a reboot on the device. The "reboot" method is mapped to a listener on the device as described in the **Create a direct method callback** section of this article.
Console.WriteLine("Invoked firmware update on device.");
194
189
```
195
190
196
-
This example gets the device twin for the rebooting device and outputs the reported properties. This output shows that the `onReboot`method callback has updated the `lastReboot`, `Reboot`, and `iothubDM` reported properties.
191
+
This example gets the device twin for the rebooting device and outputs the reported properties. This output shows that the `onReboot` callback method updated the `lastReboot`, `Reboot`, and `iothubDM` reported properties.
0 commit comments