Skip to main content

Answered Question How to return a value with async WCF service calls?RSS Feed

(0)

Seuche
Seuche

Member

Member

3 points

28 Posts

How to return a value with async WCF service calls?

Hello,

 

I'm currently trying to get a DataTable created with Silverlight Dataset (www.silverlightdataset.net) from a SQL statement.

I've written a method that allows me to call a WCF service which is executing the statement and getting the result in XML data. 

My silverlight app should convert the XML data to a Datatable and make it usable.

 

 
        public DataTable getData(string lvsSQL)
        {
            DataTable cvsDT = new DataTable();
            AsyncCallback tmpCallback = new AsyncCallback(delegate(IAsyncResult asyncResult)
            {
                string lvsXML = ((WCFService.IWCFService)asyncResult.AsyncState).EndReturnData(asyncResult);
                DataSet ds = new DataSet();

                ds.FromXml(lvsXML);

                cvsDT = ds.Tables.Count > 0 ? ds.Tables[0] : new DataTable();
                b_IsCompleted = true;
            });

            b_IsCompleted = false;

            IAsyncResult asResult = serviceClient.BeginReturnData(lvsSQL, tmpCallback, serviceClient);

            return cvsDT;
        }
 

 That was my first try but that didn't work. My cvsDT is empty because the procedure is asynchronous and he returns before the statement is executed. When I'm using AsyncWaitHandle.WaitOne() the debugger says the Thread died and nothing happens.

I don't know how to do this. All I want to do is to fill a combo box directly with a SQL statement.

Does anyone of you have a solution?

I'm not able to use LINQ2SQL or the Entitiy Framework because we need dynamic database / table usage without entities and untypized Datasets to get our data from.

Regards

Sergey.Lutay
Sergey.L...

Star

Star

8720 points

1,580 Posts

Re: How to return a value with async WCF service calls?

Hi,

You need to subscribe to serviceClient.ReturnDataCompleted(maybe name is different it is depend on method to call) event and you will get data at that handler.

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Blog

Twitter

Sincerely,
Sergey Lutay

Seuche
Seuche

Member

Member

3 points

28 Posts

Re: Re: How to return a value with async WCF service calls?

 Hi,

 yes, that's working. But I won't get a result value for my function after that. Just in the result parameter.

I'm using a Channel Factory. Is it possible to get Completed Events as Channel Factory user, too?

Regards

Sergey.Lutay
Sergey.L...

Star

Star

8720 points

1,580 Posts

Re: Re: How to return a value with async WCF service calls?

Seuche:
Is it possible to get Completed Events as Channel Factory user, too?

No, need to use calbacks. Did you see http://www.netfxharmonics.com/2008/11/Understanding-WCF-Services-in-Silverlight-2?

Seems like all right at code which you show. Look at that article there are a part about Channel Factory.

(If this has answered your question, please click on "mark as answer" on this post. Thank you!)

Blog

Twitter

Sincerely,
Sergey Lutay

Seuche
Seuche

Member

Member

3 points

28 Posts

Re: Re: How to return a value with async WCF service calls?

 Hi,

 

my problem is that I'm just getting a empty datatable because the return statement is being executed before  the EndGetData() is executed. I don't get anywhere with this problem. Everything works except that.


Regards

Seuche
Seuche

Member

Member

3 points

28 Posts

Re: Re: Re: How to return a value with async WCF service calls?

 Is there really no way to wrap a Begin / End Pattern into one function to return the result of the End method with just passing the parameter?

Seuche
Seuche

Member

Member

3 points

28 Posts

Answered Question

Re: How to return a value with async WCF service calls?

 Solved the problem by using a AsyncCallback parameter and wrappers for Begin / End.

acidburner
acidburner

Member

Member

156 points

78 Posts

Re: Re: How to return a value with async WCF service calls?

I've got a simular problem. Could you tell me how you implemented the wrapper begin/end?

Seuche
Seuche

Member

Member

3 points

28 Posts

Re: Re: Re: How to return a value with async WCF service calls?

 Hello,

 follow this link: CodeProject: Synchronous Web Service Calls with Silverlight: Dispelling the async-only myth. Free source code and programming help

  • Unanswered Question
  • Answered Question
  • Announcement