public async void checkIoTHubForMessages() { while (true) { try { Microsoft.Azure.Devices.Client.Message IoTMessage = await deviceClient.ReceiveAsync(); if (IoTMessage != null) { string IoTMessageString = Encoding.ASCII.GetString(IoTMessage.GetBytes()); Dictionary <string, string> thisIsMyProperty = new Dictionary <string, string>(); thisIsMyProperty.Add("source", "IoTHub"); // create a gateway message from IoTMessageString Microsoft.Azure.Devices.Gateway.Message messageToPublish = new Microsoft.Azure.Devices.Gateway.Message("Data: " + IoTMessageString, thisIsMyProperty); this.broker.Publish(messageToPublish); // publish message await deviceClient.CompleteAsync(IoTMessage); // mark the message received Console.WriteLine("received message: " + IoTMessageString); Console.WriteLine(" code not implemented to handle messages. Finish me!"); } } catch { } } }
// note below, because both .Client and .Gateway impement Message, I had to be explicit public void Receive(Microsoft.Azure.Devices.Gateway.Message received_message) { dynamic mymessage = new JObject(); // creating a JSON object as the message mymessage.text = System.Text.Encoding.UTF8.GetString(received_message.Content, 0, received_message.Content.Length); deviceClient.SendEventAsync(new Microsoft.Azure.Devices.Client.Message(Encoding.UTF8.GetBytes(mymessage.ToString()))); Console.WriteLine("Sent message: " + mymessage.ToString()); }