none
Help with Asnyc Method RRS feed

  • Question

  • I need some help on how to convert this method into an asnyc method.   Here is the code.

            public   void GetValue()
            {
                var ctx = new CoinCatalogEntities(new Uri("http://localhost:54405/WcfDataService1.svc"));
                var query =  (from c in ctx.USsingle
                             select new USsingleResults()
                             {
                                 CoinID = c.CoinID,
                                 Checkbox = (bool)c.Checkbox,
                                 Year = c.Year,
                                 Description = c.Description,
                                 Type = c.Type,
                                 Value = c.Value,
                                 G = c.G,
                                 VG = c.VG,
                                 F = c.F,
                                 VF = c.VF,
                                 XF = c.XF,
                                 AU = c.AU,
                                 MS60 = c.MS60,
                                 MS63 = c.MS63,
                                 MS64 = c.MS64,
                                 MS64FB = c.MS64FB,
                                 MS65 = c.MS65,
                                 MS65FB = c.MS65FB
                             }).ToList<USsingleResults>();
    
    
                
    
                Value = query;
    
            }

    Tuesday, February 11, 2014 6:45 PM

Answers

  • Hello,

    Just assuming my Execute() as your GetValue() and replace my query with yours:

    public   void GetValue()
    
            {
    
       var ctx = new CoinCatalogEntities(new Uri("http://localhost:54405/WcfDataService1.svc"));
    
                DataServiceQuery< USsingleResults > query = (DataServiceQuery< USsingleResults>)(from c in ctx.USsingle
    
                             select new USsingleResults()
    
                             {
    
                                 CoinID = c.CoinID,
    
                                 Checkbox = (bool)c.Checkbox,
    
                                 Year = c.Year,
    
                                 Description = c.Description,
    
                                 Type = c.Type,
    
                                 Value = c.Value,
    
                                 G = c.G,
    
                                 VG = c.VG,
    
                                 F = c.F,
    
                                 VF = c.VF,
    
                                 XF = c.XF,
    
                                 AU = c.AU,
    
                                 MS60 = c.MS60,
    
                                 MS63 = c.MS63,
    
                                 MS64 = c.MS64,
    
                                 MS64FB = c.MS64FB,
    
                                 MS65 = c.MS65,
    
                                 MS65FB = c.MS65FB
    
                             });
    
                try
    
                {
    
                    // Begin query execution, supplying a method to handle the response 
    
                    // and the original query object to maintain state in the callback.
    
                    query.BeginExecute(OnOrderQueryComplete, query);
    
                }
    
                catch (DataServiceQueryException ex)
    
                {
    
                    throw new ApplicationException("An error occurred during query execution.", ex);
    
                }
    
            }
    
            private void OnOrderQueryComplete(IAsyncResult result)
    
            {
    
                // Get the original query from the result.
    
                DataServiceQuery< USsingleResults > query = (DataServiceQuery< USsingleResults >)result.AsyncState;
    
                foreach (USsingleResults result in query.EndExecute(result))
    
                {
    
    //…
    
                }
    
            }

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, February 13, 2014 9:37 AM
    Moderator

All replies

  • Hello,

    From your description, you want execute asynchronous data service queries. In WCF Data Service, we usually use the BeginExecute() and EndExecute() to implement asynchronous data service queries. Here I made an example and please check it:

    internal void Execute()
    
            {
    
                WCFDBEntities db = new WCFDBEntities(new Uri("http://localhost:34019/WcfDataService1.svc/"));
    
                DataServiceQuery<Order> query = (DataServiceQuery<Order>)(from order in db.Orders select order);
    
                try
    
                {
    
                    // Begin query execution, supplying a method to handle the response 
    
                    // and the original query object to maintain state in the callback.
    
                    query.BeginExecute(OnOrderQueryComplete, query);
    
                }
    
                catch (DataServiceQueryException ex)
    
                {
    
                    throw new ApplicationException("An error occurred during query execution.", ex);
    
                }
    
                for (int i = 0; i < 10; i++)
    
                {
    
                    Console.WriteLine(i);
    
                    Thread.Sleep(1000);
    
                }
    
            }
    
            private void OnOrderQueryComplete(IAsyncResult result)
    
            {
    
                // Get the original query from the result.
    
                DataServiceQuery<Order> query = (DataServiceQuery<Order>)result.AsyncState;
    
                foreach (Order Order in query.EndExecute(result))
    
                {
    
                    Console.WriteLine("Order Name: {0}", Order.OrderName);
    
                }
    
            }
    

    The result:

    If you want to know more about it, please check link below:

    http://msdn.microsoft.com/en-us/library/dd756365(v=vs.110).aspx

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 12, 2014 3:03 AM
    Moderator
  • Thanks for the reply.  I have also found this sample online but I don't understand how I would use it in my code. 

    Thanks,

    Bryan

    Wednesday, February 12, 2014 3:05 PM
  • Hello,

    Just assuming my Execute() as your GetValue() and replace my query with yours:

    public   void GetValue()
    
            {
    
       var ctx = new CoinCatalogEntities(new Uri("http://localhost:54405/WcfDataService1.svc"));
    
                DataServiceQuery< USsingleResults > query = (DataServiceQuery< USsingleResults>)(from c in ctx.USsingle
    
                             select new USsingleResults()
    
                             {
    
                                 CoinID = c.CoinID,
    
                                 Checkbox = (bool)c.Checkbox,
    
                                 Year = c.Year,
    
                                 Description = c.Description,
    
                                 Type = c.Type,
    
                                 Value = c.Value,
    
                                 G = c.G,
    
                                 VG = c.VG,
    
                                 F = c.F,
    
                                 VF = c.VF,
    
                                 XF = c.XF,
    
                                 AU = c.AU,
    
                                 MS60 = c.MS60,
    
                                 MS63 = c.MS63,
    
                                 MS64 = c.MS64,
    
                                 MS64FB = c.MS64FB,
    
                                 MS65 = c.MS65,
    
                                 MS65FB = c.MS65FB
    
                             });
    
                try
    
                {
    
                    // Begin query execution, supplying a method to handle the response 
    
                    // and the original query object to maintain state in the callback.
    
                    query.BeginExecute(OnOrderQueryComplete, query);
    
                }
    
                catch (DataServiceQueryException ex)
    
                {
    
                    throw new ApplicationException("An error occurred during query execution.", ex);
    
                }
    
            }
    
            private void OnOrderQueryComplete(IAsyncResult result)
    
            {
    
                // Get the original query from the result.
    
                DataServiceQuery< USsingleResults > query = (DataServiceQuery< USsingleResults >)result.AsyncState;
    
                foreach (USsingleResults result in query.EndExecute(result))
    
                {
    
    //…
    
                }
    
            }

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, February 13, 2014 9:37 AM
    Moderator
  • Thanks for the reply.  I tried you example but I get an error on the code below.  The error is "Class name is not valid at this point.  

    Thanks Bryan

    query = (DataServiceQuery
    Thursday, February 13, 2014 1:52 PM
  • Hello,

    How do you define the USsingleResults class and the USsingle entity class? If it is possible, could you please upload your program to SkyDrive so that we can download it and test with it to see why it throws such an error?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Friday, February 14, 2014 5:58 AM
    Moderator