Skip to content

Develop and Debug with IoT Edge Simulator tool (iotedgehubdev) with C#

Wei Shen edited this page Dec 3, 2019 · 6 revisions

Prerequisites

  • Docker CE
  • Visual Studio Code configured with Azure IoT Tools
  • iotedgehubdev
  • .NET Core 2.1 SDK
  • C# for Visual Studio Code extension

Create an Edge Solution Project

  1. View > Command Palette
  2. In the command palette, enter Azure IoT Edge: New IoT Edge solution. FOllow the prompts in the command palette to create your C# solution.
Field Value
Select folder Choose the location on your development machine for VS Code to create the solution files.
Provide a solution name Enter a descriptive name for your solution or accept the default EdgeSolution.
Select module template Choose C# Module.
Provide a module name Name your module CSharpModule.
Provide Docker image repository for the module An image repository includes the name of your container registry and the name of your container image. Your container image is prepopulated from the name you provided in the last step. Replace localhost:5000 with the login server value from your Azure container registry. You can retrieve the login server from the Overview page of your container registry in the Azure portal. The final image repository looks like <registry name>.azurecr.io/csharpmodule.

Folder structure

Add your registry credentials

The environment file(.env) stores the credentials for your container registry. The credential is needed for IoT Edge runtime to get the module from container registry after deploying the solution to Edge devices.

  1. In the VS Code explorer, open the .env file.
  2. Update the fields with the username and password values from your Azure container registry.
  3. Save this file

Select your target architecture

Currently, Visual Studio Code can develop C# modules for Linux AMD64 and Linux ARM32v7 devices. You need to select which architecture you're targeting with each solution, because the container is built and run differently for each architecture type. The default is Linux AMD64.

  1. Open the command palette and search for Azure IoT Edge: Set Default Target Platform for Edge Solution, or select the shortcut icon in the side bar at the bottom of the window.
  2. In the command palette, select the target architecture from the list of options. Here we will use AMD64 as sample.

If you are targeting the Linux ARM32v7, please refer to this doc to setup the remote docker host to an ARM32v7 machine.

Update the module with custom code

  1. Open modules > CSharpModule > Program.cs. This file contains the sample methods to communicate with edgeHub.
  2. Edit the source code or add more source files.
  3. Update modules > CSharpModule > Dockerfile.amd64 and modules > CSharpModule > Dockerfile.amd64.debug if necessary.
  4. If docker build arguments needs to be updated please update modules > CSharpModule > module.json. Please refer to the doc for the description of module.json file.
  5. Add or update the module twin of CSharpModule in deployment.template.json and deployment.debug.template.json

Build and run the solution project with simulator

Single module debug

Clone this wiki locally