/// <summary> /// A sample for illustrating how a device should handle connection status updates. /// </summary> /// <param name="args"> /// Run with `--help` to see a list of required and optional parameters. /// </param> public static async Task <int> Main(string[] args) { // Parse application parameters Parameters parameters = null; ParserResult <Parameters> result = Parser.Default.ParseArguments <Parameters>(args) .WithParsed(parsedParams => { parameters = parsedParams; }) .WithNotParsed(errors => { Environment.Exit(1); }); // Set up logging ILoggerFactory loggerFactory = new LoggerFactory(); loggerFactory.AddColorConsoleLogger( new ColorConsoleLoggerConfiguration { MinLogLevel = LogLevel.Debug, }); var logger = loggerFactory.CreateLogger <Program>(); const string SdkEventProviderPrefix = "Microsoft-Azure-"; // Instantiating this seems to do all we need for outputting SDK events to our console log _ = new ConsoleEventListener(SdkEventProviderPrefix, logger); // Run the sample var sample = new DeviceReconnectionSample(parameters.GetConnectionStrings(), parameters.TransportType, logger); await sample.RunSampleAsync(); logger.LogInformation("Done."); return(0); }
public static async Task Main(string[] args) { // Parse application parameters Parameters parameters = null; ParserResult <Parameters> result = Parser.Default.ParseArguments <Parameters>(args) .WithParsed(parsedParams => { parameters = parsedParams; }) .WithNotParsed(errors => { Environment.Exit(1); }); // Set up logging ILoggerFactory loggerFactory = new LoggerFactory(); loggerFactory.AddColorConsoleLogger( new ColorConsoleLoggerConfiguration { MinLogLevel = LogLevel.Debug, }); s_logger = loggerFactory.CreateLogger <Program>(); const string SdkEventProviderPrefix = "Microsoft-Azure-"; // Instantiating this seems to do all we need for outputting SDK events to our console log _ = new ConsoleEventListener(SdkEventProviderPrefix, s_logger); if (!parameters.Validate(s_logger)) { throw new ArgumentException("Required parameters are not set. Please recheck required variables by using \"--help\""); } var runningTime = parameters.ApplicationRunningTime != null ? TimeSpan.FromSeconds((double)parameters.ApplicationRunningTime) : Timeout.InfiniteTimeSpan; s_logger.LogInformation("Press Control+C to quit the sample."); using var cts = new CancellationTokenSource(runningTime); Console.CancelKeyPress += (sender, eventArgs) => { eventArgs.Cancel = true; cts.Cancel(); s_logger.LogInformation("Sample execution cancellation requested; will exit."); }; s_logger.LogDebug($"Set up the device client."); using DeviceClient deviceClient = await SetupDeviceClientAsync(parameters, cts.Token); var sample = new TemperatureControllerSample(deviceClient, s_logger); await sample.PerformOperationsAsync(cts.Token); // PerformOperationsAsync is designed to run until cancellation has been explicitly requested, either through // cancellation token expiration or by Console.CancelKeyPress. // As a result, by the time the control reaches the call to close the device client, the cancellation token source would // have already had cancellation requested. // Hence, if you want to pass a cancellation token to any subsequent calls, a new token needs to be generated. // For device client APIs, you can also call them without a cancellation token, which will set a default // cancellation timeout of 4 minutes: https://github.com/Azure/azure-iot-sdk-csharp/blob/64f6e9f24371bc40ab3ec7a8b8accbfb537f0fe1/iothub/device/src/InternalClient.cs#L1922 await deviceClient.CloseAsync(); }