Skip to main content
Microsoft Silverlight
Home Forums Silverlight Programming Programming with .NET - General How to return a value with async WCF service calls?
8 replies. Latest Post by Seuche on December 3, 2009.
(0)
Seuche
Member
3 points
28 Posts
11-19-2009 10:01 AM |
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.L...
Star
8720 points
1,580 Posts
11-19-2009 10:10 AM |
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.
11-19-2009 10:25 AM |
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?
11-19-2009 10:48 AM |
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.
11-19-2009 4:46 PM |
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.
11-20-2009 7:39 PM |
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?
11-23-2009 8:54 AM |
Solved the problem by using a AsyncCallback parameter and wrappers for Begin / End.
acidburner
156 points
78 Posts
11-27-2009 3:08 PM |
I've got a simular problem. Could you tell me how you implemented the wrapper begin/end?
12-03-2009 11:42 AM |
follow this link: CodeProject: Synchronous Web Service Calls with Silverlight: Dispelling the async-only myth. Free source code and programming help