【问题标题】:Windows Phone Background Task - Using HttpWebRequest or WebClientWindows Phone 后台任务 - 使用 HttpWebRequest 或 WebClient
【发布时间】:2011-09-06 13:26:32
【问题描述】:

我需要使用 WCF 向服务器发送信息。我目前正在使用 WebClient 使用 json 数据调用 WCF。现在,使用后台任务,我用 json 调用相同的 WCF,但 UploadStringAsync 的回调函数永远不会被调用。我也尝试了 HttpWebRequest,但它也不起作用。

我可以在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();在收到响应之前中止执行后台任务,并且没有等待 HttpWebRequest 或 WebClient 响应。

有人有解决方法吗?

【问题讨论】:

  • 来自msdn.microsoft.com/en-us/library/hh202942(v=vs.92).aspx - “定期代理通常运行 25 秒”:会不会是您的通话时间比这更长?
  • 你能展示你从后台代理调用服务器的代码吗?
  • 编辑您的问题以添加更多信息。答案只是为了回答——不是讨论或澄清(但是,答案下的 cmets 可以用于此类)。

标签: webclient httprequest windows-phone-7


【解决方案1】:

正如 Paul 在他的评论中提到的,您的后台任务可能会在 25 秒不活动后终止。如果您的任务被强制终止 3 (?) 次,它将被取消计划,直到您的应用程序再次安排它(我相信如果它跟上,它们也可能被永久禁止,但我不是 100% 确定这一点)。

编辑

NotifyComplete(); 可以在异步回调中发生。处理完响应后,只需将其移至回调的末尾即可。

【讨论】:

  • 小伙子,你救了我的命!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-15
  • 1970-01-01
  • 2014-08-01
  • 2014-01-31
  • 1970-01-01
相关资源
最近更新 更多