从.net调用DocumentDb存储过程

Iza*_*nus 7 c# stored-procedures azure azure-cosmosdb

我有以下存储过程,只能通过id找到一个对象.

function sample(id) {
    var context = getContext();
    var response = context.getResponse();
    var collection = context.getCollection();

    var findObject = "SELECT * FROM Objects o where o.userId='" + id +"'";

    // Query documents and take 1st item.
    var isAccepted = collection.queryDocuments(
        collection.getSelfLink(),
        findObject,

        function (err, feed, options) {
            if (err) throw err;

            // Check the feed and if empty, set the body to 'no docs found', 
            // else take 1st element from feed
            if (!feed || !feed.length) throw new Error("Object not found");
            else response.setBody(feed[0]);
        });

    if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
Run Code Online (Sandbox Code Playgroud)

这是扩展Document的C#类

public class UserPoints: Document
{
    [JsonProperty("userId")]
    public Guid UserId;

    [JsonProperty("remainingPoints")]
    public int RemainingPoints;
}
Run Code Online (Sandbox Code Playgroud)

在我的main函数中,我调用上面的存储过程并期望它返回UserId的UserPoints对象.

例如:

UserPoints points = new UserPoints()
{
    UserId = Guid.NewGuid(),
    RemainingPoints = 1000
};

await client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri(databaseName, collection), points);
points.Dump();

var response = await client.ExecuteStoredProcedureAsync<UserPoints>(UriFactory.CreateStoredProcedureUri(databaseName, collection, storedProcedureName), points.UserId.ToString());
response.Response.Dump();
Run Code Online (Sandbox Code Playgroud)

我得到以下异常当我执行存储过程时,无法将"Microsoft.Azure.Documents.Document"类型的对象强制转换为"UserPoints"类型

如果我只是停止扩展Document基类,那么一切正常,但是我无法访问更新所需的SelfLink属性.难道我做错了什么?如果ExecuteStoredProcedureAsync采用强类型,它不应该能够键入强制类型并返回该类型的对象吗?

Ara*_* R. 0

这是 DocumentDB .NET SDK 中的一个错误。我们正在研究修复方案。

请注意,作为发布修复程序之前的解决方法,您不必从 Document 派生即可将文档存储在 DocumentDB 中。如果您需要访问某些内部属性(例如 Id),您可以将这些属性添加到您的对象中,例如

 [JsonProperty("id")] public string Id {get; set;}
Run Code Online (Sandbox Code Playgroud)