Nov 06, 2013, 9:56 PM
[OPEN] [#946] DirectMethod with async await
I have a direct method that needs to retrieve information from several database tables before returning a result to the user. To improve performance we have implemented the data layer using the new async await functionality of .Net.
When using the data layer from within a DirectMethod we encountered one of two issues:
The following is a sample DirectMethod with a call to a delayed task to illustrate the first case:
When using the data layer from within a DirectMethod we encountered one of two issues:
- If we used the await by itself, the control returned to the DirectMethod invoke call and from there to the browser without the code getting a result. Which is expected since the DirectMethod Invoke code does not await a result from a task.
- If we used the await with ConfigureAwait(false) option, the control waited until we got the result but HttpContext.Current becomes null, as well as Extnet.ResourceManager properties. Resulting in System.NullReferenceException
The following is a sample DirectMethod with a call to a delayed task to illustrate the first case:
[Ext.Net.DirectMethod]
public async Task<int> GetDmNumber(){
try{
var num = await GetNumberAsync();
num +=10;
return num;
}
catch(Excption ex){
Ext.Net.ResourceManager.AjaxSuccess = false;
Ext.Net.ResourceManager.AjaxErrorMessage = ex.Message;
}
}
private async Task<int> GetNumberAsync(){
await Task.Delay(1000);
return 5;
}
The following is the code that wait for the return value from the async function but fails to maintain the HttpContext resulting in a NullReferenceException for the AjaxSuccess [Ext.Net.DirectMethod]
public async Task<int> GetDmNumber(){
try{
var num = await GetNumberAsync().ConfigureAwait(false);
num +=10;
throw Exception("test exception");
return num;
}
catch(Excption ex){
Ext.Net.ResourceManager.AjaxSuccess = false;
Ext.Net.ResourceManager.AjaxErrorMessage = ex.Message;
}
}
private async Task<int> GetNumberAsync(){
await Task.Delay(1000);
return 5;
}
Is there a way to use async await in a DirectMethod that will work?
Last edited by Daniil; Dec 02, 2015 at 3:22 PM.
Reason: [OPEN] [#946]