locked
Astoria RRS feed

  • Question

  • Hi,

     

    Wheather it is possible to execute queries Asynchronously in Astoria?

    Friday, September 21, 2007 4:26 PM

Answers

  • Do you mean from the client library? If that's the case, yes, the client API includes both synchronous and asynchronous entry points for submiting requests to an Astoria server.

     

    The "Using Astoria" document that comes with the September 2007 CTP includes a brief description of the API. Here is an example:

     

    Code Snippet

    WebDataContext ctx =
                      new WebDataContext("http://localhost:1234/Northwind.svc");

    WebDataQuery<Customer> q = ctx.CreateQuery<Customer>(
                                     "/Customers[City eq 'London']",
                                     QueryOption.IgnoreMissingProperties);

    q.BeginExecute(
        delegate(IAsyncResult ar)
        {
            foreach (Customer c in q.EndExecute(ar)) {
                Console.WriteLine(c.CompanyName);
            }
        },
        null);

     

     

    Pablo Castro

    Technical Lead

    Microsoft Corporation

    http://blogs.msdn.com/pablo


     

     

    Friday, September 21, 2007 4:52 PM
    Moderator
  • Hi,

     

    Once you switch to async execution -in Astoria or any other system that does async processing- you have to manage the fact that stacks may unwind before the execution finishes and that more sophisticated coordination between activities is required.

     

    Why do you want to use async in this case? Are you concerned about blocking threads? Is is a perf-related aspect? or a scalability one? or something else? It would be good to know in more detail what you're trying to achieve.

     

    To answer the specific question above: there are two main reasons that come to my mind to use async execution in a web page:

    a) to paralelize execution. in this case it's ok to block once you submited all of the requests. For that you can make all the Begin* calls and then use WaitAny/WaitAll to proceed once you have the answer. I discussed this technique in a similar problem space (it was SqlClient instead of the Astoria client, but the patterns still apply) in this article:

    http://msdn2.microsoft.com/en-us/library/ms379553(VS.80).aspx

     

    b) to unwind the asp.net thread and allow it to process another request while the remote execution takes place. For this case you need to use the async support in asp.net. There is an interesting article in MSDN magazine about async in asp.net, you can find it here:

    http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/

     

    Pablo Castro

    Technical Lead

    Microsoft Corporation

    http://blogs.msdn.com/pablo

     

     

    Tuesday, September 25, 2007 5:32 PM
    Moderator

All replies

  • Do you mean from the client library? If that's the case, yes, the client API includes both synchronous and asynchronous entry points for submiting requests to an Astoria server.

     

    The "Using Astoria" document that comes with the September 2007 CTP includes a brief description of the API. Here is an example:

     

    Code Snippet

    WebDataContext ctx =
                      new WebDataContext("http://localhost:1234/Northwind.svc");

    WebDataQuery<Customer> q = ctx.CreateQuery<Customer>(
                                     "/Customers[City eq 'London']",
                                     QueryOption.IgnoreMissingProperties);

    q.BeginExecute(
        delegate(IAsyncResult ar)
        {
            foreach (Customer c in q.EndExecute(ar)) {
                Console.WriteLine(c.CompanyName);
            }
        },
        null);

     

     

    Pablo Castro

    Technical Lead

    Microsoft Corporation

    http://blogs.msdn.com/pablo


     

     

    Friday, September 21, 2007 4:52 PM
    Moderator
  • Hi,

     

    Thanks, it worked well in the Console Application.

     

    I wrote the same piece of code in Web Application calling through client library.

    The output was, the page completed before the Async Query Execution completes. So I added the follg code:

    qCust.BeginExecute(

    delegate(IAsyncResult Async)

    {

    Async.AsyncWaitHandle.WaitOne();

    foreach (Customers c in qCust.EndExecute(Async))

    arrList.Add(c.ContactName);

    }

    , null);

     

    but even this didnt help me out.........

    Any Solution?

     

    Shaki

    Tuesday, September 25, 2007 12:14 PM
  • Hi,

     

    Once you switch to async execution -in Astoria or any other system that does async processing- you have to manage the fact that stacks may unwind before the execution finishes and that more sophisticated coordination between activities is required.

     

    Why do you want to use async in this case? Are you concerned about blocking threads? Is is a perf-related aspect? or a scalability one? or something else? It would be good to know in more detail what you're trying to achieve.

     

    To answer the specific question above: there are two main reasons that come to my mind to use async execution in a web page:

    a) to paralelize execution. in this case it's ok to block once you submited all of the requests. For that you can make all the Begin* calls and then use WaitAny/WaitAll to proceed once you have the answer. I discussed this technique in a similar problem space (it was SqlClient instead of the Astoria client, but the patterns still apply) in this article:

    http://msdn2.microsoft.com/en-us/library/ms379553(VS.80).aspx

     

    b) to unwind the asp.net thread and allow it to process another request while the remote execution takes place. For this case you need to use the async support in asp.net. There is an interesting article in MSDN magazine about async in asp.net, you can find it here:

    http://msdn.microsoft.com/msdnmag/issues/05/10/WickedCode/

     

    Pablo Castro

    Technical Lead

    Microsoft Corporation

    http://blogs.msdn.com/pablo

     

     

    Tuesday, September 25, 2007 5:32 PM
    Moderator
  • Hi,

     

    The above link helped me to solve the problem ............... Thanks.

    Actually I tried to execute queries asyncly in a Web Application. (not using AJAX)

     

    Now I need help in executing a stored procedure in Astoria.....

     

    Shaki

    Thursday, September 27, 2007 3:11 AM