Problem to pass the result to the caller for asynchronous httpWebRequest

大兔子大兔子 提交于 2020-02-05 02:32:07

问题


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();

        }
    }

回答1:


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
}



回答2:


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.



来源:https://stackoverflow.com/questions/5441217/problem-to-pass-the-result-to-the-caller-for-asynchronous-httpwebrequest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!