我有一个使用 .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 设备中,但模块运行时状态显示错误,日志没有显示任何内容。
以下是在 Windows IoT Edge 项目上下文中使用 Windows 容器设置和调试 C/C# 模块的步骤:
File
-> New
-> Project
。Azure IoT Edge (Windows amd64)
下的 Azure IoT
并提供项目名称,然后单击 OK
。"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
。
如果使用 Azure 容器注册表等私有注册表,请使用
docker login
命令登录。
在 Edge 项目下的
.env
文件中输入注册表的凭据。
生成部署清单
deployment.windows-amd64.debug.json
。
使用 VS 的开发者命令提示符运行
BuildAndPush.cmd
脚本来构建并推送镜像。
部署到物联网边缘设备:
使用 Cloud Explorer 将生成的清单部署到 IoT Edge 设备。
使用
docker exec
命令在远程 Windows 计算机上手动启动远程调试器。
在 Visual Studio 中,转到
Debug
-> Attach to Process
。
将
Connection Type
设置为 Remote (no authentication)
。
将
Connection target
设置为 [IP Address]:4022
。
选择要调试的进程:
IoTEdgeModule1.exe
并“附加到”本机代码。dotnet.exe
并“附加到”到托管 (CoreCLR)。设置断点并调试您的 C/C# 模块。
确保根据您的设置将
[Your Registry Server]
、[Your Edge Module Container ID]
和 [IP Address]
等占位符替换为适当的值。另外,请确保您拥有 Docker、Visual Studio 和 Azure 服务所需的权限和配置。