none
How to call WCF Data Service asynchronously when you are also implementing client callbacks?

    Question

  • Hi!  I have been learning how to do WCF Data Services lately and I hit a stumbling block on my code.

    What I have is an ASP.NET page that implements client callbacks to prevent the page from making a postback.  I can call the WCF Data Service with no problems in my code behind.  What I am having trouble is if I wanted to call the WCF Data Service asynchronously.

    Can somebody tell me if it is possible to call it in my codebehind?  I see code samples of asynchronous WCF calls that I can probably use.  What I am having trouble with those samples is the OnCompleteEvent.  How can I make it so that after doing the OnCompleteEvent, it sends data back into the client UI page?

    Tuesday, May 15, 2012 7:00 AM

Answers

  • Atually it seem that you are trying to update Web control (Presentation Element)  from Code Behind. This would have been feasible in any client application like console, WPF windows form etc where worker can take context of UI thread for rendering.

    In fact the code sample provided in link given by Allen  (How to: Execute Asynchronous Data Service Queries (WCF Data Services) is also assuming the same

    But your case is of Web Application in which UI thread is out of control of server code (Code Behind) so as a result you won`t be able to leverage WCF Data Service Callback as suggested in above MSDN link.

    Up to best of my knowledge In you case you have only two options 

    1. Use Client Callback that in turn will call Data Service
    2. Use JSON script directly in ASPX page to Call Data service (I wont`t suggest because it need lot of manual work, but could be a good solution for simple operation like GetPrice() )

    so you have only Option1 (Client Call Back) that is best suitable in your usecase , and one more thing prefer writing minimum callbacks if possible

    Ashwini 

    Monday, May 21, 2012 3:23 AM

All replies

  • well your problem is not very clear to me bit it seem that you are able to fetch the data but not able to update UI controls

    I would suggest to use AJAX patterns that will be more helpful in such scenario, Load you response in JSON object and then update page UI

    Alternatively look this article if you want to use ASP.Net Callbacks  http://msdn.microsoft.com/en-us/library/ms178208.aspx
    • Edited by Ashwini47 Tuesday, May 15, 2012 1:30 PM update
    Tuesday, May 15, 2012 7:17 AM
  • Hi bauey,

    Welcome to MSDN Forum.

    I'm not clear about the question. If you want to call WCF Data Service asynchronously, please refer to the links below.

    How to: Execute Asynchronous Data Service Queries (WCF Data Services)

    Asynchronous Operations (WCF Data Services)

    How to Call WCF Services Synchronously and Asynchronously

    If I misunderstand your concern, please feel free to let me know.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 17, 2012 8:36 AM
    Moderator
  • Hi Ashwini47,

    The link you provided is the client callback that I mentioned in my post.  I use that so that from the UI, I can go to my codebehind via the RaiseCallbackEvent method to call the methods on my WCF Data Services.  I then get whatever the WCF Data Service returns, concatenate the results into a long string that I can return back to the UI via the GetCallbackResult method.

    Are you saying that I would be better off calling the WCF Data Service directly from the UI via JSON?  I was hoping to keep it within the codebehind so that I don't expose how I call the WCF Data Service in my code.  

    Can you suggest any links that can do this via plain JavaScript?  I'm afraid that I am not that familiar with JSON code yet.

    Sunday, May 20, 2012 2:04 PM
  • Hi Allen_Li1988,

    Thanks for the links on how to call WCF Data Services asynchronously.  

    My problem here is how to marry the link that Ashwini47 gave on the client callbacks sample (http://msdn.microsoft.com/en-us/library/ms178208.aspx) to the sample you gave (How to: Execute Asynchronous Data Service Queries (WCF Data Services)).

    What I am having trouble with here is when I am in the UI and triggered an event causing me to fire up the RaiseCallbackEvent method in the codebehind.  I would then call the BeginExecuteCustomerQuery on your sample code.  Since it is called asynchronously, what will happen first?  Will the GetCallbackResult method be called first, thereby sending back to the UI a blank response?  Or will it wait for the OnCustomerQueryComplete to end before calling the GetCallbackResult method?  And would I have to explicitly call GetCallbackResult inside OnCustomerQueryComplete?  Or will the GetCallbackResult automatically be called after OnCustomerQueryComplete?

    Sunday, May 20, 2012 2:17 PM
  • Hi Bauey

    Actually you really do not need to club above two solution because Client callbacks are  it self async all you nee to raise a Synchronous call in the function that raise CallBack (Ref http://msdn.microsoft.com/en-us/library/ms178208.aspx) as below.

    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;
                    
            }
           
        }
        public String GetCallbackResult()
        {
    
            return returnValue;
        }

    I have simplified the code sample provided in above MSDN Link and customized to consumed Public Northwind OData Service and submitted to MSDN code gallery as and can download from  http://code.msdn.microsoft.com/How-to-call-WCF-Data-a25c61f0

    Please let me know it it helps


    • Edited by Ashwini47 Sunday, May 20, 2012 4:16 PM
    Sunday, May 20, 2012 4:15 PM
  • Hi Ashwini47,

    That is practically what I am doing right now.  My problem here is that doing client callbacks and calling the WCF Data Service synchronously feels really sluggish.  I was trying hoping on trying to call the WCF Data Service asynchronously instead but hit a wall on how to do it that way.

    Sunday, May 20, 2012 7:55 PM
  • Atually it seem that you are trying to update Web control (Presentation Element)  from Code Behind. This would have been feasible in any client application like console, WPF windows form etc where worker can take context of UI thread for rendering.

    In fact the code sample provided in link given by Allen  (How to: Execute Asynchronous Data Service Queries (WCF Data Services) is also assuming the same

    But your case is of Web Application in which UI thread is out of control of server code (Code Behind) so as a result you won`t be able to leverage WCF Data Service Callback as suggested in above MSDN link.

    Up to best of my knowledge In you case you have only two options 

    1. Use Client Callback that in turn will call Data Service
    2. Use JSON script directly in ASPX page to Call Data service (I wont`t suggest because it need lot of manual work, but could be a good solution for simple operation like GetPrice() )

    so you have only Option1 (Client Call Back) that is best suitable in your usecase , and one more thing prefer writing minimum callbacks if possible

    Ashwini 

    Monday, May 21, 2012 3:23 AM