public async Task <T> GetDocumentAsync <T>(string id)
        {
            try
            {
                var result = await _cosmosDBClient.ReadDocumentAsync <T>(UriFactory.CreateDocumentUri(_dbSetting.DatabaseId, _dbSetting.CollectionId, id),
                                                                         CreateRequestOptions("*"));

                return(result.Document);
            }
            catch (DocumentClientException e)
            {
                if (e.Error.Code == "NotFound")
                {
                    return(default(T));
                }
                else
                {
                    throw;
                }
            }
        }
示例#2
0
        private static async Task CreateActiveTransactionsDocIfNotExists(ActiveTransactions activeTransactionsDoc)
        {
            var client = new DocumentClient(new Uri(Endpoint), AuthKey);
            Uri collectionLink = UriFactory.CreateDocumentCollectionUri(DbName, CollectionName);
            bool needToCreate = false;

            try
            {
                await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(DbName, CollectionName, activeTransactionsDoc.Id.ToString()));
            }
            catch (DocumentClientException de)
            {
                if (de.StatusCode != HttpStatusCode.NotFound)
                {
                    throw;
                }
                else
                {
                    needToCreate = true;
                }
            }
            if (needToCreate)
            {
                await client.CreateDocumentAsync(collectionLink, activeTransactionsDoc);
            }
        }