Back in classic ASP.NET I’d persist data extract from a web service in base class property as follows:
private string m_stringData;
public string _stringData
{ get {
if (m_stringData==null)
{
//fetch data from my web service
m_stringData = ws.FetchData()
}
return m_stringData;
}
}
This way I could always simple use _stringData and know that I’d always get the data I was after (sometimes I’d use Session state as a store instead of a private member variable).
In Silverlight with a WCF this doesn’t work, because the WCF service has to be called asynchronously.
So, how do I get round this?… There are a number of routes to take. One method involves wrapping the messages synchronously and can be found at codeplex
Another alternative that is less verbose, and arguably easier, to follow is using a lambda expression as follows:
private string m_stringData;
public string _stringData
{
get
{ //if we don't have a list of departments, fetch from WCF
if (m_stringData == null)
{
StringServiceClient client = new StringServiceClient();
client.GetStringCompleted +=
(sender, e) =>
{
m_stringData = e.Result;
};
client.GetStringAsync();
}
return m_stringData;
}
}
A final solution was posted on this stackoverflow thread
Leave a Reply