【发布时间】: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