c# - Async LINQ queries null when using Entity Framework 6 with Azure SQL? -


i have web application moved azure. in have quite few queries use ef's async linq extensions (examples below). since migrating azure, however, have noticed not of these queries returning null, miniprofiler isn't registering them:

// returns null. no queries logged in miniprofiler var someuser = await context.users.firstordefaultasync(x => x.id == id)  // works 100% expected var someuser = context.users.firstordefault(x => x.id == id) 

i've tried "tricking" calling .tolistasync().firstordefault(). of course, didn't work either.

for it's worth, app uses .net 4.5 , ef6. has been running in traditionally-hosted production environment ~18 months.

i'm sure has been answered somewhere, i've been having tough time finding information on it.


edit: id in example above coming separate method gets current user id current principal:

// .getuserid() extension method userid principal // works sql server // not work azure sql var someuser = await context.users.firstordefaultasync(x => x.id == httpcontext.current.getuserid()); 

to fix this, pulled call out:

var userid = httpcontext.current.getuserid(); var someuser = await context.users.firstordefaultasync(x => x.id == userid); 

still, though, know why works traditional sql server, not when connecting azure sql database?

when executing async code ef6 uses .configureawait(false) prevent deadlocks, continuation (the method body after await) no longer guaranteed execute same thread.

when running on local sql server time thread blocked below threshold , therefore continuation executed synchronously avoid context switching. sql azure has more latency , triggers proper asynchronous execution resulting in context being lost in case.

asp.net sets httpcontext.current on request thread, when ef evaluates lambda on thread pool thread null.


Comments