Click here to Skip to main content
14,300,703 members
Rate this:
Please Sign up or sign in to vote.
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

Rate this:
Please Sign up or sign in to vote.

Solution 1

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());
     }
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100