[OPEN] [#946] DirectMethod with async await

  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 4:22 PM. Reason: [OPEN] [#946]
  2. #2
    Hello!

    We'll investigate it.
  3. #3
    Hi

    Well, direct methods were not designed for server side async handlers (and we never tested such scenario)
    I was able to make runable sample but I cannot guarantee that it will work in all cases
    <%@ Page Language="C#" Async="true" %>
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    
    <!DOCTYPE html>
    
    
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    
    
        <script runat="server">
            protected void Page_Load(object sender, EventArgs e)
            {
                RegisterAsyncTask(new PageAsyncTask(GetDmNumber));
            }
            
            [Ext.Net.DirectMethod]
            public async System.Threading.Tasks.Task<int> GetDmNumber(){
              try{
                  var num = await GetNumberAsync();
                 
                  int x = num + 10;
                  ResourceManager.DirectMethodResult = x;
                  return x;
              }
              catch(Exception ex){
                Ext.Net.ResourceManager.AjaxSuccess = false;
                Ext.Net.ResourceManager.AjaxErrorMessage = ex.Message;
              }
    
    
              return 0;
            }
            
            private async System.Threading.Tasks.Task<int> GetNumberAsync(){
               await System.Threading.Tasks.Task.Delay(1000);
               return 5;
            }
        </script>
    </head>
    <body>
        <form runat="server">
            <ext:ResourceManager runat="server" />
            
            <ext:Button runat="server" Text="Get result" Handler="App.direct.GetDmNumber({success: function(result){alert(result);}})">
            </ext:Button>
        </form>
    </body>
    </html>
    We will investigate possibility to add support of async direct methods in future releases
    Vladimir Shcheglov
    Sr. Developer
  4. #4
    Thanks for the info, Vlad.
    Registering the async DirectMethods in Page_Load worked in some of our cases but not all. Since DirectMethods don't officially support async, we'll go back to our old code.
    For a future release, I think adding support for async DirectMethods would be great.
  5. #5
    Can you post that invalid cases? It will be useful for us
    Vladimir Shcheglov
    Sr. Developer
  6. #6
    The failed cases involve calling a combination of database/wcf async function before returning a result. Since these are proprietary, I can't share them. I'll see if I can translate the cases to a generic example.
  7. #7
    We are closing the thread for now. If you are able to create a sample(-s) ever, please feel free to post regardless the thread is closed.

Similar Threads

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

Tags for this Thread

Posting Permissions