System.Net.Sockets.SocketException:连接尝试失败,因为连接方

时间:2019-08-29 09:33:27

标签: azure-iot-hub

我有以下代码,该代码尝试从设备读取消息以使iot-hub变为天蓝色。

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 = "*****=";
        private readonly static string s_iotHubSasKeyName = "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());
        }
    }
}

在var runtimeInfo = await上s_eventHubClient.GetRuntTimeInformationAsync();问题出在此对象runtimeInfo(未连接)上。这可能归功于防火墙。但是在我的sendMessage类上,我可以毫无问题地发送。请协助。

0 个答案:

没有答案