查询执行后异常的Azure IoT中心GetNextAsTwinAsync

时间:2019-07-10 11:59:28

标签: c# sql .net azure-iot-hub

我正在尝试将设备查询到我的Azure IoT中心:

var query = registry.CreateQuery("SELECT * FROM devices where statusUpdateTime<=" + (rightnowUTC-tenMinAgoUTC ));
        while (query.HasMoreResults)
        {
            var page = await query.GetNextAsTwinAsync();
            Console.WriteLine("Devices con aggiornamento negli ultimi dieci minuti");
            foreach (var twin in page)
            {
                Console.WriteLine("Device id: " + twin.DeviceId + ", Connection state: " + twin.ConnectionState + ", Status: " +
                 twin.Status + ", Last activity time: " + twin.LastActivityTime + ", Status updated time: "+twin.StatusUpdatedTime);
            }
        }

我检查了查询对象本身,并执行了while循环。

一旦转到“ GetNextAsTwinSync”,将引发此异常:

(Inner Exception #0) System.ArgumentException: {"Message":"ErrorCode:ArgumentInvalid;BadRequest","ExceptionMessage":"Tracking ID:b862e066a6944c9caf8f726aae82f80d-G:12-TimeStamp:07/10/2019 11:55:01"}
       at Microsoft.Azure.Devices.HttpClientHelper.ExecuteAsync(HttpClient httpClient, HttpMethod httpMethod, Uri requestUri, Func`3 modifyRequestMessageAsync, Func`2 isMappedToException, Func`3 processResponseMessageAsync, IDictionary`2 errorMappingOverrides, CancellationToken cancellationToken)
       at Microsoft.Azure.Devices.HttpClientHelper.PostAsync[T](Uri requestUri, T entity, IDictionary`2 errorMappingOverrides, IDictionary`2 customHeaders, MediaTypeHeaderValue customContentType, ICollection`1 customContentEncoding, CancellationToken cancellationToken)
       at Microsoft.Azure.Devices.HttpRegistryManager.ExecuteQueryAsync(String sqlQueryString, Nullable`1 pageSize, String continuationToken, CancellationToken cancellationToken)
       at Microsoft.Azure.Devices.Query.GetNextAsync(QueryOptions options)
       at Microsoft.Azure.Devices.Query.GetAndCastNextResultAsync[T](QueryResultType type, QueryOptions options)
       at Microsoft.Azure.Devices.Query.GetNextAsTwinAsync(QueryOptions options)
       at Microsoft.Azure.Devices.Query.GetNextAsTwinAsync()

1 个答案:

答案 0 :(得分:0)

我建议您阅读IoT Hub query language for device and module twins, jobs, and message routing

您的查询字符串错误,这就是 System.ArgumentException 的原因。

使用以下查询字符串:

string querystring = $"SELECT * FROM devices where statusUpdateTime <= '{(DateTime.UtcNow - TimeSpan.FromMinutes(10)).ToString("o")}'";

请注意,强烈建议将ISO 8601格式用于日期时间数据类型。