[OPEN] [#946] DirectMethod with async await

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1

    [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:
    1. 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.
    2. 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]

Similar Threads

  1. Async File Upload
    By Iguion in forum 2.x Help
    Replies: 4
    Last Post: Nov 28, 2012, 9:05 AM
  2. Async refresh page
    By fangmdu in forum 2.x Help
    Replies: 1
    Last Post: Jul 02, 2012, 8:03 PM
  3. [CLOSED] Async TreeGrid Example
    By Aparna_B in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: May 15, 2012, 5:19 PM
  4. Replies: 0
    Last Post: Apr 23, 2012, 5:50 AM
  5. Page Async Tasks
    By anand in forum 1.x Help
    Replies: 2
    Last Post: Jun 05, 2009, 2:28 PM

Tags for this Thread

Posting Permissions