【问题标题】:Timeout exception with wp7 REST service call using WebClient使用 WebClient 的 wp7 REST 服务调用超时异常
【发布时间】:2012-01-16 20:01:10
【问题描述】:

编辑:我很乐意放弃这个问题的赏金 - 时间快到了 - 下面的所有 cmets 都是最新的,但仍然没有解决方案。

遇到一个奇怪的错误。我已将代码简化为最简单的形式,但以下代码仍然出错。

    public partial class MainPage : PhoneApplicationPage {
    private readonly WebClient webClient;

    public MainPage() {
        InitializeComponent();

        webClient = new WebClient();
        webClient.OpenReadCompleted += clientOpenRead_Completed;
    }

    private void LoadButton_Click(object sender, RoutedEventArgs e) {
        webClient.OpenReadAsync(new Uri(@"validURL"));
    }

    private void clientOpenRead_Completed(object sender, System.Net.OpenReadCompletedEventArgs e) {
        using (var sr = new StreamReader(e.Result)) {
            Result.Text = sr.ReadToEnd();
        }
    }  
}

sr.ReadToEnd();总是返回空字符串,当我从 clientOpenRead_Completed 检查“e.Result”时,它包含以下异常:

base    {"Timeouts are not supported on this stream."}  System.SystemException {System.InvalidOperationException}

其他重要验证:validURL 在浏览器请求时有效。此外,上面的代码在控制台应用程序中调用时可以正常工作,相同的 URL 和类似的代码在 Monodroid 中也可以正常工作。

最后,服务源非 WCF。

有什么想法吗?

谢谢。

编辑:在我检查 e.Result 时的堆栈跟踪:(来自一个稍微不同的项目,但有同样的问题)

>   AppTest.dll!AppTest.Data.AsyncServiceProvider.clientOpenRead_Completed(object sender, System.Net.OpenReadCompletedEventArgs e) Line 20  C#
System.Net.dll!System.Net.WebClient.OnOpenReadCompleted(System.Net.OpenReadCompletedEventArgs e) + 0x15 bytes   
System.Net.dll!System.Net.WebClient.OpenReadOperationCompleted(object arg) + 0xc bytes  
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo rtmi, object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object parameters, System.Globalization.CultureInfo culture, bool isBinderDefault, System.Reflection.Assembly caller, bool verifyAccess, ref System.Threading.StackCrawlMark stackMark)   
mscorlib.dll!System.Reflection.RuntimeMethodInfo.InternalInvoke(object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, object[] parameters, System.Globalization.CultureInfo culture, ref System.Threading.StackCrawlMark stackMark) + 0x168 bytes 
mscorlib.dll!System.Reflection.MethodBase.Invoke(object obj, object[] parameters) + 0xa bytes   
mscorlib.dll!System.Delegate.DynamicInvokeOne(object[] args) + 0x98 bytes   
mscorlib.dll!System.MulticastDelegate.DynamicInvokeImpl(object[] args) + 0x8 bytes  
mscorlib.dll!System.Delegate.DynamicInvoke(object[] args) + 0x2 bytes   
System.Windows.dll!System.Windows.Threading.DispatcherOperation.Invoke() + 0xc bytes    
System.Windows.dll!System.Windows.Threading.Dispatcher.Dispatch(System.Windows.Threading.DispatcherPriority priority) + 0x83 bytes  
System.Windows.dll!System.Windows.Threading.Dispatcher.OnInvoke(object context) + 0x8 bytes 
System.Windows.dll!System.Windows.Hosting.CallbackCookie.Invoke(object[] args) + 0x19 bytes 
System.Windows.dll!System.Windows.Hosting.DelegateWrapper.InternalInvoke(object[] args) + 0x2 bytes 
System.Windows.RuntimeHost.dll!System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(System.IntPtr pHandle, int nParamCount, System.Windows.Hosting.NativeMethods.ScriptParam[] pParams, ref System.Windows.Hosting.NativeMethods.ScriptParam pResult) + 0x5e bytes 
[External Code] 

