Tuesday, May 22, 2012

How to call WCF Data Service Asynchronously (async Mode) using ASP.Net Callbacks

We can use ASP.Net Client Callbacks to invoke Data Service in Asynchronous mode without freezing the UI .

Alternative Titles
Async ASP.Net and WCF Data Service 

Instrucutions Step By Step
C#
 public void RaiseCallbackEvent(String eventArgument) 
    { 
        returnValue = "Result from Async Northwind Serice Call"+Environment.NewLine; 
        NorthwindEntities nwEntities =  
            new NorthwindEntities(new Uri("http://services.odata.org/Northwind/Northwind.svc/")); 
         
        foreach (Product item in nwEntities.Products) 
        { 
            returnValue = returnValue + String.Format("ProductID={0} ProductName={1} Price={2}",  
                item.ProductID, item.ProductName, item.UnitPrice) + Environment.NewLine; 
                 
        } 
        
    }
Run your application and navigate to http://localhost:22438/WebSite1/ClientCallback.aspx
How Does It Work 
  • Actaually function LookUpStock() raise an Async call to server 
  • Call is Routed to ' public void RaiseCallbackEvent(String eventArgument)"
  • This function actually raise a query to Northwind Database and build an string with list of product 
  • This list is returned to server using client call back " public String GetCallbackResult()"
Note : Based on data it may take long time to the result to be appered on UI

No comments:

Post a Comment