public void Test_Create_ModuleIdentity_WithAuthMethod_ShouldCreateModuleClient() { string token = TokenHelper.CreateSasToken(IotHubHostName); IIdentity identity = new ModuleIdentity(IotHubHostName, DeviceId, ModuleId); var authenticationMethod = new ModuleAuthenticationWithToken(DeviceId, ModuleId, token); var transportSettings = new ITransportSettings[] { new MqttTransportSettings(TransportType.Mqtt_Tcp_Only) }; IClient client = new ClientProvider().Create(identity, authenticationMethod, transportSettings); Assert.NotNull(client); Assert.True(client is ModuleClientWrapper); }
public async Task Init(CancellationTokenSource cancellationTokenSource) { var tokenHelper = new SecurityDaemonClient(); while (!cancellationTokenSource.IsCancellationRequested) { try { Console.WriteLine($"tokenHelper: '{tokenHelper.ToString()}'"); var token = tokenHelper.GetModuleToken(3600).Result; Console.WriteLine($"Token: '{token}'"); var tokenAuthentication = new ModuleAuthenticationWithToken(tokenHelper.DeviceId, tokenHelper.ModuleId, token); var transportType = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only); ITransportSettings[] settings = { transportType }; using (ModuleClient moduleClient = ModuleClient.Create(tokenHelper.IotHubHostName, tokenAuthentication, settings)) { if (moduleClient == null) { Console.WriteLine("Failed to create ModuleClient!"); throw new InvalidOperationException("Failed to create ModuleClient"); } moduleClient.SetConnectionStatusChangesHandler((ConnectionStatus status, ConnectionStatusChangeReason reason) => { if (reason == ConnectionStatusChangeReason.Bad_Credential) { Init(cancellationTokenSource).Wait(); } }); Console.WriteLine($"Starting DeviceStreamSample on '{_hostName}:{_port}'"); await RunSampleAsync(moduleClient, true, cancellationTokenSource); } } catch (Exception ex) { Console.WriteLine($"{DateTime.UtcNow} - Got an exception: {ex.ToString()}"); Console.WriteLine("Waiting 1 minute and trying again..."); await Task.Delay(TimeSpan.FromMinutes(1)); } } }