【问题讨论】:

  • 异常的堆栈跟踪是什么?
  • 这是一种奇怪的行为。该应用程序不会崩溃,但是当单击该按钮时,什么也不显示。当我单步执行代码时,e.Result 为空,当我从 clientOpenRead_Completed 检查“e.Result”时,它在“e.Result.ReadTimeout”中包含上述异常。我似乎正在内部处理异常,但我仍然需要处理它,以便应用程序正常运行。
  • 您下载的是什么类型的数据?无线电流? xml?
  • 来自 Web 服务的 JSON 响应(来自响应的文本)。我可以在浏览器中输入相同的 url 并查看有效的 json 响应 - url 和服务正在工作。
  • 顺便说一句,当您使用 OpenReadAsync 时,e.Result.ReadTimeout 总是会出现该异常,因此您应该忘记这一点并寻找其他问题。编辑:如果 e.Result 为空,我会专注于 validUrl,你可能犯了一个简单的错误。

标签: c# web-services windows-phone-7 rest


【解决方案1】:

也许试试: webClient.DownloadStringAsync(yourUrl);

而不是

webClient.OpenReadAsync(yourUrl);

【讨论】:

  • 这对我有用。我遇到了与上述相同的问题,并且我使用的是 OpenReadAsync 方法,现在使用 DownloadStringAsync 方法可以正常工作。谢谢
【解决方案2】:

在调用OpenReadAsync()之前尝试将webClientAllowReadStreamBuffering设置为false

更新

根据您的评论,我认为您引用的 System.Net.dll 可能有错误(非 Windows 手机)版本,这可能是问题的原因。在 7.1(标准安装)上应该是

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71\System.Net.dll

如果您使用的是 7.0,则应该是

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone\System.Net.dll

【讨论】:

  • 我最初想这样做,但我似乎无法使用该属性。 System.Net 包含在内,因为 WebClient 已解决:错误 1“System.Net.WebClient”不包含“AllowReadStreamBuffering”的定义,并且没有接受“System.Net.WebClient”类型的第一个参数的扩展方法“AllowReadStreamBuffering”可能是找到(您是否缺少 using 指令或程序集引用?)
  • 当我在执行期间检查 webClient 时,它确实显示了 AllowReadStreamBuffering 属性(值为 true),但它无法在代码中解决它。
  • @IUnknown - WebClient 的这个属性只存在于 Silverlight 中。查看我的更新。
  • 这是我在项目中拥有的(应该是正确的):C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71 \System.Net.dll
  • @IUnknown - 有线。很难猜到为什么它没有这个属性。
【解决方案3】:

您是否尝试过使用不同的 ContentTypeAccept 标头对 HttpWebRequest 执行相同的操作?

HttpWebRequest httpWebRequest = HttpWebRequest.CreateHttp(@"validURL");
httpWebRequest.Method = "GET";
httpWebRequest.BeginGetResponse((asyncresult) =>
{
    try
    {
        WebResponse webResponse = httpWebRequest.EndGetResponse(asyncresult);
        using (Stream stream = webResponse.GetResponseStream())
        {
            StreamReader Reader = new StreamReader(stream);
            string response = Reader.ReadToEnd();
        }
    }
    catch (Exception ex)
    {
        exception(ex);
    }
}, httpWebRequest);

【讨论】:

  • 我还没有尝试过,但是当我检查 webClient 对象时,请求、响应标头和方法都设置正确。您是否正在尝试使用上述代码进行测试?谢谢
  • 一旦我遇到问题:请求返回错误,但似乎一切正常。解决方案是直接指定 Url 是绝对的。从有效的http:// 字符串中它不会拾取它。
【解决方案4】:

网址是否托管为 https ?如果是这样,您可以尝试从 WP7 中的浏览器点击 URL 吗?

这个请求的数据量是多少?

【讨论】:

  • 我正在测试的那个大小是 63K,但我也在一个不到 1K 的大小上进行了测试——同样的问题。在 WP7 浏览器中测试是个好主意,但不幸的是,当我尝试时它抱怨 .json 扩展名。我们将它用于所有 json 请求,我无法更改它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
  • 2012-05-09
  • 2019-11-06
  • 1970-01-01
  • 1970-01-01
  • 2018-01-05
相关资源
最近更新 更多