| author | SoniaLopezBravo | |||||
|---|---|---|---|---|---|---|
| ms.author | sonialopez | |||||
| ms.service | azure-iot-hub | |||||
| services | iot-hub | |||||
| ms.devlang | csharp | |||||
| ms.topic | include | |||||
| ms.date | 03/28/2025 | |||||
| ms.custom |
|
The quickstart uses two prewritten .NET applications:
-
A simulated device application that responds to direct methods called from a service application. To receive the direct method calls, this application connects to a device-specific endpoint on your IoT hub.
-
A service application that calls the direct methods on the simulated device. To call a direct method on a device, this application connects to service-side endpoint on your IoT hub.
-
An Azure account with an active subscription. Create one for free.
-
The two sample applications you run in this quickstart are written using C#. You need the .NET SDK 6.0 or greater on your development machine.
You can download the .NET Core SDK for multiple platforms from .NET.
You can verify the current version of C# on your development machine using the following command:
dotnet --version -
Clone or download the Microsoft Azure IoT SDK for .NET from GitHub. The sample applications used by this quickstart are included in the SDK.
-
Make sure that port 8883 is open in your firewall. The device sample in this quickstart uses MQTT protocol, which communicates over port 8883. This port might be blocked in some corporate and educational network environments. For more information and ways to work around this issue, see the Connect to IoT Hub section of Communicate with an IoT hub using the MQTT protocol.
[!INCLUDE azure-cli-prepare-your-environment.md]
[!INCLUDE iot-hub-cli-version-info]
[!INCLUDE iot-hub-include-create-hub-cli]
You also need your IoT hub's service connection string to enable the service application to connect to the hub and retrieve the messages. The service connection string is for your IoT hub as a whole, and is different from the device connection string you retrieved in the previous section.
The following command retrieves the service connection string for your IoT hub:
az iot hub connection-string show --policy-name service --hub-name {YourIoTHubName} --output table
Make a note of the service connection string, which looks like:
HostName={YourIoTHubName}.azure-devices.net;SharedAccessKeyName=service;SharedAccessKey={YourSharedAccessKey}
You use this value later in the quickstart.
The simulated device application connects to a device-specific endpoint on your IoT hub, sends simulated telemetry, and listens for direct method calls from your hub. In this quickstart, the direct method call from the hub tells the device to change the interval at which it sends telemetry. The simulated device sends an acknowledgment back to your hub after it executes the direct method.
-
In a local terminal window, navigate to the root folder of the SDK. Then, navigate to the iothub\device\samples\getting started\SimulatedDeviceWithCommand folder.
-
Run the following command to install the required packages for the simulated device application:
dotnet restore -
Run the following command to build and run the simulated device application.
{DeviceConnectionString}: Replace this placeholder with the device connection string you noted previously.dotnet run -- -c "{DeviceConnectionString}"The following screenshot shows the output as the simulated device application sends telemetry to your IoT hub:
:::image type="content" source="./media/quickstart-control-device-dotnet/SimulatedDevice-1.png" alt-text="Screenshot of a terminal window that shows the output of the simulated device application.":::
The service application connects to a service-side endpoint on your IoT Hub. The application makes direct method calls to a device through your IoT hub and listens for acknowledgments. An IoT Hub service application typically runs in the cloud.
-
In another local terminal window, navigate to the root folder of the SDK. Then, navigate to the iothub\service\samples\getting started\InvokeDeviceMethod folder.
-
In the local terminal window, run the following commands to install the required libraries for the service application:
dotnet build -
In the local terminal window, run the following commands to build and run the service application.
{ServiceConnectionString}: Replace this placeholder with the IoT Hub service connection string you noted previously.{DeviceName}: Replace this placeholder with the name of the device you registered.dotnet run -- -c "{ServiceConnectionString}" -d {DeviceName}The following screenshot shows the output as the application makes a direct method call to the device and receives an acknowledgment:
:::image type="content" source="./media/quickstart-control-device-dotnet/BackEndApplication.png" alt-text="Screenshot of a terminal window that shows the result of the direct method call from the service application.":::
After you run the service application, you see a message in the local terminal window running the simulated device, and the rate at which it sends messages changes:
:::image type="content" source="./media/quickstart-control-device-dotnet/SimulatedDevice-2.png" alt-text="Screenshot of a terminal window that shows the direct message result and updated output from the simulated device application.":::