Manikandan Murugesan

Manikandan Murugesan

  • 133
  • 14.2k
  • 430.7k

How to create a device identity for IotHub in VS-17

Jun 14 2017 10:25 PM
I am beginner of IOT and i learning to creating the app with IotHub by seeing the tutorial from this link:
 https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-csharp-getstarted
i created the IotHub using Azure but I can't create an identity of my device.
  Here's my code : 
   
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Microsoft.Azure.Devices;  
  4. using Microsoft.Azure.Devices.Common.Exceptions;  
  5. namespace DeviceIdentity  
  6. {  
  7.     class Program  
  8.     {  
  9.         static  RegistryManager Registrymanager;  
  10.         static string connectionString = "{iot hub connection string}";  
  11.         private static async Task AddDeviceAsync()  
  12.         {  
  13.             string deviceId = "myFirstdevice";  
  14.             Device device;  
  15.             try  
  16.             {  
  17.                 device = await Registrymanager.AddDeviceAsync(new Device(deviceId));  
  18.             }  
  19.             catch (DeviceAlreadyExistsException)  
  20.             {  
  21.                 device = await Registrymanager.GetDeviceAsync(deviceId);  
  22.             }  
  23.             Console.WriteLine("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey);  
  24.         }  
  25.         static void Main(string args[])  
  26.         {  
  27.               Registrymanager =RegistryManager.CreateFromConnectionString(connectionString);  
  28.             AddDeviceAsync().Wait();  
  29.             Console.ReadLine();  
  30.         }  
  31.     }  


Answers (1)