/// <summary>
        /// Invokes a method on the device.
        /// </summary>
        /// <param name="deviceId">Unique identifier of the device to be updated.</param>
        public async Task InvokeMethodOnDeviceAsync(string deviceId)
        {
            SampleLogger.PrintHeader("INVOKE REBOOT METHOD ON A DEVICE");

            try
            {
                #region Snippet:IotInvokeMethodOnDevice

                var request = new CloudToDeviceMethodRequest
                {
                    MethodName = "reboot",
                };

                CloudToDeviceMethodResponse response = (await IoTHubServiceClient.Devices
                                                        .InvokeMethodAsync(deviceId, request)
                                                        .ConfigureAwait(false))
                                                       .Value;

                SampleLogger.PrintSuccess($"\t- Method 'REBOOT' invoked on device {deviceId}");
                SampleLogger.PrintHeader($"Status of method invocation is: {response.Status}");

                #endregion Snippet:IotInvokeMethodOnDevice
            }
            catch (Exception ex)
            {
                // Try to cleanup before exiting with fatal error.
                await CleanupHelper.DeleteAllDevicesInHubAsync(IoTHubServiceClient);

                SampleLogger.FatalError($"Failed to invoke method on device due to:\n{ex.Message}");
                throw;
            }
        }
        public async Task <Response <CloudToDeviceMethodResponse> > InvokeDeviceMethodAsync(string deviceId, CloudToDeviceMethodRequest directMethodRequest, CancellationToken cancellationToken = default)
        {
            if (deviceId == null)
            {
                throw new ArgumentNullException(nameof(deviceId));
            }
            if (directMethodRequest == null)
            {
                throw new ArgumentNullException(nameof(directMethodRequest));
            }

            using var message = CreateInvokeDeviceMethodRequest(deviceId, directMethodRequest);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                CloudToDeviceMethodResponse value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                if (document.RootElement.ValueKind == JsonValueKind.Null)
                {
                    value = null;
                }
                else
                {
                    value = CloudToDeviceMethodResponse.DeserializeCloudToDeviceMethodResponse(document.RootElement);
                }
                return(Response.FromValue(value, message.Response));
            }