使用添加设备异步方法将设备添加到 Azure IoT 中心时出现错误"the device id is invalid"



当我的项目将设备添加为设备 ID 时,我使用了 RegisterManager.AddDevicesAsync 方法,包括"I(字母("。并且发生了异常。设备 ID 是否对此问题有任何约束?

public async Task AddDeviceAsync(DeviceConfig deviceConfig)
{
try
{
DeviceStatus status;
if (!Enum.TryParse(deviceConfig.Status, true, out status))
{
status = DeviceStatus.Enabled;
}
var d = new Device(deviceConfig.DeviceId)
{
Status = status
};
await this.registryManager.AddDeviceAsync(d);
}
catch (ArgumentException ex)
{
this.logger.LogError(ex.Message);
throw new EVCException(ex.Message);
}
catch (DeviceAlreadyExistsException ex)
{
this.logger.LogInformation(ex.Message);
}
} 
await this.deviceManager.AddDeviceAsync(new DeviceConfig { DeviceId = "ILICA"});

AddDeviceAsync()期望一个Device对象,而不是一个DeviceConfig,你可能想要类似以下内容的东西,我猜:

await this.deviceManager.AddDeviceAsync(d);

你可以尝试使用DeviceClient.CreateFromConnectionString

using Microsoft.Azure.Devices.Client; // namespace
using DeviceClient device = DeviceClient.CreateFromConnectionString("connectionstring"); 

最新更新