private async Task <List <string> > BootstrapDefaultDevices() { List <string> sampleIds = SampleDeviceFactory.GetDefaultDeviceNames(); foreach (string id in sampleIds) { dynamic device = DeviceSchemaHelper.BuildDeviceStructure(id, true); SecurityKeys securityKeys = _securityKeyGenerator.CreateRandomKeys(); try { await _iotHubRepository.AddDeviceAsync(device, securityKeys); await _virtualDeviceStorage.AddOrUpdateDeviceAsync(new InitialDeviceConfig() { DeviceId = DeviceSchemaHelper.GetDeviceID(device), HostName = _configProvider.GetConfigurationSettingValue("iotHub.HostName"), Key = securityKeys.PrimaryKey }); } catch (Exception ex) { //if we fail adding to table storage for the device simulator just continue Trace.TraceError("Failed to add simulated device : {0}", ex.Message); } } return(sampleIds); }
public async void AddOrUpdateDeviceAsyncTest() { var deviceConfig = _fixture.Create <InitialDeviceConfig>(); _tableStorageClientMock.Setup(x => x.ExecuteAsync(It.IsNotNull <TableOperation>())) .ReturnsAsync(new TableResult()); await _virtualDeviceStorage.AddOrUpdateDeviceAsync(deviceConfig); }
/// <summary> /// Adds the given device and assigned keys to the underlying repositories /// </summary> /// <param name="device">Device to add to repositories</param> /// <param name="securityKeys">Keys to assign to the device</param> /// <returns>Device that was added to the device registry</returns> private async Task <DeviceModel> AddDeviceToRepositoriesAsync(DeviceModel device, SecurityKeys securityKeys) { DeviceModel registryRepositoryDevice = null; ExceptionDispatchInfo capturedException = null; // if an exception happens at this point pass it up the stack to handle it // (Making this call first then the call against the Registry removes potential issues // with conflicting rollbacks if the operation happens to still be in progress.) await _iotHubRepository.AddDeviceAsync(device, securityKeys); try { registryRepositoryDevice = await _deviceRegistryCrudRepository.AddDeviceAsync(device); } catch (Exception ex) { // grab the exception so we can attempt an async removal of the device from the IotHub capturedException = ExceptionDispatchInfo.Capture(ex); } //Create a device in table storage if it is a simulated type of device //and the document was stored correctly without an exception bool isSimulatedAsBool = false; try { isSimulatedAsBool = (bool)device.IsSimulatedDevice; } catch (InvalidCastException ex) { Trace.TraceError("The IsSimulatedDevice property was in an invalid format. Exception Error Message: {0}", ex.Message); } if (capturedException == null && isSimulatedAsBool) { try { await _virtualDeviceStorage.AddOrUpdateDeviceAsync(new InitialDeviceConfig() { DeviceId = device.DeviceProperties.DeviceID, HostName = _configProvider.GetConfigurationSettingValue("iotHub.HostName"), Key = securityKeys.PrimaryKey }); } catch (Exception ex) { //if we fail adding to table storage for the device simulator just continue Trace.TraceError("Failed to add simulated device : {0}", ex.Message); } } // Since the rollback code runs async and async code cannot run within the catch block it is run here if (capturedException != null) { // This is a lazy attempt to remove the device from the Iot Hub. If it fails // the device will still remain in the Iot Hub. A more robust rollback may be needed // in some scenarios. await _iotHubRepository.TryRemoveDeviceAsync(device.DeviceProperties.DeviceID); capturedException.Throw(); } return(registryRepositoryDevice); }