Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I am experience SocketException in my readdevice-2message class, what could be the result of this?

System.Net.Sockets.SocketException: 'A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond'


What I have tried:

using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.EventHubs;

namespace ReadMessageApplication
{
    class Program
    {
        private readonly static string s_eventHubsCompartibleEndpoint = "sb://iothub-ns-university-2081416-42bc501981.servicebus.windows.net/";
        private readonly static string s_eventHubsCompartiblePath = "universityiothub";
        private readonly static string s_iotHubSasKey = "E****";
        private readonly static string s_iotHubSasK<pre><pre lang="c#">
eyName ="iothubowner";
private static EventHubClient s_eventhubClient;

private static async Task ReceivedMessageFromDeviceAsync(string partition, CancellationToken ct)
{
var eventHubReciver = s_eventhubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now));
Console.WriteLine("Create receiver on partition:" + partition);

while (true)
{
if (ct.IsCancellationRequested) break;
Console.WriteLine("Listening for messages on:" + partition);

var events = await eventHubReciver.ReceiveAsync(100);

if (events == null) continue;

foreach(EventData eventData in events)
{
string data = Encoding.UTF8.GetString(eventData.Body.Array);
Console.WriteLine("Message received on partition {0}:", partition);
Console.WriteLine(" {0}:", data);
Console.WriteLine("Application properties (set by device)");

foreach(var prop in eventData.Properties)
{
Console.WriteLine(" {0}: {1}", prop.Key, prop.Value);
}
Console.WriteLine("System properties (set by Iot Hub)");
foreach(var prop in eventData.SystemProperties)
{
Console.WriteLine(" {0}: {1}", prop.Key, prop.Value);
}
}
}
}

private static async Task Main(string[] args)
{
Console.WriteLine("IOT Hub Device Message - Read device to cloud message. Ctrl-C to exit.\n");

var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompartibleEndpoint), s_eventHubsCompartiblePath, s_iotHubSasKeyName, s_iotHubSasKey);
s_eventhubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

//partition receiver for each hub.
var runtimeInfo = await s_eventhubClient.GetRuntimeInformationAsync();
var dtwoPartition = runtimeInfo.PartitionIds;

CancellationTokenSource ct = new CancellationTokenSource();

Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
ct.Cancel();
Console.WriteLine("Exiting..");
};
var tasks = new List<task>();
foreach(string partition in dtwoPartition)
{
tasks.Add(ReceivedMessageFromDeviceAsync(partition, ct.Token));
}
Task.WaitAll(tasks.ToArray());
}
}
}
Posted
Updated 10-Sep-19 21:49pm

1 solution

private static async Task Main(string[] args)
     {
         Console.WriteLine("IOT Hub Device Message - Read device to cloud message. Ctrl-C to exit.\n");

         var connectionString = new EventHubsConnectionStringBuilder(new Uri(s_eventHubsCompartibleEndpoint), s_eventHubsCompartiblePath, s_iotHubSasKeyName, s_iotHubSasKey);
         s_eventhubClient = EventHubClient.CreateFromConnectionString(connectionString.ToString());

         //partition receiver for each hub.
         var runtimeInfo = await s_eventhubClient.GetRuntimeInformationAsync();
         var dtwoPartition = runtimeInfo.PartitionIds;

         CancellationTokenSource ct = new CancellationTokenSource();

         Console.CancelKeyPress += (s, e) =>
          {
              e.Cancel = true;
              ct.Cancel();
              Console.WriteLine("Exiting..");
          };
         var tasks = new List<Task>();
         foreach(string partition in dtwoPartition)
         {
             tasks.Add(ReceivedMessageFromDeviceAsync(partition, ct.Token));
         }
         Task.WaitAll(tasks.ToArray());
     }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900