使用Cloud Service-Worker角色的Azure云到设备直接消息

时间:2018-08-15 09:10:06

标签: c# azure azure-iot-sdk

我们正在一个项目,需要在设备之间交换大量消息/命令。

我们正在使用Cloud Service Worker角色来处理命令,并使用Cloud to Device Direct方法将其发送到相关设备。

辅助角色配置为具有2 GB RAM的A2V2-2内核,辅助角色容量没有问题,CPU和内存均处于控制状态。

对于较少的消息/命令来说,它的工作正常(例如500条消息)。但是,当没有消息增加时,我们将面临性能问题(例如1000条消息)。我们的目标是<5秒延迟。当我们尝试登录时在Worker Role VM中发现,在向设备发送消息/命令时,TCP连接数一直在增加,并导致速度缓慢。

下面的代码行我们将使用直接方法发送消息。寻找更好的方法来在每次直接方法调用之后处理Service客户端对象。

 var methodInvocation = new CloudToDeviceMethod(methodInfo.MethodName) { ResponseTimeout = TimeSpan.FromSeconds(methodInfo.ResponseTimeout) };
            //set the payload
            methodInvocation.SetPayloadJson(methodInfo.Payload);

            //invokes direct method
             var response = _serviceClient.InvokeDeviceMethodAsync(methodInfo.DeviceId, methodInvocation);

            if (_serviceClient != null)
            {
                //closes the service client connection
                _serviceClient.CloseAsync();
                _serviceClient.Dispose();
            }

1 个答案:

答案 0 :(得分:0)

最后我们找到了解决方案.Azure Service Client对象未正确关闭和处置。成功直接方法调用后,我们已明确关闭并处置Service Client对象。

  //closes the service client connection
  await _serviceClient.InvokeDeviceMethodAsync(methodInfo.DeviceId, methodInvocation);
 _serviceClient.CloseAsync().Wait();
 _serviceClient.Dispose();