我可以异步使用服务,如下所示:
public void PostMethodResponse()
{
try
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(_url);
myRequest.Method = "POST";
myRequest.Headers["SOAPAction"] = _action;
myRequest.ContentType = "text/xml; charset=utf-8";
myRequest.Accept = "text/xml";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);
}
catch (Exception ex)
{
throw ex;
}
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
System.IO.Stream postStream = request.EndGetRequestStream(asynchronousResult);
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(_postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
catch (Exception ex)
{
throw ex;
}
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
_response = responseString;
}
catch (Exception ex)
{
_response = ex.Message;
}
}
我从ViewModel类调用PostMethodResponse()函数(在Model类中)。我能够在GetResponseCallback函数中获得响应但是如何将该响应返回到ViewModel然后返回到View(前端.xaml)。为了获得响应,我们可以触发一个事件GetResponseCallback函数,然后捕获它的ViewModel类和Fire相同的事件ViewModel和Catch it View,但这不是一个正确的方法。
请帮助我理解MVVM架构调用Web服务。
提前致谢。
答案 0 :(得分:2)
像这样:
class HttpRequest<T>
{
internal HttpRequest(HttpWebRequest webRequest, Action<T> callback)
{
WebRequest = webRequest;
Callback = callback;
}
internal HttpWebRequest WebRequest { get; private set; }
internal Action<T> Callback { get; private set; }
}
class Class1
{
private Uri _url;
private string _action;
private string _postData;
public void PostMethodResponse(Action<string> callback)
{
try
{
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(_url);
myRequest.Method = "POST";
myRequest.Headers["SOAPAction"] = _action;
myRequest.ContentType = "text/xml; charset=utf-8";
myRequest.Accept = "text/xml";
myRequest.BeginGetRequestStream(GetRequestStreamCallback, new HttpRequest<string>(myRequest, callback));
}
catch (Exception ex)
{
// log blah
}
}
private void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
try
{
HttpRequest<string> request = (HttpRequest<string>)asynchronousResult.AsyncState;
System.IO.Stream postStream = request.WebRequest.EndGetRequestStream(asynchronousResult);
byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(_postData);
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.WebRequest.BeginGetResponse(GetResponseCallback, request);
}
catch (Exception ex)
{
// nothing to see, move along
}
}
private void GetResponseCallback(IAsyncResult asynchronousResult)
{
try
{
HttpRequest<string> request = (HttpRequest<string>)asynchronousResult.AsyncState;
HttpWebResponse response = (HttpWebResponse)request.WebRequest.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
if (streamResponse != null)
{
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
request.Callback(responseString);
}
}
catch (Exception )
{
}
}
}