locked
Asynchronous ASP.NET Page Processing(PageAsyncTask) in Layered Architecture RRS feed

  • Question

  • User-1598237142 posted

    I am working on an application which involves payment gateways. Payment gateway takes some 4-5 seconds to respond. To tune the application I thought of using PageAsyncTask whenever Payments are processed.

    My application uses multiple layers, for e.g before hitting to payment gateway, all sorts of calculations(taxes and all) are calculated in bussiness layer and then the payment gateway processing is done in the data access layer. Method which performs the Payments  also returns some custom message to top layers based on the response value. This is done by setting a property in the DAL class.

    Now issue is how to register BeginEventHandler and EndEventHandler. I tried creating a sample application for this. But when I debugged my EndEventHandler was not getting called at all.

    Sample Scenario..

    On a button click event I am trying to call a DAL method which will create a HttpWebRequest and will return the response to me in a string variable

    My Page Class has Events below:

    protected void btnSubmit_Click(object sender, EventArgs e)
            {
                
                PageAsyncTask task = new PageAsyncTask(new BeginEventHandler(BeginAsyncOperation), new EndEventHandler(EndAsyncOperation),new EndEventHandler(TimeoutAsyncOperation), "" );
                RegisterAsyncTask(task);
            }
    
            protected IAsyncResult BeginAsyncOperation(object sender, EventArgs e, AsyncCallback cb, object state)
            {
    
                DBMimic dbmimic = new DBMimic();
                return(dbmimic.ReadWebRequestAsnyc());
                
             
            }
    
            
        protected void EndAsyncOperation(IAsyncResult ar)
    
        {
    
            // Get results of async operation
    
            
          
        }
    
     
    
       protected void TimeoutAsyncOperation(IAsyncResult ar)
    
        {
    
            // Called if async operation times out (@ Page AsyncTimeout)
    
           
    
        }




     

    DBmimic is a class in Data access Layer which has following methods

        public class DBMimic
        {
    
            public string ResponseCode
            {
                get;
                set;
            }
        
    
    
            /// <summary>
            /// Read Web Request Asyn
            /// </summary>
            /// <returns></returns>
            public IAsyncResult ReadWebRequestAsnyc()
            {
                string postData = "";
                    
                HttpWebRequest httpwebRequest = HttpWebRequest.Create("http://www.infinite-usa.com") as HttpWebRequest;
                httpwebRequest.Method = "POST";
                httpwebRequest.Timeout = 36000;
                //webRequest.Method = method == "" || method == null ? "POST" : method; // GET/POST
                httpwebRequest.ContentLength = postData.Length;
                httpwebRequest.ContentType = "";
                
          
                return( httpwebRequest.BeginGetResponse(new AsyncCallback(CallBack_BeginGetRequestStream), httpwebRequest));
                
            }
    
            private void CallBack_BeginGetRequestStream(IAsyncResult iAsyncResult)
            {
               Object o = iAsyncResult;
               HttpWebRequest webRequest=iAsyncResult.AsyncState as HttpWebRequest;
               WebResponse webResponse = webRequest.EndGetResponse(iAsyncResult);
               Stream stream=  webResponse.GetResponseStream();
               StreamReader streamReader = new StreamReader(stream);
               ResponseCode = streamReader.ReadToEnd();
               //String postResponse;
               //HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
               //using (StreamReader responseStream = new StreamReader(webResponse.GetResponseStream()))
               //{
               //    postResponse = responseStream.ReadToEnd();
               //    responseStream.Close();
               //}
            
                
    
            }
    
        }

    Now when I run this.. EndAsyncOperation in the page never gets called Infact everytime.. TimeoutAsync operation is called. Is this because of the delegate that I have used in BeginEndResponse in the DAL method?.

    Please guide.


    Sunday, October 3, 2010 4:23 AM

Answers

  • User-952121411 posted

    I am working on an application which involves payment gateways. Payment gateway takes some 4-5 seconds to respond. To tune the application I thought of using PageAsyncTask whenever Payments are processed.
     

    I am all for async pages and operations in ASP.NET at times, but I do not think making the operation of calling a payment gateway should be done asynchronously. Really what are you going to allow the user do anyway if control returned once the async call was made? You definitely don;t want them to be able to do anything on the page as this could cause interruption  This is one time you do want a blocking synchronous call where control is completely server side until that response comes back from the payment gateway.  You don;t even want to do little side tasks while waiting for the payment gateway response because I know business and database rules will be completely different based on the response you receive.  You don;t want to waist time doing anything until you know for sure the response.

    I make these comments from experience as I have coded against PayPal's payment gateway, and I call it in a synchronous manner; no processing or decisions are made until that response comes back because the logic is very different based on the outcome of the payment attempt. I just think this should naturally be a blocking call that does nothing until the response comes back.

    Now with that said in general of just wiring up the callback methods and getting them to work, have a look at the following links which may help you out a bit with your code:

    Asynchronous Pages in ASP.NET:

    http://msdn.microsoft.com/en-us/magazine/cc163725.aspx

    Calling Synchronous Methods Asynchronously:

    http://msdn.microsoft.com/en-us/library/2e08f6yc(v=VS.100).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 5, 2010 3:17 PM

All replies

  • User-952121411 posted

    I am working on an application which involves payment gateways. Payment gateway takes some 4-5 seconds to respond. To tune the application I thought of using PageAsyncTask whenever Payments are processed.
     

    I am all for async pages and operations in ASP.NET at times, but I do not think making the operation of calling a payment gateway should be done asynchronously. Really what are you going to allow the user do anyway if control returned once the async call was made? You definitely don;t want them to be able to do anything on the page as this could cause interruption  This is one time you do want a blocking synchronous call where control is completely server side until that response comes back from the payment gateway.  You don;t even want to do little side tasks while waiting for the payment gateway response because I know business and database rules will be completely different based on the response you receive.  You don;t want to waist time doing anything until you know for sure the response.

    I make these comments from experience as I have coded against PayPal's payment gateway, and I call it in a synchronous manner; no processing or decisions are made until that response comes back because the logic is very different based on the outcome of the payment attempt. I just think this should naturally be a blocking call that does nothing until the response comes back.

    Now with that said in general of just wiring up the callback methods and getting them to work, have a look at the following links which may help you out a bit with your code:

    Asynchronous Pages in ASP.NET:

    http://msdn.microsoft.com/en-us/magazine/cc163725.aspx

    Calling Synchronous Methods Asynchronously:

    http://msdn.microsoft.com/en-us/library/2e08f6yc(v=VS.100).aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 5, 2010 3:17 PM
  • User-333284963 posted

    I am all for async pages and operations in ASP.NET at times, but I do not think making the operation of calling a payment gateway should be done asynchronously. Really what are you going to allow the user do anyway if control returned once the async call was made?

    There's another good reason to use async pages that you seem to have overlooked: to return the thread to the thread pool on the PreRender event and then grab a new thread after the lengthy operation is completed to return the results.

    Friday, July 8, 2011 3:09 PM