Warm tip: This article is reproduced from stackoverflow.com, please click
asp.net-core azure azure-cosmosdb c# stored-procedures

ExecuteStoredProcedureAsync() returning different results

发布于 2020-03-29 12:48:20

Long story short, a stored procedure in cosmosDB is returning 2 when executed inside the portal, and is returning 0 when called from ExecuteStoredProcedureAsync() in my c# console app. The correct response is 2

Here's the stored procedure:

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);
        }
    );
}

When I run this from the azure portal, it returns the correct result: 2.

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

Here's the C# call:

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);
    }
}

When I run this from the C# console app, it returns 0

What's the deal?

Questioner
Chris Phillips
Viewed
99
Jack Jia 2020-01-31 17:45

Based on my test, you may not set the PartitionKey correctly.

If you have set partition key, you need to pass the correct partition key.

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

If there is no partition key, then you need to pass 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