首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Azure IotHubException是因为另一个AMQP客户端在尝试接收消息时打开了一个接收链接而关闭了连接。

Azure IotHubException是因为另一个AMQP客户端在尝试接收消息时打开了一个接收链接而关闭了连接。
EN

Stack Overflow用户
提问于 2021-01-29 09:34:57
回答 1查看 589关注 0票数 0

我正在尝试接收Azure设备客户端消息,如下所示

代码语言:javascript
复制
public async Task<List<string>> RecieveMessage(string correlationId)
        {
            var response = new List<string>();
            InitializeDeviceClient("AMQP");
            var flag = true;
            while (flag)
            {
                Microsoft.Azure.Devices.Client.Message receivedMessage = await deviceClient.ReceiveAsync(TimeSpan.FromSeconds(120));

                if (receivedMessage == null)
                {
                    await Task.Delay(100).ConfigureAwait(false);
                    continue;
                }

                Trace.WriteLine(receivedMessage.CorrelationId.ToString());
                await this.deviceClient.CompleteAsync(receivedMessage);
                if (receivedMessage.CorrelationId != correlationId)
                {
                    continue;
                }
                   
                var content = Encoding.UTF8.GetString(receivedMessage.GetBytes());
                response.Add(content);
                flag = false;
            }
            return response;
        }

在ReceiveAsync方法中,在VSTS中运行时,我会间歇性地得到下面的异常。

Microsoft.Azure.Devices.Client.Exceptions.IotHubException: error(condition:com.microsoft:link-creation-conflict,描述:{“errorCode”:409002,"trackingId":"e22773c954504357bcd262ebcf7e6c9e-G:3-TimeStamp:01/28/2021 23:24:27,“message”:“连接被关闭,因为另一个errorCode客户端打开了一个接收链接。每个客户端标识只允许一个连接。要了解更多信息,请参见https://aka.ms/iothub409002","timestampUtc":"2021-01-28T23:24:27.2617772Z"},info:[com.microsoft:is-filtered:True]) --> Microsoft.Azure.Amqp.AmqpException:{"errorCode":409002,Microsoft.Azure.Amqp.AmqpException 23:24:27,“message”:“由于另一个Microsoft.Azure.Amqp.AmqpException客户端打开了一个接收链接而关闭了连接。每个客户端标识只允许一个连接。要了解更多信息,请参见https://aka.ms/iothub409002","timestampUtc":"2021-01-28T23:24:27.2617772Z"}

StackTrace:

代码语言:javascript
复制
at Microsoft.Azure.Amqp.AsyncResult.End[TAsyncResult](IAsyncResult result)
       at Microsoft.Azure.Amqp.AmqpObject.OpenAsyncResult.End(IAsyncResult result)
       at Microsoft.Azure.Amqp.AmqpObject.EndOpen(IAsyncResult result)
       at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
    --- End of stack trace from previous location where exception was thrown ---
       at Microsoft.Azure.Devices.Client.Transport.AmqpIoT.AmqpIoTSession.OpenReceivingAmqpLinkAsync(DeviceIdentity deviceIdentity, AmqpSession amqpSession, Nullable`1 senderSettleMode, Nullable`1 receiverSettleMode, String deviceTemplate, String moduleTemplate, String linkSuffix, String correlationId, TimeSpan timeout)
    --- End of inner exception stack trace ---
        at Microsoft.Azure.Devices.Client.Transport.AmqpIoT.AmqpIoTSession.OpenReceivingAmqpLinkAsync(DeviceIdentity deviceIdentity, AmqpSession amqpSession, Nullable`1 senderSettleMode, Nullable`1 receiverSettleMode, String deviceTemplate, String moduleTemplate, String linkSuffix, String correlationId, TimeSpan timeout)
       at Microsoft.Azure.Devices.Client.Transport.AmqpIoT.AmqpIoTSession.OpenMessageReceiverLinkAsync(DeviceIdentity deviceIdentity, TimeSpan timeout)
       at Microsoft.Azure.Devices.Client.Transport.AmqpIoT.AmqpUnit.EnsureMessageReceivingLinkAsync(TimeSpan timeout)
       at Microsoft.Azure.Devices.Client.Transport.AmqpIoT.AmqpUnit.ReceiveMessageAsync(TimeSpan timeout)
       at Microsoft.Azure.Devices.Client.Transport.Amqp.AmqpTransportHandler.ReceiveAsync(TimeoutHelper timeoutHelper)
       at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.ExecuteWithErrorHandlingAsync[T](Func`1 asyncOperation)
       at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.<>c__DisplayClass18_0.<<ReceiveAsync>b__0>d.MoveNext()
    --- End of stack trace from previous location where exception was thrown ---
       at Microsoft.Azure.Devices.Client.Transport.RetryDelegatingHandler.ReceiveAsync(TimeoutHelper timeoutHelper)
       at Microsoft.Azure.Devices.Client.InternalClient.ReceiveAsync(TimeSpan timeout)
       at 
    RecieveMessage(String correlationId) in ....file.cs:line 487

这是什么原因?

是不是InitializeDeviceClient被多次调用了?

该方法实现如下

代码语言:javascript
复制
public void InitializeDeviceClient(string connType)
        {
            bool setFlag = _fixture.UseCertificate;
            if (setFlag)
            {
                try
                {
                    var initialPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Constants.PathCertPath.ToString());
                    DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(initialPath);
                    FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*");
                    var cert = new X509Certificate2(File.ReadAllBytes(Path.Combine(initialPath, filesInDir[0].ToString())),
                     "123",
                     X509KeyStorageFlags.UserKeySet);
                    var auth = new DeviceAuthenticationWithX509Certificate(Path.GetFileName(filesInDir[0].ToString().Replace(".pfx", "")), cert);
                    if (deviceClient == null)
                    {
                        switch (connType.ToUpper())
                        {
                            case ConnectionTypes.Amqp:
                                deviceClient = DeviceClient.Create(_fixture.IoTHubHostName, auth, Microsoft.Azure.Devices.Client.TransportType.Amqp_Tcp_Only);
                                break;
                            case ConnectionTypes.Mqtt:
                                deviceClient = DeviceClient.Create(_fixture.IoTHubHostName, auth, Microsoft.Azure.Devices.Client.TransportType.Mqtt);
                                break;
                            case ConnectionTypes.Https:
                                deviceClient = DeviceClient.Create(_fixture.IoTHubHostName, auth, Microsoft.Azure.Devices.Client.TransportType.Http1);
                                break;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error in sample: {0}", ex.Message);
                }
            }
        }
EN

回答 1

Stack Overflow用户

发布于 2021-02-02 08:23:22

这是因为您没有关闭已建立的连接并重新创建连接。您的错误消息显示发生了错误。

函数中使用await DeviceClient.CloseAsync().ConfigureAwait(false)后,可以使用await DeviceClient.CloseAsync().ConfigureAwait(false)关闭连接。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65951951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档