不支持Azure Cosmos DB查询

时间:2020-09-21 22:22:27

标签: c# linq .net-core azure-functions azure-cosmosdb

我创建了一个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, 50),
                        new Position(130, 40),
                        new Position(100, 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));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我不确定是什么原因导致您的错误。但是我可以用您的代码得到结果。您可以按照此进行尝试。

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, 50),
                        new Position(130, 40),
                        new Position(100, 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));
        }
    }
}

图书馆:

enter image description here

使用邮递员进行测试:

enter image description here

希望这可以为您提供帮助。