In Silverlight I want to be able to get the output from a Helper class like this:
public MainPage()
{
InitializeComponent();
String ouput;
Helper helper = new Helper(url);
Helper.invoke(output);
}
I can’t see how to do that since in Helper Class I am obliged to do an asynchronous call:
private String webserviceUrl;
private XDocument xdoc = new XDocument();
public Helper(String webserviceUrl)
{
this.webserviceUrl = webserviceUrl;
}
public void invoke(ref String output)
{
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(this.webserviceUrl);
try
{
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.BeginGetResponse(new AsyncCallback(HandlerWebResponse), httpWebResponse);
}
catch
{
}
}
private void HandlerWebResponse(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
using (StreamReader streamReader1 = new StreamReader(response.GetResponseStream()))
{
string resultString = streamReader1.ReadToEnd();
}
}
Create an event to notify that the service has been successfully consumed. In the event parameters you can pass the result of the invoked web services.
You can then invoke this event in your web response handler:
And in your code the initiates the service call you can subscribe to this event to get notified when it has finished: