We have a business scenario where a client sends us a request, then our backend service inserts multiple documents at the same time and returns the most recent CosmosDB's session token. The logic looks roughly similar to this:
// Controller Code
[HttpPost]
public async Task<IActionResult> Demo([FromBody] User[] users)
{
// Create multiple documents here
var tasks = new List<Task>();
foreach(var user in users)
{
tasks.add(this.container.CreateItemAsync(user, new PartitionKey("my partitionKey")));
}
// task when all returns an array of ItemResponses
ItemResponse<User> [] usersItemResponses = await Task.WhenAll(tasks);
// I could extract the session tokens associated with each ItemResponse
List<string> sessionTokens = usersItemResponses.Select(response => response.Headers.Session)
// Here I would like to return the newest session token to the client side, but because the tasks were executed in parallel, I wouldn't know which request finished last, and therefore no way to get the latest session token.
string sessionTokenString;
return this.Ok(sessionTokenString);
}
However, I cannot find documentation for how to compare session tokens, and I do not want to do these write operations in sequence.
How do we go about comparing the session tokens of these write operations and return the latest session token to our client so that they can read the db data in subsequent requests?