I have an HTTP endpoint for returning data directly from Azure Cosmos DB. The endpoint is basically a database access point (this seems like a common case). Since I'm returning the data unmodified, there's no need to deserialize it, even though I have an object for Project on the server that is used elsewhere. I'm wondering if there are any ways to make this code more efficient, or tighter.

public async Task<IHttpActionResult> GetProjects()
    await ValidateUser();
    var cosmosDb = await Storage.GetCosmosDb();
    var queryDefinition = new QueryDefinition($"SELECT * FROM projects project WHERE project.organization = '{CurrentUser.Organization}'");
    var projectsQuery = cosmosDb.Containers[typeof(Project)]
    var projects = new JArray();
    while (projectsQuery.HasMoreResults)
        var responseMessage = await projectsQuery.ReadNextAsync();
        if (!responseMessage.IsSuccessStatusCode)
            return InternalServerError(new Exception(
                $"Query item from stream failed. Status code: {responseMessage.StatusCode} Message: {responseMessage.ErrorMessage}"));

        using (var streamReader = new StreamReader(responseMessage.Content))
        using (var jsonTextReader = new JsonTextReader(streamReader))
            var project = JObject.Load(jsonTextReader);

    return Ok(projects);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.