Turn .Net 6 Background Service app as IoT Edge Module

43 views Asked by At

I have an Background Service app which is created using .Net 6 and I want this service to run in edge client machines (Windows and Linux), the solution what I'm thinking of is to create an IoT Edge Module and run this in edge devices.

What are the possibilities to achieve this?

public class Worker : BackgroundService
{
     private readonly ILogger\<Worker\> \_logger;
     private readonly IBackgroundService \_backgroundService;
     public Worker(ILogger<Worker> logger, IBackgroundService backgroundService)
     {
         _logger = logger;
         _backgroundService= backgroundService;
     }
    
     protected override async Task ExecuteAsync(CancellationToken stoppingToken)
     {
         _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
         _backgroundService.Start();
         await Task.Delay(Timeout.InfiniteTimeSpan, stoppingToken);
     }
}

I have containerized the existing service and deployed the module in the Azure IoT Edge device, but the module runtime status shows Error and the logs shows nothing.

enter image description here

1

There are 1 answers

0
Sampath On

Below are the steps for setting up and debugging C/C# modules using Windows containers in the context of the Windows IoT Edge project:

  • Open Visual Studio and click on File -> New -> Project.
  • Select Azure IoT Edge (Windows amd64) under Azure IoT and provide a name for your project, then click OK.

enter image description here

  • Add Dockerfiles for C# modules with remote debug support to the project.

enter image description here

"systemModules": {
    "edgeAgent": {
    //...
        "image": "mcr.microsoft.com/azureiotedge-agent:1.4",
    //...
    "edgeHub": {
    //...
        "image": "mcr.microsoft.com/azureiotedge-hub:1.4",
    //...

  • Add a script to build and push module images. Update the script to push images to your own repository.

  • Update module.json to ensure the AzureIoTEdgeApp1 project can find the Dockerfile.windows-amd64.debug file.

  • Edit deployment.template.json to expose port 4022 for debugging.

  • Right-click AzureIotEdgeApp1 project, go to Properties, and change Target Configuration to Debug.

enter image description here

  • If using a private registry like Azure Container Registry, use docker login command to sign in.

  • Input credentials for your registry in the .env file under Edge project.

  • Generate deployment manifest deployment.windows-amd64.debug.json.

  • Code taken from git

  • Use Developer Command Prompt for VS to run BuildAndPush.cmd script to build and push the image.

Deploy to IoT Edge Device:

enter image description here

  • Use Cloud Explorer to deploy the generated manifest to the IoT Edge device. enter image description here

  • Manually start the remote debugger on the remote Windows machine using the docker exec command.

enter image description here

  • In Visual Studio, go to Debug -> Attach to Process.

  • Set Connection Type to Remote (no authentication).

  • Set Connection target to [IP Address]:4022.

  • Choose the process to debug:

    • For C module, choose process IoTEdgeModule1.exe and “Attach to” to Native code.
    • For C# module, choose process dotnet.exe and “Attach to” to Managed (CoreCLR).
  • Set breakpoints and debug your C/C# module.

Make sure to replace placeholders like [Your Registry Server], [Your Edge Module Container ID], and [IP Address] with appropriate values according to your setup. Also, ensure that you have the necessary permissions and configurations for Docker, Visual Studio, and your Azure services.