Calling DocumentDb Stored Procedures from .net

I have the following stored procedure that simply finds an object by 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.'); } 

And this is my C # class that extends Document

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

In my main function, I call the above stored procedure and expect it to return a UserPoints object for UserId.

For instance:

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

I get the following exception Cannot use an object of type "Microsoft.Azure.Documents.Document" to enter "UserPoints" when executing a stored procedure

Everything works fine if I just stopped extending the Document base class, but then I do not have access to the SelfLink property that I need for updates. Am I doing something wrong? If ExecuteStoredProcedureAsync accepts a strong type, should it not introduce it and return an object of that type?

+5
source share
1 answer

This is a bug in the DocumentDB.NET SDK. We are studying the fix.

Please note that as a workaround until the fix is ​​released, you do not need to output from the document to store documents in DocumentDB. If you need access to some internal properties, such as Id, you can add these properties to your object, for example. a

  [JsonProperty("id")] public string Id {get; set;} 
0
source

Source: https://habr.com/ru/post/1247238/


All Articles