简体   繁体   English

物联网中心消息处理器

[英]IOT hub message processor

Having some trouble processing messages created in the Azure IoT Hub. 处理在Azure IoT中心中创建的消息时遇到一些麻烦。

Getting the following eror: Exception thrown: 'Microsoft.ServiceBus.Messaging.Amqp.AmqpException' in Microsoft.ServiceBus.dll ("An AMQP error occurred (condition='amqp:link:redirect').") 得到以下错误:引发异常:Microsoft.ServiceBus.dll中的“ Microsoft.ServiceBus.Messaging.Amqp.AmqpException”(“发生AMQP错误(条件='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. 我发现重复线程Azure IoT中心基本接收示例AMQP错误 ,它建议检查块端口或代理设置,您可以尝试一下。

Your iotHubD2cEndpoint has an incorrect format. 您的iotHubD2cEndpoint的格式不正确。 You can find your compatible endpoint in your azure portal -> messages -> device to cloud settings. 您可以在Azure门户->消息->设备到云设置中找到兼容的端点。

I found this a very helpful example: https://github.com/ppatierno/codesamples/tree/master/IoTHubAmqp/IoTHubAmqp 我发现这是一个非常有用的示例: 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. 最好的选择是在Azure中创建一个新的IoT中心并替换字符串。

While building the connectionstring, can you try "Endpoint=" instead of "HostName=" on line 12? 在建立连接字符串时,可以在第12行尝试使用“ Endpoint =”代替“ HostName =”吗?

Hope this helps! 希望这可以帮助!

Mert 莫特

A couple of comments: 几点评论:

  1. Please ensure that you are using the latest version of the Service Bus dll. 请确保您使用的是最新版本的Service Bus dll。 ie, 3.1.7 (as of today). 即3.1.7(截至今天)。
  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. 有时我看到代理过滤amqp数据包。 Changing the transport type to http probably will solve the problem. 将传输类型更改为http可能会解决此问题。 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. 现在,我没有访问Visual Studio的权限,但我似乎记得,可以在客户端属性下设置传输方式。 If you try it you can easily find if the problem is in the proxy or in the program. 如果尝试使用它,您可以轻松找到问题出在代理服务器还是程序中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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