将 .Net 6 后台服务应用程序转变为 IoT Edge 模块

问题描述 投票:0回答:1

我有一个使用 .Net 6 创建的后台服务应用程序,我希望该服务在边缘客户端计算机(Windows 和 Linux)中运行,我想到的解决方案是创建一个 IoT Edge 模块并在边缘设备。

实现这一目标的可能性有哪些?

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);
     }
}

我已将现有服务容器化,并将模块部署到 Azure IoT Edge 设备中,但模块运行时状态显示错误,日志没有显示任何内容。

azure .net-6.0 azure-iot-edge .net-services
1个回答
0
投票

以下是在 Windows IoT Edge 项目上下文中使用 Windows 容器设置和调试 C/C# 模块的步骤:

  • 打开 Visual Studio 并单击
    File
    ->
    New
    ->
    Project
  • 选择
    Azure IoT Edge (Windows amd64)
    下的
    Azure IoT
    并提供项目名称,然后单击
    OK

enter image description here

  • 将具有远程调试支持的 C# 模块的 Dockerfile 添加到项目中。

enter image description here

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

  • 添加脚本来构建和推送模块图像。更新脚本以将图像推送到您自己的存储库。

  • 更新 module.json 以确保 AzureIoTEdgeApp1 项目可以找到 Dockerfile.windows-amd64.debug 文件。

  • 编辑deployment.template.json以公开端口4022以进行调试。

  • 右键单击 AzureIotEdgeApp1 项目,转到

    Properties
    ,然后将
    Target Configuration
    更改为
    Debug

enter image description here

  • 如果使用 Azure 容器注册表等私有注册表,请使用

    docker login
    命令登录。

  • 在 Edge 项目下的

    .env
    文件中输入注册表的凭据。

  • 生成部署清单

    deployment.windows-amd64.debug.json

  • 代码采取来自git

  • 使用 VS 的开发者命令提示符运行

    BuildAndPush.cmd
    脚本来构建并推送镜像。

部署到物联网边缘设备:

enter image description here

  • 使用 Cloud Explorer 将生成的清单部署到 IoT Edge 设备。 enter image description here

  • 使用

    docker exec
    命令在远程 Windows 计算机上手动启动远程调试器。

enter image description here

  • 在 Visual Studio 中,转到

    Debug
    ->
    Attach to Process

  • Connection Type
    设置为
    Remote (no authentication)

  • Connection target
    设置为
    [IP Address]:4022

  • 选择要调试的进程:

    • 对于 C 模块,选择进程
      IoTEdgeModule1.exe
      并“附加到”本机代码。
    • 对于 C# 模块,选择进程
      dotnet.exe
      并“附加到”到托管 (CoreCLR)。
  • 设置断点并调试您的 C/C# 模块。

确保根据您的设置将

[Your Registry Server]
[Your Edge Module Container ID]
[IP Address]
等占位符替换为适当的值。另外,请确保您拥有 Docker、Visual Studio 和 Azure 服务所需的权限和配置。

© www.soinside.com 2019 - 2024. All rights reserved.