static DeviceDataAuth GetMeADevice(IotHubApiProxyClient i) { string result = null; try { result = i.Device.GetById("1234567"); } catch (HttpOperationException e) { if (e.Response.StatusCode != System.Net.HttpStatusCode.NotFound) { throw e; } } if (null != result) { Console.WriteLine("Found existing device!"); DeviceData data = JsonConvert.DeserializeObject <DeviceData>(result); Console.WriteLine(JsonConvert.SerializeObject(data)); Console.WriteLine("Deleting existing device!"); i.Device.DeleteById("1234567"); } string v = i.Device.PostById("1234567"); return(JsonConvert.DeserializeObject <DeviceDataAuth>(v)); }
static void Main(string[] args) { IotHubApiProxyClient i = new IotHubApiProxyClient(); i.BaseUri = new Uri("<https url of your web service>"); i.Credentials = new ApiKeyCredentials("1234"); // replace with your access key Console.WriteLine("Creating device in iot hub"); DeviceDataAuth auth = GetMeADevice(i); Console.WriteLine(auth.Id); Console.WriteLine(auth.ConnectionString); Console.WriteLine("Created."); Console.WriteLine("press any key..."); Console.ReadKey(); Console.WriteLine("Connecting device in iot hub"); //try //{ deviceClient = DeviceClient.CreateFromConnectionString(auth.ConnectionString, TransportType.Http1); Console.WriteLine("Connected..."); Console.WriteLine("press any key..."); Console.ReadKey(); SendEvent().Wait(); ReceiveCommands().Wait(); Console.WriteLine("Exited!\n"); //} //catch (Exception ex) //{ // Console.WriteLine("Error in sample: {0}", ex.Message); //} Console.WriteLine("press any key..."); Console.ReadKey(); Console.WriteLine("Deleting device"); i.Device.DeleteById("1234567"); Console.WriteLine("Deleted"); }