c# - Is there a way to prevent the DocumentDB SDK from throwing exceptions and handle errors with status code? -
i trying hand @ .net documentdb sdk , went on testing realised trying document using documentclient.readdocumentasync throw notfoundexception if document not exist. behaviour seems odd me based on documentation every call should return resourceresponse object has statuscode property. has been able prevent exceptions being thrown configuration or other , rely on resourceresponse.statuscode property ?
here code sample:
private idocumentclient _documentclient = ... // ... public async task<dynamic> readdocument(string databasename, string collectionname, string documentid, string partitionkey) { return await _documentclient.readdocumentasync( urifactory.createdocumenturi( databasename, collectionname, documentid), new requestoptions { partitionkey = new partitionkey(partitionkey) }); }
i use extension particular case:
/// <summary> /// gets first result /// </summary> /// <typeparam name="t">type of class</typeparam> /// <param name="source">queryable take 1 from</param> /// <returns></returns> public static t takeone<t>(this iqueryable<t> source) { var documentquery = source.asdocumentquery(); if (documentquery.hasmoreresults) { var queryresult = documentquery.executenextasync<t>().result; if (queryresult.any()) { return queryresult.single<t>(); } } return default(t); } it returns document or null:
var document = client.createdocumentquery<user>("the_collection_uri",new feedoptions() { maxitemcount = 1 }).where(x => x.id == "some_id").takeone();
Comments
Post a Comment