你调用的对象是空的。使用TPM设备客户端发送消息时

时间:2016-10-27 17:13:27

标签: c# azure uwp azure-iot-hub tpm

我们有一个带有bpm180传感器的覆盆子pi3,我们初次化了这样的连接

 TpmDevice myDevice = new TpmDevice(0); // Use logical device 0 on the TPM
                string hubUri = myDevice.GetHostName();
                string deviceId = myDevice.GetDeviceId();
                string sasToken = myDevice.GetSASToken();

                deviceClient = DeviceClient.Create(
                    hubUri,
                    Microsoft.Azure.Devices.Client.AuthenticationMethodFactory.
                        CreateAuthenticationWithToken(deviceId, sasToken), Microsoft.Azure.Devices.Client.TransportType.Amqp);

然后我们发送这样的消息:

public async void Bpm180Tick(object state)
        {
            string temperatureText;
            try
            {
                var sensorData = await _bmp180.GetSensorDataAsync(Bmp180AccuracyMode.UltraHighResolution);
                temperatureText = sensorData.Temperature.ToString("");
                temperatureText += "°C";
                var temperatureDataPoint = new
                {
                    deviceId = Global.GlobalTemperature,
                    valor = sensorData.Temperature,
                    fecha = DateTime.Now
                };
                //// actualizaciones de la interfaz de usuario... deben ser invocados en el subproceso de interfaz de usuario
                var messageString = JsonConvert.SerializeObject(temperatureDataPoint);
                var message = new Microsoft.Azure.Devices.Client.Message(Encoding.ASCII.GetBytes(messageString));
                //message.Properties["Ambiente"] = ambiente;
                //autoResetEvent.WaitOne();
                await deviceClient.SendEventAsync(message);
                //autoResetEvent.Set();
                var task = this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    Txt_Temperaura.Text = temperatureText;
                });
            }
            catch (Exception ex)
            {
                var a = ex.StackTrace;
                temperatureText = "Sensor Error: " + ex.Message;
            }


        }

然而,我们得到了和目标参考,但错误似乎在库(sdk)中,而不是在我们的代码中

堆栈跟踪就是这个......

  at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.<SendAmqpMessageAsync>d__27.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Devices.Client.Transport.AmqpTransportHandler.<SendEventAsync>d__19.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<>c__DisplayClass13_0.<<ExecuteWithErrorHandlingAsync>b__0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Devices.Client.Transport.ErrorDelegatingHandler.<ExecuteWithErrorHandlingAsync>d__14`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.Azure.Devices.Client.Transport.GateKeeperDelegatingHandler.<SendEventAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at ActiveSense.Tempsense.Sensor.MainPage.<Bpm180Tick>d__23.MoveNext()

4 个答案:

答案 0 :(得分:1)

我想知道在异步代码运行时温度文本是否可能不再存在。我首先尝试将其变为静态类变量,看看会发生什么。

答案 1 :(得分:1)

很可能SAS令牌已过期,但AMQP库没有给您一个有用的错误。要确认这一点,请使用带有SharedAccessKey的连接字符串:

DeviceClient.CreateFromConnectionString("...;SharedAccessKey=xyz;...", TransportType.Amqp);

如果可行,您需要考虑更新SAS令牌(不要在产品代码中留下SharedAccessKey!)。如果这给您带来了相同的错误,则问题出在代码的其他地方。

答案 2 :(得分:1)

SAS令牌可能会过期。

另一方面,由于Azure-IoT-SDK是开源的,您可以直接引用sdk源代码来检查错误。

  1. 删除项目nuget参考&#34; Microsoft.Azure.Devices.Client&#34;
  2. 将azure-iot-sdks存储库与项目并排克隆
  3. 在项目中引用Microsoft.Azure.Devices.Client.WinRT。
  4. 您应该能够调试AmqpTransportHandler.SendAmqpMessageAsync源代码以查看问题的真正原因。

    我自己没有运气使用Windows IoT Core 14393在RPi3上复制你的问题。

答案 3 :(得分:0)

这是因为TPM库中存在一个错误,我提出了问题并修复了拉取请求。

以下是该问题的链接:https://github.com/ms-iot/security/issues/11

SAS令牌立即过期。它工作一段时间的唯一原因是IoT Hub允许一些时钟偏差。我还在等待NuGet上新版本的Microsoft.Devices.Tpm。