c# - Many nested AggregateExceptions -


working entity framework 7, made simple mistake linq (used skip , forgot include orderby clause).

the exception thrown included number of nested aggregate exceptions.

the code generates (and catches) exception is:

int[] newids; try {     newids = await db.products         .where(p => p.portalid == portalid)         .skip(ids.productids.count) //skip rows read         .take(taketotal) //get next block         .select(p => p.productid)         .toarrayasync(); } catch (aggregateexception ex) {     console.writeline(ex.message);     newids = new int[] { }; } 

the code above in repo class called asp.net 5 webapi controller. levels of call using async-await.

however aggregate exception got (this dumped immediate window catch block shown above):

system.aggregateexception: 1 or more errors occurred. ---> system.aggregateexception: 1 or more errors occurred. ---> system.aggregateexception: 1 or more errors occurred. ---> system.aggregateexception: 1 or more errors occurred. ---> system.aggregateexception: 1 or more errors occurred. ---> system.aggregateexception: 1 or more errors occurred. ---> system.invalidoperationexception: query containing skip operator must include @ least 1 orderby operation. @ microsoft.data.entity.relational.query.sql.defaultsqlquerygenerator.generatelimitoffset(selectexpression selectexpression) @ microsoft.data.entity.relational.query.sql.defaultsqlquerygenerator.visitselectexpression(selectexpression selectexpression) @ microsoft.data.entity.relational.query.expressions.selectexpression.accept(expressiontreevisitor visitor) @ microsoft.data.entity.relational.query.sql.defaultsqlquerygenerator.generatesql(selectexpression selectexpression, idictionary`2 parametervalues) etc etc

here actual exception has ended wrapped whole bunch of layers of aggregate exception (6 nested layers). understand why i'm getting aggregate exception, wondered why many of them? more since i'm looking @ exception before has bubbled controller entry-point.

would result of number of layers of async-await, (don't think have many 6) or issue in ef7 implementation?

this using ef 7 release 7.0.0-beta4.

as the msdn page on task<t> explains, exceptions thrown task wrapped in aggregateexception before being thrown awaiting code. if you're using multiple levels of async/await , not catching exception @ lowest possible level, each time bubbles level, it'll wrapped again, leading aggregateexception inside aggregateexception, 1 every time you're awaiting without catching.

it might each operation counted own task; ie. each time add operation, result comes out of previous 1 , down next, each 1 awaiting previous. take look:

newids = await db.products               // 1     .where(p => p.portalid == portalid)  // 2     .skip(ids.productids.count)          // 3     .take(taketotal)                     // 4     .select(p => p.productid)            // 5     .toarrayasync();                     // 6 

six layers of things, each awaiting result previous. 6 aggregateexception layers. now, exception caused third of six, nature of error, it's comes part ef reads whole query before doing of it, , while doing it's spotted you've got .skip() without matching .orderby().

as stephen cleary reminded me in comments, while things await return task<t>, degree of unwrapping you, await doesn't behave quite task<t>.result, meaning await should throw actual exception without wrapping in aggregateexception. means @ best have half answer here (which little awkward, seeing it's been accepted already). honestly, i'd suggest un-accept answer others don't skip on question, , see if else knows might fill gaps.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -