WaterBoy
WaterBoy

Reputation: 709

Error accessing wcf service asynchronously using BeginExecute in c# .net

I am building an standard odata client using: Microsoft.Data.Services.Client.Portable Windows 8 VS2013

I have added a service reference to the project (TMALiveData) with authorisation. Now I want to retrieve data. I am using the following code, but when I do, I get a null pointer reference in the final loop.

The code is:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Services.Client;

namespace testLSconWithMSv2
{
   public class testLSCon
    {
        static string mResult;

        public static string result { get { return mResult; } }

        public static void testREADLiveConnection()
        {

            Uri tmaLiveDataRoot = new Uri("https://xxx.azurewebsites.net/xxx.svc/");
            TMLiveData.TMALiveData mLiveData = new TMLiveData.TMALiveData(tmaLiveDataRoot);
            mResult = null;


            DataServiceQuery<TMLiveData.JobType> query = (DataServiceQuery<TMLiveData.JobType>)mLiveData.JobTypes.Where(c => c.IsActive == true);
            mResult = "Trying to READ the data";
            try
            {
                query.BeginExecute(OnQueryComplete, query);
            }
            catch (Exception ex)
            {
                mResult = "Error on beginExecute: " + ex.Message;
            }


        }

        private static  void OnQueryComplete(IAsyncResult result)
        {

            DataServiceQuery<TMLiveData.JobType> query = result as DataServiceQuery<TMLiveData.JobType>;
            mResult = "Done!";
            try
            {
                foreach (TMLiveData.JobType jobType in query.EndExecute(result))
                {
                    mResult += jobType.JobType1 + ",";
                }
            }catch (Exception ex)
            {
                mResult = "Error looping for items: " + ex.Message;
            }



        }

    }
}

Is there anything obvious that I'm doing wrong? I have based the approach on the ms example at: How to Execute Async...

Upvotes: 1

Views: 200

Answers (1)

Yuval Itzchakov
Yuval Itzchakov

Reputation: 149538

You're getting a NullReferenceException because you're trying to cast the IAsyncResult to a DataServiceQuery<TMLiveData.JobType>. You need to cast the IAsyncResult.AsyncState instead:

private static  void OnQueryComplete(IAsyncResult result)
{
        DataServiceQuery<TMLiveData.JobType> query = (DataServiceQuery<TMLiveData.JobType>) result.AsyncState;

        mResult = "Done!";
        try
        {
            foreach (TMLiveData.JobType jobType in query.EndExecute(result))
            {
                mResult += jobType.JobType1 + ",";
            }
        }
        catch (Exception ex)
        {
            mResult = "Error looping for items: " + ex.Message;
        }
}

On a side note, i used an explicit cast here instead of the as operator. If you did, you would get an InvalidCastException instead of a NullReferenceException, which would be make it a whole lot easier for you to discover the error.

Upvotes: 1

Related Questions