Search code examples
azureazure-sdk-.netazure-iot-sdk

Azure iot deviceId validation from Azure SDK


We use Azure SDK to create Resource groups, iot hubs and devices.

For example:

iotHubDescription = await iotHubClient.IotHubResource.CreateOrUpdateAsync(resourceGroupName, iotHubName,
            iotHubDescription);

or

var device = await registryManager.AddDeviceAsync(new Device(azureDevice.DeviceId));

I've found how to validate Iot Hub name before create it:

var info = await iotHubClient.IotHubResource.CheckNameAvailabilityAsync(new OperationInputs(iotHubName));

but can't find how to validate device id.

So, question is: How to validate Iot device id from Azure SDK?


Solution

  • You need to catch using the Exception,

    try
    {
       simulatedDevice = await registryManager.AddDeviceAsync(new Device(simulatedDeviceId));}
       catch (DeviceAlreadyExistsException)
       {
        simulatedDevice = await registryManager.GetDeviceAsync(simulatedDeviceId);
        Console.WriteLine("Retrieving existing device id");
       }
    }