Problem to pass the result to the caller for asynchronous httpWebRequest
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);
}
cat开发者_运维百科ch
{
}
}
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();
}
}
It looks like you're basically trying to get away from the asynchronous model. While you can do that by effectively blocking until the event handler for the asynchronous request has fired, you really shouldn't. There are good reasons for Silverlight to only support asynchronous web operations - you should go with that decision.
It's fine to have a helper class to effectively perform a transformation on the result, but I'd suggest doing that in an asynchronous style - pass in an event handler which will be called when the request completes (either successfully or unsuccessfully). Transform the result (e.g. reading it as a string) and then call the event handler.
It can be a bit of a pain, admittedly, but you really need to start thinking in terms of an asynchronous model.
You might also want to look at WebClient
which already has support for fetching an HTTP result as a string.
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.
public event Action<string> ResponseResult;
You can then invoke this event in your web response handler:
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();
if (ResponseResult != null)
ResponseResult(resultString);
}
}
And in your code the initiates the service call you can subscribe to this event to get notified when it has finished:
Helper helper = new Helper(url);
public MainPage()
{
InitializeComponent();
Helper.ResponseResult += ResponseHandler;
Helper.invoke(output);
}
public void ResponseHandler(string response)
{
// do something with response
}
精彩评论