// Invoke the direct method on the device, passing the payload private async Task InvokeMethod(string userName, string uniqueId, string correlationId) { var methodInvocation = new CloudToDeviceMethod("LedColorControlMethod") { ResponseTimeout = TimeSpan.FromSeconds(30) }; var dataString = $"{userName}|{uniqueId}|{correlationId}"; var payLoadData = "{\"color\": \"red\", \"data\": \"" + dataString + "\"}"; methodInvocation.SetPayloadJson(payLoadData); var deviceId = ConfigurationManager.AppSettings[DeviceIdConfigurationName]; // Invoke the direct method asynchronously and get the response from the simulated device. var response = await serviceClient.InvokeDeviceMethodAsync(deviceId, methodInvocation); responseBody = response.GetPayloadAsJson(); }