简体   繁体   中英

IOT hub message processor

Having some trouble processing messages created in the Azure IoT Hub.

Getting the following eror: Exception thrown: 'Microsoft.ServiceBus.Messaging.Amqp.AmqpException' in Microsoft.ServiceBus.dll ("An AMQP error occurred (condition='amqp:link:redirect').")

Can anyone point me in the right direction?

Regards, Jonas

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.ServiceBus.Messaging;

namespace IOTHubMessageProcessor
{
    class Program
    {
        static string connectionString = "HostName=yaddaaaa.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=keydataasdss+tacsCxwkWQeUm9sMCc2GHnQkIZHM=";
        static string iotHubD2cEndpoint = "messages/events";
        static EventHubClient eventHubClient;
        static void Main(string[] args)
        {

            Console.WriteLine("Receive messages\n");
            eventHubClient = EventHubClient.CreateFromConnectionString(connectionString, iotHubD2cEndpoint);

            var d2cPartitions = eventHubClient.GetRuntimeInformation().PartitionIds;

            foreach (string partition in d2cPartitions)
            {
                ReceiveMessagesFromDeviceAsync(partition);
            }
            Console.ReadLine();
        }


        private async static Task ReceiveMessagesFromDeviceAsync(string partition)
        {
            var eventHubReceiver = eventHubClient.GetDefaultConsumerGroup().CreateReceiver(partition, DateTime.UtcNow);

            while (true)
            {
                EventData eventData = await eventHubReceiver.ReceiveAsync();
                if (eventData == null) continue;

                string data = Encoding.UTF8.GetString(eventData.GetBytes());
                Console.WriteLine(string.Format("Message received. Partition: {0} Data: '{1}'", partition, data));
            }
        }
    }
}

I don't encounter this issue when testing with your code, so it might be related to something else. I find the duplicate thread Azure IoT hub basic receiving example, AMQP error , it suggests check on block port or proxy settings, you can try it.

Your iotHubD2cEndpoint has an incorrect format. You can find your compatible endpoint in your azure portal -> messages -> device to cloud settings.

I found this a very helpful example: https://github.com/ppatierno/codesamples/tree/master/IoTHubAmqp/IoTHubAmqp

Code looks fine to me. Mine is identical and works well.

Your best bet is to create a new IoT hub in Azure and replace the strings.

While building the connectionstring, can you try "Endpoint=" instead of "HostName=" on line 12?

Hope this helps!

Mert

A couple of comments:

  1. Please ensure that you are using the latest version of the Service Bus dll. ie, 3.1.7 (as of today).
  2. Please do not mix async and sync method calls in your code.

Let us know if you are still encountering this issue.

Sometimes I see that the proxy filters amqp packets. Changing the transport type to http probably will solve the problem. Now I don't have access to Visual Studio but I seem to remember that it is possible to set the transport under the client properties. If you try it you can easily find if the problem is in the proxy or in the program.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM