Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 474.2k

System.Net.Socket Error using Azure-Iot-device-read-message

Aug 29 2019 4:28 AM
Hi team
 
I have this error on my console application;
  1. using System;  
  2. using System.Threading;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Microsoft.Azure.EventHubs;  
  8. namespace ReadMessageApplication  
  9. {  
  10. class Program  
  11. {  
  12. private readonly static string s_eventHubsCompartibleEndpoint ="sb://iothub-ns-university-2081416-42bc501981.servicebus.windows.net/";  
  13. private readonly static string s_eventHubsCompartiblePath = "universityiothub";  
  14. private readonly static string s_iotHubSasKey = "****=";  
  15. private readonly static string s_iotHubSasKeyName = "iothubowner";  
  16. private static EventHubClient s_eventhubClient;  
  17. private static async Task ReceivedMessageFromDeviceAsync(string partition, CancellationToken ct)  
  18. {  
  19. var eventHubReciver = s_eventhubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now));  
  20. Console.WriteLine("Create receiver on partition:" + partition);  
  21. while (true)  
  22. {  
  23. if (ct.IsCancellationRequested) break;  
  24. Console.WriteLine("Listening for messages on:" + partition);  
  25. var events = await eventHubReciver.ReceiveAsync(100);  
  26. if (events == nullcontinue;  
  27. foreach(EventData eventData in events)  
  28. {  
  29. string data = Encoding.UTF8.GetString(eventData.Body.Array);  
  30. Console.WriteLine("Message received on partition {0}:", partition);  
  31. Console.WriteLine(" {0}:", data);  
  32. Console.WriteLine("Application properties (set by device)");  
  33. foreach(var prop in eventData.Properties)  
  34. {  
  35. Console.WriteLine(" {0}: {1}", prop.Key, prop.Value);  
  36. }  
  37. Console.WriteLine("System properties (set by Iot Hub)");  
  38. foreach(var prop in eventData.SystemProperties)  
  39. {  
  40. Console.WriteLine(" {0}: {1}", prop.Key, prop.Value);  
  41. }  
  42. }  
  43. }  
  44. }  
  45. private static async Task Main(string[] args)  
  46. {  
  47. Console.WriteLine("IOT Hub Device Message - Read device to cloud message. Ctrl-C to exit.\n");  
  48. var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompartibleEndpoint), s_eventHubsCompartiblePath, s_iotHubSasKeyName, s_iotHubSasKey);  
  49. s_eventhubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());  
  50. //partition receiver for each hub.  
  51. var runtimeInfo = await s_eventhubClient.GetRuntimeInformationAsync();  
  52. var dtwoPartition = runtimeInfo.PartitionIds;  
  53. CancellationTokenSource ct = new CancellationTokenSource();  
  54. Console.CancelKeyPress += (s, e) =>  
  55. {  
  56. e.Cancel = true;  
  57. ct.Cancel();  
  58. Console.WriteLine("Exiting..");  
  59. };  
  60. var tasks = new List<Task>();  
  61. foreach(string partition in dtwoPartition)  
  62. {  
  63. tasks.Add(ReceivedMessageFromDeviceAsync(partition, ct.Token));  
  64. }  
  65. Task.WaitAll(tasks.ToArray());  
  66. }  
  67. }  


Answers (2)