Async Await example

7 posts, 0 answers
  1. Brent
    Brent avatar
    33 posts
    Member since:
    Jun 2010

    Posted 14 Sep 2012 Link to this post

    Could you provide an example of how to use the new .NET 4.5 Async and Await in conjunction with ORM WCF Data Services.  I don't see any examples out and I have been searching for a while out on the net.


  2. Ivailo
    Admin
    Ivailo avatar
    318 posts

    Posted 18 Sep 2012 Link to this post

    Hello Brent,

    Unfortunately we do not have such sample application. 

    The reason why we have not provided any guidance for the time being is that all the specifics of our WCF Data Services implementation lie within the host. On the client side, you would use those new features in .NET 4.5 in exactly the same manner as you would use them if the you had the Microsoft implementation of Data Services on the server side. 

    In the long run we are planning to provide more resources on how to use WCF services, so your request will be considered. However, we cannot yet give you a release date for an example using async and await.

    We believe that the following links might help you on the topic, as they are fully valid for OpenAccess Data Services as well:

    http://arbel.net/2011/06/04/wcf-tasks/ 
    http://stackoverflow.com/questions/8968112/task-based-data-service-in-silverlight 
    http://dotnet.dzone.com/articles/visual-studio-11-based-async  

    Let us know if you experience any issues with your implementation. 


    All the best,
    Ivailo
    the Telerik team
    Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!
  3. DevCraft banner
  4. Brent
    Brent avatar
    33 posts
    Member since:
    Jun 2010

    Posted 09 Oct 2012 Link to this post

    Sorry,
      I have looked at the links and I'm just not following what i need to do create a "wrapper" on the client portion to enable using await for my queries.  Am I modifying the service definitions or am I creating something new?

    Brent 
  5. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 12 Oct 2012 Link to this post

    Hi Brent,

    Data Service client generated by Add Service Reference tool (or svcutil.exe) already defines very convenient ways of handling calls asynchronously. My suggestion is to look how to use the existing API.

    While plain WCF Services proxy can be modified and wrapped to expose Begin/EndOperation() methods in Tasks and use async/await keyword, this is hardly possible in Data Services.
    I have make several attempts to implement such wrapper for Data Services, but the code gets complicated and requires several acrobatics in order to work (and there are no guarantees that it works flawlessly all the time). From my point of view there is very little return of investment in using Tasks and async/await with Data Services. Of course the situation in you project can turn the scales in different direction. 

    Getting back to your questions - you do not need to modify any of the existing server code to enable asynchronous client proxy generation - this is a feature of svcutil.exe utility.

    Please let us know why do you need async/await with Data Services in order to enable us to give you meaningful work around or try to push through the current limitations.

    Regards,
    Viktor Zhivkov
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  6. Brent
    Brent avatar
    33 posts
    Member since:
    Jun 2010

    Posted 12 Oct 2012 Link to this post

    Sure,
      My goal in using the async await was to be able to pause execution of code while waiting for results to come back and also  freeing up the UI thread.  I have a method that has multiple steps to it and hence multiple odata queries.  Instead of having 5-10 methods/functions/events making up the complete process.  I was hoping to utilize the await keyword and keep all of my code within the same sub.  
      I am currently converting a WPF application to silverlight and am trying to keep the code base as close as possible.

    I am currently using the DataServiceCollection and populating with a linq query,  I am then handling the callback.  I do however have several steps which is causing multiple callbacks and branches.


    Brent
  7. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 17 Oct 2012 Link to this post

    Hello Brent,

    Excuse me for the late reply, but I will need a bit more time to send you properly working sample using Tasks and async/await.
    I have been able to do some progress, but still have some things to iron out before the code is ready.
    Having Q3 2012 shipped today, tomorrow I will finish the code and post it so you can evaluate it.

    Thanks you for your patience.

    Kind regards,
    Viktor Zhivkov
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  8. Viktor Zhivkov
    Admin
    Viktor Zhivkov avatar
    291 posts

    Posted 18 Oct 2012 Link to this post

    Hello Brent,

    At last I managed to implement Task-based calling of Data Services in Silverlight.

    I have prepared a small demo application that exposes Sofia Car Rental database (which can be found in the attached zip file) through Data Services v3 (same code should work for v2 too).
    There you can find the code that does the magic:
    01.private async Task<IEnumerable<Customer>> LoadDataUsingTasks()
    02.{
    03.    DataServiceQuery<Customer> query = dataModel.Customers;
    04. 
    05.    var task = Task.Factory.FromAsync(new Func<AsyncCallback, object, IAsyncResult>(query.BeginExecute),
    06.                                      new Func<IAsyncResult, IEnumerable<Customer>>(query.EndExecute),
    07.                                      null, TaskCreationOptions.None);
    08. 
    09.    return await task;
    10.}

    The most important bit is Line 5 - I am creating a task using Task.FromAsync(...) method that is introduced to handle exactly this situation.

    I have added two more examples that handle consuming data using the Begin/End API and the "default" API that uses LoadAsync(...) method so you can compare them and choose the one that suits you most.
    Please note that the provided code is "barebone" and requires polishing and handling of errors.

    I believe based on this sample you will be able to create a wrapper around the generated service client proxy in order to hide these implementation details.

    Thank you for your patience! We will be happy if you share your feedback on the provided code.

    Regards,
    Viktor Zhivkov
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
Back to Top
DevCraft banner