WCF异步调用引发异常,该异常转到未处理异常处理程序-调用堆栈中没有我的代码

本文关键字:异常 调用 堆栈 代码 我的 异常处理程序 异步 WCF 未处理 | 更新日期: 2023-09-27 18:14:20

我正在对ASP进行WCF调用。. NET从Windows Phone (7.0, 7.1 SDK)。在某些情况下(我无法在模拟器或我的任何设备上实现这一点),未处理的异常处理程序会为其中一个调用中发生的异常触发(澄清-这都是在客户端上-而不是在服务器上)。我看到了两种情况(它们经常发生)。我以前从未遇到过这个问题(不是在旧版本上,也不是在使用相同WCF代理代码的其他应用程序上)。

有人知道为什么会发生这种情况吗?我能做些什么来解决这个问题?

第一个调用栈是:

System.ServiceModel.ProtocolException: The content type text/html of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 75 bytes of the response were: 'The page cannot be displayed because an internal server error has occurred.'. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.    at    
System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)    at    
System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)    at    
System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)    at    
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)    at    
System.Delegate.DynamicInvokeOne(Object[] args)    at    
System.MulticastDelegate.DynamicInvokeImpl(Object[] args)    at    
System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)    at    
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)    at    
System.Delegate.DynamicInvokeOne(Object[] args)    at    
System.MulticastDelegate.DynamicInvokeImpl(Object[] args)    at    
System.Windows.Threading.DispatcherOperation.Invoke()    at    
System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)    at    
System.Windows.Threading.Dispatcher.OnInvoke(Object context)    at    
System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)    at    
System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)     at    
System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)    at    
System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(IAsyncResult result)    at    
System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)    at    
System.Threading.ThreadPool.WorkItem.doWork(Object o)    at    
System.Threading.Timer.ring()     at    
System.ServiceModel.Channels.HttpChannelUtilities.ValidateRequestReplyResponse(HttpWebRequest request, HttpWebResponse response, HttpChannelFactory factory, WebException responseException)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.ProcessResponse(HttpWebResponse response, WebException responseException)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(IAsyncResult result)    at    
System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)       

发生这种情况的第二个回调是:

System.ServiceModel.EndpointNotFoundException: There was no endpoint listening at    
http://MyServer.com/Fail/FailService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound.    at    
System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)    at    
System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)    at    
System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)    at    
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)    at    
System.Delegate.DynamicInvokeOne(Object[] args)    at    
System.MulticastDelegate.DynamicInvokeImpl(Object[] args)    at    
System.Windows.Threading.Dispatcher.<>c__DisplayClass4.<FastInvoke>b__3()    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethodInfo rtmi, Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark)    at    
System.Reflection.RuntimeMethodInfo.InternalInvoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, StackCrawlMark& stackMark)    at    
System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters)    at    
System.Delegate.DynamicInvokeOne(Object[] args)    at    
System.MulticastDelegate.DynamicInvokeImpl(Object[] args)    at    
System.Windows.Threading.DispatcherOperation.Invoke()    at    
System.Windows.Threading.Dispatcher.Dispatch(DispatcherPriority priority)    at    
System.Windows.Threading.Dispatcher.OnInvoke(Object context)    at    
System.Windows.Hosting.CallbackCookie.Invoke(Object[] args)    at    
System.Windows.RuntimeHost.ManagedHost.InvokeDelegate(IntPtr pHandle, Int32 nParamCount, ScriptParam[] pParams, ScriptParam& pResult)     at    
System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)    at    
System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(IAsyncResult result)    at    
System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)    at    
System.Threading.ThreadPool.WorkItem.doWork(Object o)    at    
System.Threading.Timer.ring()     at    
System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)    at    
System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.OnGetResponse(IAsyncResult result)    at    
System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClassa.<InvokeGetResponseCallback>b__8(Object state2)    at    
System.Threading.ThreadPool.WorkItem.doWork(Object o)    at    
System.Threading.Timer.ring()    

WCF异步调用引发异常,该异常转到未处理异常处理程序-调用堆栈中没有我的代码

看起来你的web服务器/服务可能由于可能未处理的异常而崩溃。我会检查web服务器上的事件日志,看看是否有IIS报告崩溃的情况。

更新澄清问题

根据我现在对问题的理解,问题是,如果web服务关闭,WCF服务将引发一个异常,该异常只能在未处理的异常处理程序中捕获。

我从来没有遇到过这种行为,但我理解为什么会发生这种情况,并有一个可能的解决方案。WCF服务请求是异步触发的,因此实际的连接和处理发生在线程中,您可以在调用堆栈中看到。

如果你钩住了异步方法的完成事件,异常应该通过参数传递给事件处理程序,而不是被抛出。我认为只有在没有处理程序的情况下才会抛出异常,因为框架不知道该如何处理异常。