c# - ExecuteStoredProcedureAsync() 返回不同的结果

标签 c# azure asp.net-core stored-procedures azure-cosmosdb

长话短说,cosmosDB 中的存储过程在门户内执行时返回 2,而在我的 c# 控制台应用程序中从 ExecuteStoredProcedureAsync() 调用时返回 0。 正确答案是2

这是存储过程:

JS:

function countItems() {
    var context = getContext();
    var collection = context.getCollection();
    var collectionLink = collection.getSelfLink();
    var response = context.getResponse();

    var query = "SELECT * FROM c";

    var isAccepted = collection.queryDocuments(
        collectionLink,
        query,
        function(err, documents, responseOptions) {
            if (err) {
                throw err;
            }
            response.setBody(documents.length);
        }
    );
}

当我从 azure 门户运行此命令时,它返回正确的结果:2。

//////////////////////

这是 C# 调用:

C#

private static async Task ExecuteStoredProc(string spId, CosmosContext cosmosContext)
{
    using (var client = new CosmosClient(cosmosContext.Endpoint, cosmosContext.MasterKey))
    {
        var container = client.GetContainer(cosmosContext.DbId, cosmosContext.ContainerId);
        var scripts = container.Scripts;
        var pk = new PartitionKey(cosmosContext.DbId);
        var result = await scripts.ExecuteStoredProcedureAsync<string>(spId, pk, null);
        var message = result.Resource;

        Console.WriteLine(message);
    }
}

当我从 C# 控制台应用程序运行它时,它返回 0

这是怎么回事?

最佳答案

根据我的测试,您可能没有正确设置PartitionKey

如果您设置了分区键,则需要传递正确的分区键。

static void Main(string[] args)
{
    using (var client = new CosmosClient(Endpoint, Key))
    {
        // With Partition Key
        var container = client.GetContainer("TestDB", "Demo");

        var scripts = container.Scripts;

        // With Partition Key
        var pk = new PartitionKey("B");
        var result =scripts.ExecuteStoredProcedureAsync<string>("length", pk, null).GetAwaiter().GetResult();

        var message = result.Resource;
        Console.WriteLine(message);
    }
    Console.ReadLine();
}

enter image description here

enter image description here

如果没有分区键,则需要传递PartitionKey.None

static void Main(string[] args)
{
    using (var client = new CosmosClient(Endpoint, Key))
    {
        // Without Partition Key
        var container = client.GetContainer("ToDoList", "Items");

        var scripts = container.Scripts;

        //Without Partition Key
        var result = scripts.ExecuteStoredProcedureAsync<string>("length", PartitionKey.None, null).GetAwaiter().GetResult();

        var message = result.Resource;
        Console.WriteLine(message);
    }
    Console.ReadLine();
}

enter image description here

enter image description here

关于c# - ExecuteStoredProcedureAsync() 返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59996385/

相关文章:

c# - 如何伪造Http post?

c# - "smooth"随机数的算法

c# - 如何在 LINQ to Entities 中重用字段过滤器

c# - 将 Application Insights 与单元测试结合使用?

dependency-injection - .net核心依赖注入(inject)在构造函数上带有参数

c# - 将具体实现作为通用返回

node.js - 浏览器关闭时 Passport session 不会被破坏

azure - 无法获取 Azure Devops Workitem 布局

c# - 解决错误 - "An item with the same key has already been added when returning 404"

azure - Azure.Fluent.Management 和 Azure.Storage.Blobs 包之间存在冲突吗?