static async Task UpdateDeploymentEnvironmentVariablesAsync(TestResultReportingClient apiClient, CancellationTokenSource cts) { RegistryManager registryManager = null; try { registryManager = RegistryManager.CreateFromConnectionString(Settings.Current.IoTHubConnectionString.OrDefault()); JObject deploymentJson = await GetEdgeAgentDeploymentManifestJsonAsync(registryManager, Settings.Current.DeviceId); DateTime testStartAt = DateTime.UtcNow; long count = 1; var envVars = new Dictionary <string, string>(); while (!cts.IsCancellationRequested && DateTime.UtcNow - testStartAt < Settings.Current.TestDuration) { KeyValuePair <string, string> newEnvVar = AddEnvironmentValue(deploymentJson, Settings.Current.TargetModuleId.OrDefault(), count); ConfigurationContent configContent = JsonConvert.DeserializeObject <ConfigurationContent>(deploymentJson.ToString()); await registryManager.ApplyConfigurationContentOnDeviceAsync(Settings.Current.DeviceId, configContent); envVars.Add(newEnvVar.Key, newEnvVar.Value); var testResult = new DeploymentTestResult(Settings.Current.TrackingId, Settings.Current.ModuleId + ".send", envVars, DateTime.UtcNow); await ModuleUtil.ReportTestResultAsync(apiClient, Logger, testResult); Logger.LogInformation($"Successfully report to TRC for new deployment: tracking id={Settings.Current.TrackingId}, new environment variable={newEnvVar.Key}:{newEnvVar.Value}, EnvVars Count={envVars.Count}."); await Task.Delay(Settings.Current.DeploymentUpdatePeriod, cts.Token); count++; } } finally { registryManager?.Dispose(); } }
public async Task <List <IoTDevice> > GetDevicesAsync(string searchterm = null) { List <IoTDevice> devices = null; RegistryManager registryManager = null; try { devices = new List <IoTDevice>(); registryManager = RegistryManager.CreateFromConnectionString(_configuration.GetConnectionString("IOTHUB")); IEnumerable <Microsoft.Azure.Devices.Device> iotDevices = await registryManager.GetDevicesAsync(100); foreach (Microsoft.Azure.Devices.Device iotDevice in iotDevices) { devices.Add(new IoTDevice(iotDevice.Id, Convert.ToString(iotDevice.ConnectionState))); } } catch (Exception exception) { _logger.LogCritical(exception, exception.Message); } finally { registryManager?.Dispose(); } return(devices); }
/// <summary> /// Method to be used when the application exits /// </summary> public void Dispose() { if (registryManager == null) { registryManager.Dispose(); } }
protected virtual void Dispose(bool disposing) { if (!_disposedValue) { if (disposing) { _registryManager.Dispose(); } _disposedValue = true; } }
/// <inheritdoc /> public void Dispose() { RegistryManager.Dispose(); }
public ValueTask DisposeAsync() { RegistryManager rm = this.registryManager; return(new ValueTask(Task.Run(() => rm.Dispose()))); }
async Task BackupAndRestoreMessageDeliveryTestBase( ITransportSettings[] transportSettings, int beforeBackupMessageCount, int afterBackupMessageCount, int expectedMessageCountAfterRestore, Action postBackupModifier) { ProtocolHeadFixture protocolHeadFixture = EdgeHubFixtureCollection.GetFixture(); TestModule sender = null; TestModule receiver = null; string edgeDeviceConnectionString = await SecretsHelper.GetSecretFromConfigKey("edgeCapableDeviceConnStrKey"); IotHubConnectionStringBuilder connectionStringBuilder = IotHubConnectionStringBuilder.Create(edgeDeviceConnectionString); RegistryManager rm = RegistryManager.CreateFromConnectionString(edgeDeviceConnectionString); Func <int, TimeSpan> waitTimeComputer = (numberOfMessages) => TimeSpan.FromMinutes(Math.Ceiling(numberOfMessages / 2000d) + 2); try { sender = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "sender1", transportSettings); receiver = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "receiver1", transportSettings); Console.WriteLine($"Sending {beforeBackupMessageCount} messages."); // Send 10 messages before a receiver is registered. Task <int> task1 = sender.SendMessagesByCountAsync("output1", 0, beforeBackupMessageCount, waitTimeComputer(beforeBackupMessageCount)); int sentMessagesCount = await task1; Assert.Equal(beforeBackupMessageCount, sentMessagesCount); TimeSpan waitTime = TimeSpan.FromMinutes(2); Console.WriteLine($"Waiting {waitTime.TotalSeconds} seconds before validating receipt of messages."); // Wait for a while and then close the test fixture which will in turn close the protocol heads and the in-memory DB store thus creating a backup. await Task.Delay(TimeSpan.FromMinutes(2)); await protocolHeadFixture.CloseAsync(); Console.WriteLine("Protocol heads closed."); postBackupModifier(); // Get new fixture to re-initialize the edge hub container. protocolHeadFixture = EdgeHubFixtureCollection.GetFixture(); // Reconnect clients due to C# SDK bug where it illegally attempts to send through closed amqp link sender = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "sender1", transportSettings); receiver = await TestModule.CreateAndConnect(rm, connectionStringBuilder.HostName, connectionStringBuilder.DeviceId, "receiver1", transportSettings); // Register the message handler now. await receiver.SetupReceiveMessageHandler(); Console.WriteLine($"Sending {afterBackupMessageCount} messages."); // Send more messages after the receiver is registered. Task <int> task2 = sender.SendMessagesByCountAsync("output1", beforeBackupMessageCount, afterBackupMessageCount, TimeSpan.FromMinutes(2)); sentMessagesCount = await task2; Assert.Equal(afterBackupMessageCount, sentMessagesCount); waitTime = waitTimeComputer(expectedMessageCountAfterRestore); Console.WriteLine($"Waiting {waitTime.TotalSeconds} seconds before validating receipt of messages."); // Validate that all the messages were received (both sent earlier and the new messages). await Task.Delay(waitTime); ISet <int> receivedMessages = receiver.GetReceivedMessageIndices(); Assert.Equal(expectedMessageCountAfterRestore, receivedMessages.Count); } finally { if (rm != null) { await rm.CloseAsync(); rm.Dispose(); } if (sender != null) { await sender.Disconnect(); sender.Dispose(); } if (receiver != null) { await receiver.Disconnect(); receiver.Dispose(); } await protocolHeadFixture.CloseAsync(); } // wait for the connection to be closed on the Edge side await Task.Delay(TimeSpan.FromSeconds(10)); }
public async Task RemoveDevices() { var test = await registryManager.RemoveDevices2Async(addedDevices); registryManager.Dispose(); }