2011-12-15 82 views
1

我能夠異步消費服務象下面這樣:如何使用Web服務異步

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()函數(這是在模型類)。我能夠在GetResponseCallback函數中獲得響應,但是如何將該響應返回到ViewModel,然後返回到View(前端.xaml)。爲了獲得響應,我們可以觸發一個事件GetResponseCallback函數,然後捕獲ViewModel類並啓動ViewModel同一事件並捕獲它,但這是一種不正確的方式。

請幫我理解MVVM架構調用Web服務。

在此先感謝。

+1

這都無關MVVM。這是關於事件的基本理解和C#中的異步編程 – 2011-12-15 11:34:23

+2

每當有人輸入「throw ex」時,一隻小貓就會死亡。 – 2011-12-15 11:38:55

回答

2
  1. 將您的viewmodel的回調添加到BeginGetRequestStream對象狀態參數中。
  2. 從GetResponseCallback中的異步結果中獲取回調。將其轉換爲您的回撥類型,並在回覆中回覆它。

像這樣:

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) 
     { 

     } 
    } 
}