如何解决不支持Azure Cosmos DB查询
我创建了一个Azure函数(HTTP触发),该函数使用Linq对Cosmos DB运行空间查询。它返回在几何多边形边界内的 rock 元素。
运行此命令时,出现以下错误:
执行函数时发生异常:示例一个或多个错误 发生了。 (不支持方法“内部”。Windows / 10.0.14393 documentdb-netcore-sdk / 2.11.6)不支持“内部”方法。 Windows / 10.0.14393 documentdb-netcore-sdk / 2.11.6
我已经检查过我正在使用所有最新的NuGet库版本。有人可以提示我要检查什么吗?这是代码:
public static class Example
{
[FunctionName("example")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function,"post",Route = "example")] HttpRequest req,[CosmosDB(
databaseName: "a",collectionName: "b",ConnectionStringSetting = "CosmosDBConnection")]
DocumentClient client,ILogger log)
{
log.LogInformation($"GetRockLocationsSpatial for coordinates");
Uri collectionUri = UriFactory.CreateDocumentCollectionUri("a","b");
Polygon rectangularArea = new Polygon(
new[]
{
new LinearRing(new [] {
new Position(100,50),new Position(130,40),new Position(100,50)
})
});
var rocks =client.CreateDocumentQuery<RockLocationDocument>(collectionUri).Where(a => a.location.Within(rectangularArea)).AsEnumerable();
foreach (RockLocationDocument rock in rocks)
{
Console.WriteLine("\t" + rock);
}
return new OkObjectResult(JsonConvert.SerializeObject(rocks));
}
}
}
解决方法
我不确定是什么原因导致您的错误。但是我可以用您的代码得到结果。您可以按照此进行尝试。
RockLocationDocument.cs
using Microsoft.Azure.Documents.Spatial;
namespace example
{
public class RockLocationDocument
{
public string id { get; set; }
public Point location { get; set; }
}
}
Founction.cs
我通过CreateDocumentQuery
方法传递FeedOption。
using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Azure.Documents.Client;
using Microsoft.Azure.Documents.Spatial;
using System.Linq;
namespace example
{
public static class Example
{
[FunctionName("example")]
public static async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function,"post",Route = "example")] HttpRequest req,[CosmosDB(
databaseName: "Test",collectionName: "site",ConnectionStringSetting = "sogo_DOCUMENTDB")]
DocumentClient client,ILogger log)
{
log.LogInformation($"GetRockLocationsSpatial for coordinates");
Uri collectionUri = UriFactory.CreateDocumentCollectionUri("Test","site");
Polygon rectangularArea = new Polygon(
new[]
{
new LinearRing(new [] {
new Position(100,50),new Position(130,40),new Position(100,50)
})
});
var rocks =client.CreateDocumentQuery<RockLocationDocument>(collectionUri,new FeedOptions { EnableCrossPartitionQuery = true }).Where(a => a.location.Within(rectangularArea)).AsEnumerable();
foreach (RockLocationDocument rock in rocks)
{
Console.WriteLine("\t" + JsonConvert.SerializeObject(rock));
}
return new OkObjectResult(JsonConvert.SerializeObject(rocks));
}
}
}
图书馆:
使用邮递员进行测试:
希望这可以为您提供帮助。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。