2011-09-06 81 views
1

我需要使用WCF將信息發送到服務器。我目前使用WebClient以json數據調用WCF。現在,在後臺任務中,我使用json調用相同的WCF,但UploadStringAsync的回調函數永遠不會被調用。我也嘗試HttpWebRequest,但它不工作。Windows Phone後臺任務 - 使用HttpWebRequest或WebClient

我可以在documentation中看到後臺任務支持HttpWebRequest。

下面是處理WCF請求/響應代碼:

public class Communication 
{ 
    #region Private Variables 

    /// <summary> 
    /// Callback method passed to MakeHttpPostRequest will be set to below variable. 
    /// This variable holds the reference to callback function and used to invoke the method passed by MakeHttpPostRequest calling method. 
    /// </summary> 
    private Action<string> action; 

    private Action<string, object> genericAction; 

    private object returnValue; 

    #endregion 

    #region Methods 

    /// <summary> 
    /// Calls WCF service using POST method. 
    /// </summary> 
    /// <param name="webserviceURL">URL of WCF service.</param> 
    /// <param name="json">JSON data to be posted to WCF service.</param> 
    /// <param name="response">Callback function that is invoked when response is received from WCF service.</param> 
    public void MakeHttpPostRequest(string webserviceURL, string json, Action<string> response) 
    { 
     try 
     { 
      this.action = response; 

      if (DeviceNetworkInformation.IsNetworkAvailable) 
      { 
       Uri uri = new Uri(webserviceURL); 
       byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(json); 

       string data = Encoding.UTF8.GetString(byteArray.ToArray(), 0, (int)byteArray.Length); 
       WebClient webClient = new WebClient(); 
       webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(this.WebClient_UploadStringCompleted); 
       webClient.Headers["Content-type"] = "application/json"; 
       webClient.Encoding = Encoding.UTF8; 
       webClient.UploadStringAsync(uri, "POST", data); 
      } 
      else 
      { 
       if (this.action != null) 
       { 
        this.action(string.Empty); 
       } 
      } 
     } 
     catch (Exception ex) 
     { 
      if (this.action != null) 
      { 
       this.action(string.Empty); 
      } 

      new ErrorException.ErrorException().HandleError(ex, string.Empty, Global.Modules.General); 
     } 
    } 

    #endregion 

    #region Events 

    /// <summary> 
    /// Callback function that gets called when response is received from web service. 
    /// </summary> 
    /// <param name="sender">The object that raises the event.</param> 
    /// <param name="e">Object containing Http response details.</param> 
    private void WebClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e) 
    { 
     try 
     { 
      // Check whether to invoke any method 
      if (this.action != null) 
      { 
       // Invoke the method passed to MakeHttpPostRequest by it's calling method 
       this.action(e.Result); 
      } 
      else if (this.genericAction != null) 
      { 
       // Invoke the method passed to MakeHttpPostRequest by it's calling method 
       this.genericAction(e.Result, this.returnValue); 
      } 
     } 
     catch (Exception ex) 
     { 
      if (this.action != null) 
      { 
       this.action(string.Empty); 
      } 

      new ErrorException.ErrorException().HandleError(ex, string.Empty, Global.Modules.General); 
     } 
    } 

    #endregion 
} 

而且使用下面的代碼,以JSON發送到服務器:

// Send location data to server 
new Common.Communication().MakeHttpPostRequest(Common.ServiceURL.TrackingTracingURL, postData, result); 

上面的代碼是從工作應用的罰款。但是,從後臺任務調用時不起作用。


HttpWebRequest或WebClient沒有任何問題。這是問題呼籲:

NotifyComplete(); 

上的HttpWebRequest或WebClient的調用是異步,調用NotifyComplete();正在中止執行後臺任務bafore收到響應並且未等待HttpWebRequest或WebClient響應。

有沒有人有解決這個問題?

+1

從http://msdn.microsoft.com/en-us/library/hh202942(v=vs.92).aspx - 「定期代理通常運行25秒」:是否可能是您的通話花費的時間超過那? –

+0

你可以顯示你的代碼從後臺代理調用服務器嗎? –

+0

請僅僅給你的問題添加額外的信息。答案就是這樣,答案 - 不是討論或澄清(但是,答案下的評論可以用於此類)。 –

回答

3

正如Paul在他的評論中提到的那樣,您的後臺任務很可能會在閒置25秒後終止。如果你的任務被強制終止了3(?)次,它將不會被計劃,直到你的應用程序再次安排它(我相信如果它保持不變,它們也可以被永久禁用,但我不能100%確定)。

編輯

NotifyComplete();可以發生在異步回調。在完成處理響應後,將其移至回調結束時。

+0

我已添加代碼。 –

+0

Ty傢伙,你救了我的一天! –

相關問題