Microsoft.Azure.WebJobs.Host: 索引方法错误 'HttpExample'

Microsoft.Azure.WebJobs.Host: Error indexing method 'HttpExample'

当 运行 我的 Azure Functions 应用程序出现以下错误时:

Microsoft.Azure.WebJobs.Host: Error indexing method 'HttpExample'. Microsoft.Azure.WebJobs.Host: Can't bind CosmosDB to type 'Microsoft.Azure.Documents.Client.DocumentClient'.

这是我在 Function1.cs 文件中的 运行() 函数:

        public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "policy")] HttpRequest req,
        [CosmosDB(Connection = "CosmosConnectionString")] DocumentClient cosmos,
        ILogger log)
    {
        log.LogInformation("C# HTTP trigger function processed a request.");

        
        var query = "SELECT * FROM Policy";
        
        var policies = GetDocumentsByQuery(
           cosmos,
           "AR",
           "Policy",
           query).ToList();

        return new OkObjectResult(policies);
    }

我将我的 CosmosConnectionString 作为用户密码传递。我将 Azure Functions v4 与 CosmosDB v4 预览版和 .NET Core 6 一起使用。我不知道为什么会收到此错误。我之前使用过 .NET Core 3.1 和早期版本的 Azure 函数 + CosmosDB,并且运行良好。

编辑:

public static IQueryable<dynamic> GetDocumentsByQuery(DocumentClient cosmos, string database, string collection, string query, string partitionKey = null)
    {
        try
        {
            FeedOptions feedOptions;
            if (partitionKey == null)
            {
                feedOptions = new FeedOptions { EnableCrossPartitionQuery = true, PopulateQueryMetrics = true };
            }
            else
            {
                feedOptions = new FeedOptions { PartitionKey = new PartitionKey(partitionKey), PopulateQueryMetrics = true };
            }

            var documentQuery =
            cosmos.CreateDocumentQuery(
                UriFactory.CreateDocumentCollectionUri(database, collection),
                query,
                feedOptions);

            return documentQuery;
        }
        catch (Exception)
        {
            throw;
        }
    }

您似乎绑定了错误的类型:

Can't bind CosmosDB to type 'Microsoft.Azure.Documents.Client.DocumentClient'

对于 4.x,使用 CosmosClient。摘自 HTTP trigger, get multiple docs, using CosmosClient

The following example shows a C# function that retrieves a list of documents. The function is triggered by an HTTP request. The code uses a CosmosClient instance provided by the Azure Cosmos DB binding, available in extension version 4.x, to read a list of documents. The CosmosClient instance could also be used for write operations.

using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.Extensions.Logging;

namespace CosmosDBSamplesV2
{
    public static class DocsByUsingCosmosClient
    {  
        [FunctionName("DocsByUsingCosmosClient")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post",
                Route = null)]HttpRequest req,
            [CosmosDB(
                databaseName: "ToDoItems",
                containerName: "Items",
                Connection = "CosmosDBConnection")] CosmosClient client,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var searchterm = req.Query["searchterm"].ToString();
            if (string.IsNullOrWhiteSpace(searchterm))
            {
                return (ActionResult)new NotFoundResult();
            }

            Container container = client.GetDatabase("ToDoItems").GetContainer("Items");

            log.LogInformation($"Searching for: {searchterm}");

            QueryDefinition queryDefinition = new QueryDefinition(
                "SELECT * FROM items i WHERE CONTAINS(i.Description, @searchterm)")
                .WithParameter("@searchterm", searchterm);
            using (FeedIterator<ToDoItem> resultSet = container.GetItemQueryIterator<ToDoItem>(queryDefinition))
            {
                while (resultSet.HasMoreResults)
                {
                    FeedResponse<ToDoItem> response = await resultSet.ReadNextAsync();
                    ToDoItem item = response.First();
                    log.LogInformation(item.Description);
                }
            }

            return new OkResult();
        }
    }
}