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 :
- using System;
- using System.Threading.Tasks;
- using Microsoft.Azure.Devices;
- using Microsoft.Azure.Devices.Common.Exceptions;
- namespace DeviceIdentity
- {
- class Program
- {
- static RegistryManager Registrymanager;
- static string connectionString = "{iot hub connection string}";
- private static async Task AddDeviceAsync()
- {
- string deviceId = "myFirstdevice";
- Device device;
- try
- {
- device = await Registrymanager.AddDeviceAsync(new Device(deviceId));
- }
- catch (DeviceAlreadyExistsException)
- {
- device = await Registrymanager.GetDeviceAsync(deviceId);
- }
- Console.WriteLine("Generated device key: {0}", device.Authentication.SymmetricKey.PrimaryKey);
- }
- static void Main(string args[])
- {
- Registrymanager =RegistryManager.CreateFromConnectionString(connectionString);
- AddDeviceAsync().Wait();
- Console.ReadLine();
- }
- }
- }