How To Bulk Import IoT Devices In Azure IoT Hub Using C#

Introduction

 
Azure IoT Hub is a cloud gateway which supports open source SDKs and multiple protocols. IoT Hub communicates both ways - devices to cloud and cloud to devices. The IoT Hub monitoring helps to check device health, failures, and device connection status.
 
Please read the previous parts of the series before continuing with this one.
  1. How To Create Azure IoT Hub Using PowerShell
  2. How to Register IoT Device in Azure IoT Hub Using PowerShell
  3. How To Send Telemetry From An IoT Device To The Azure IoT Hub Using C#
  4. How To Configure Message Routing With IoT Hub - Part One
  5. How To Configure Message Routing With IoT Hub - Part Two
  6. How To Setup Diagnostic Logs With An IoT Hub

Creating a C# Console Application

  • Open Visual Studio -> choose to create a new project.
    How To Bulk Import IoT Devices In Azure IoT Hub Using C#
  • Select Console App (.NET Core) and click "Next".
How To Bulk Import IoT Devices In Azure IoT Hub Using C#
  • Enter the project name, location, and solution name.
  • Click "Create".
How To Bulk Import IoT Devices In Azure IoT Hub Using C#

Installing Microsoft.Azure.Devices NuGet package

  • Go to Project -> Manage NuGet Packages.
  • Click the "Browse" tab and search for Microsoft.Azure.Devices. You will see Microsoft.Azure.Devices SDK listed in the search results; click the "Install" button.
  • Now, click the "I Accept" button to accept the license agreement.
  • It will take a few minutes to install the SDK in our project.
How To Bulk Import IoT Devices In Azure IoT Hub Using C#

Get IoT Hub Connection string from Azure IoT Hub

  • Open your Azure portal and choose your IoT Hub.
  • Click "Shared access policies" in "Settings". You can see the list of policies to the IoT Hub.
  • Double click the iothubowner policy and copy the connection string from the Shared access key.
How To Bulk Import IoT Devices In Azure IoT Hub Using C#
  • Here, I have used RegistryManager class. It contains methods that services can use to perform create, update, and delete operations on devices.
  • Also, we have used the AddDeviceAsync() method to create bulk IoT devices.
Open the program.cs file and type the below code.
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Microsoft.Azure.Devices;  
  4. using Microsoft.Azure.Devices.Common.Exceptions;  
  5.   
  6. namespace IoTDeviceBulkImpExp  
  7. {  
  8.     class Program  
  9.     {  
  10.         private static RegistryManager registryManager;  
  11.   
  12.         private static string connectionString = "Enter the IoT Hub Connection String";  
  13.                   
  14.         static async Task Main(string[] args)  
  15.         {  
  16.   
  17.             registryManager = RegistryManager.CreateFromConnectionString(connectionString);  
  18.   
  19.             string deviceId = "device";  
  20.   
  21.             for (int i = 0; i < 10; i++)  
  22.             {  
  23.                 string newDeviceId =  deviceId + i;  
  24.   
  25.                 await AddDeviceAsync(newDeviceId);  
  26.   
  27.             }  
  28.         }  
  29.   
  30.   
  31.         private async static Task AddDeviceAsync(string deviceId)  
  32.         {  
  33.             //string deviceId = "DeviceThree";  
  34.             Device device;  
  35.             try  
  36.             {  
  37.                 Console.WriteLine("New device:");  
  38.   
  39.                 var d = new Device(deviceId);  
  40.   
  41.                 device = await registryManager.AddDeviceAsync(d);  
  42.             }  
  43.             catch (DeviceAlreadyExistsException)  
  44.             {  
  45.                 Console.WriteLine("Already existing device:");  
  46.                 device = await registryManager.  
  47.                 GetDeviceAsync(deviceId);  
  48.             }  
  49.   
  50.             Console.WriteLine("Generated device key: {0}",  
  51.             device.Authentication.SymmetricKey.PrimaryKey);  
  52.         }  
  53.     }  
  54. }  
Run the project using F5 function key.
 
How To Bulk Import IoT Devices In Azure IoT Hub Using C#
  • Now, go to the Azure Portal and check the new IoT devices that are created.
How To Bulk Import IoT Devices In Azure IoT Hub Using C#
 
That’s it. I hope you have learned how to bulk import the IoT devices in Azure IoT Hub programmatically using C#. Feel free to fill up the comment box below if you need any further assistance.