c# - Load lists from Entity Framework without make the controller wait -


i want load set of list @ begining of web app, use them later. these lists static , read data base using entity framework orm. idea load list @ begining (on home page after login) , use them on de app. don't want home page waiting list finish loagind. have tried several alternatives no 1 works (or goes sync or got errors).

1. first attempt: calling tolistasync() , await tolistasync (throw exception ef)

there 2 version of list 1 async , other sync:

private static task<list<empresa>> listaempresasasync; public static list<empresa> listaempresas 

i have defined function generates lists repository on ef

public async static task<list<t>> generateasynclistfromrepository(igenericrepositoryblockable<t> repository) {     iqueryable<t> queryasync = repository.getallactive();     return await queryasync.tolistasync(); } 

and other 1 function check result async calls:

public static list<t> forcetoloadasynclist(task<list<t>> task) {     task.wait();     return task.result; } 

the async load list:

listaempresasasync = taskmanager<empresa>.generateasynlistfromrepository((iempresarepositorio)diresolver.getservice(typeof(iempresarepositorio))); 

and when list needed force load:

public static list<empresa> listaempresas {         {         return taskmanager<empresa>.forcetoloadasynclist(listaempresasasync);                    } } 

this initial approach throws following error:

a second operation started on context before previous asynchronous operation completed

2. second: use tolist() , await end of task.run() (problem empty enumerables)

then tested use tolist() instead tolistasync:

return await task.run(() => { return queryasync.tolist(); }); 

i doesn't work either.

3. third: use tolist() , await generationlist force return list (behaviour similar sync approach. controller waiting lists loading)

following approach change signature of funcion return list:

return queryasync.tolist(); 

and await on load process

listaempresasasync = await taskmanager<empresa>.generateasynlistfromrepository((iempresarepositorio)diresolver.getservice(typeof(iempresarepositorio))); 

but working similar sync process, means on home page loading time high.

i know on ef, 1 async call allowed each context. want put these loading process on background if run sync, , when list needed check result of task.

any idea?

just clarification solution proposed yuval itzchakov should work when dbcontext not shared among loading lists calls, in scenario throws error related more 1 async call on entity framework.

there couple of things wrong code. first, need know when making async call using async-await, request doesn't complete until awaited operation returns. significant, because if need fire , forget style operation, wont do.

if want execute asynchronously (not fire , forget), following:

public static list<empresa> listaempresas  public static task<list<t>> generateasynclistfromrepository(igenericrepositoryblockable<t> repository) {     return repository.getallactive().tolistasync(); } 

and call this:

listaempresas = await taskmanager<empresa>.generateasynclistfromrepository((iempresarepositorio)diresolver.getservice(typeof(iempresarepositorio))); 

making 2 lists , using task.wait useless, aren't forcing other thread waiting on async method return, means runs synchronously.

if want fire , forget, note following:

using task.run in asp.net dangerous, doesn't register queued work asp.net threadpool, , exposed iis recycling may cause background work terminate unexpectedly. instead, if you're using .net 4.5.2 can use hostingenvironment.queuebackgroundworkitem registers queued task you. if not, stephan clearys backgroundtaskmanager


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -