Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何在WCF中拦截序列化期间发生的异常?_.net_Wcf_Serialization_Error Handling - Fatal编程技术网

.net 如何在WCF中拦截序列化期间发生的异常?

.net 如何在WCF中拦截序列化期间发生的异常?,.net,wcf,serialization,error-handling,.net,Wcf,Serialization,Error Handling,我有一个合法的数据对象,具有所有数据契约/数据成员属性。 由于某些原因,WCF服务在操作完成并将结果作为返回值传递后崩溃。 我相信这与WCF无法正确序列化该结果有关。测试客户端没有说任何特定的内容: The underlying connection was closed: The connection was closed unexpectedly. Server stack trace: at System.ServiceModel.Channels.HttpChannelUtil

我有一个合法的数据对象,具有所有数据契约/数据成员属性。 由于某些原因,WCF服务在操作完成并将结果作为返回值传递后崩溃。 我相信这与WCF无法正确序列化该结果有关。测试客户端没有说任何特定的内容:

The underlying connection was closed: The connection was closed unexpectedly.

Server stack trace: 
   at System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
   at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   at IFacade.PickSecurities(String pattern, Int32 atMost)
   at FacadeClient.PickSecurities(String pattern, Int32 atMost)

Inner Exception:
The underlying connection was closed: The connection was closed unexpectedly.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
我使用定制的服务主机工厂来控制创建服务实例

我知道我可以设置跟踪侦听器并检查日志,但这很麻烦。因此,我宁愿在发生时在服务器上显式地处理它

因此,我如何以编程方式截获该异常并返回适当的错误消息

更新

对于主张使用system.diagnostics跟踪日志进行故障排除的人。通过查看以下日志条目,您能告诉我问题是什么吗

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>131075</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2012-11-26T16:20:41.8343508Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{2d6e8a90-a784-4add-a9b6-9fd9c37b7b0f}" />
<Execution ProcessName="WebDev.WebServer40" ProcessID="5104" ThreadID="10" />
<Channel />
<Computer>WASWK060</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier>
<Description>Throwing an exception.</Description>
<AppDomain>4eaf36d4-1-129984204415953508</AppDomain>
<Exception>
<ExceptionType>System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>There is a problem with the XML that was received from the network. See inner exception for more details.</Message>
<StackTrace>
at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>System.ServiceModel.ProtocolException: There is a problem with the XML that was received from the network. See inner exception for more details. ---&gt; System.Xml.XmlException: The body of the message cannot be read because it is empty.
   --- End of inner exception stack trace ---</ExceptionString>
<InnerException>
<ExceptionType>System.Xml.XmlException, System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>The body of the message cannot be read because it is empty.</Message>
<StackTrace>
at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>System.Xml.XmlException: The body of the message cannot be read because it is empty.</ExceptionString>
</InnerException>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>

131075
3.
0
2.
WASWK060
http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx
正在引发异常。
4eaf36d4-1-129984204415953508
System.ServiceModel.ProtocolException,System.ServiceModel,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089
从网络接收的XML有问题。有关详细信息,请参阅内部异常。
在System.ServiceModel.Channels.HttpRequestContext.CreateMessage()中
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,操作回调)
位于System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult结果)
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandlerRequest()中
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()中
位于System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(对象状态)
位于System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback回调,对象状态)
位于System.ServiceModel.Activation.HostedHttPrequestStatAsyncResult.OnBeginRequestWithFlow(对象状态)
在System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32错误,UInt32字节读取,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
System.ServiceModel.ProtocolException:从网络接收的XML存在问题。有关详细信息,请参阅内部异常。--System.Xml.XmlException:无法读取消息体,因为它是空的。
---内部异常堆栈跟踪的结束---
System.Xml.XmlException,System.Xml,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089
无法读取邮件正文,因为它是空的。
在System.ServiceModel.Channels.HttpRequestContext.CreateMessage()中
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,操作回调)
位于System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult结果)
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandlerRequest()中
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()中
位于System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(对象状态)
位于System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback回调,对象状态)
位于System.ServiceModel.Activation.HostedHttPrequestStatAsyncResult.OnBeginRequestWithFlow(对象状态)
在System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32错误,UInt32字节读取,NativeOverlapped*NativeOverlapped)
在System.Threading.\u IOCompletionCallback.PerformiCompletionCallback(UInt32错误代码,UInt32个字节,本机重叠*pOVERLAP)
System.Xml.XmlException:无法读取消息体,因为它是空的。
另一个

<E2ETraceEvent xmlns="http://schemas.microsoft.com/2004/06/E2ETraceEvent">
<System xmlns="http://schemas.microsoft.com/2004/06/windows/eventlog/system">
<EventID>131075</EventID>
<Type>3</Type>
<SubType Name="Error">0</SubType>
<Level>2</Level>
<TimeCreated SystemTime="2012-11-26T16:20:41.8383508Z" />
<Source Name="System.ServiceModel" />
<Correlation ActivityID="{2d6e8a90-a784-4add-a9b6-9fd9c37b7b0f}" />
<Execution ProcessName="WebDev.WebServer40" ProcessID="5104" ThreadID="10" />
<Channel />
<Computer>WASWK060</Computer>
</System>
<ApplicationData>
<TraceData>
<DataItem>
<TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord" Severity="Error">
<TraceIdentifier>http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx</TraceIdentifier>
<Description>Throwing an exception.</Description>
<AppDomain>4eaf36d4-1-129984204415953508</AppDomain>
<Exception>
<ExceptionType>System.ServiceModel.ProtocolException, System.ServiceModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType>
<Message>Content Type application/soap+xml; charset=utf-8 was sent to a service expecting text/xml; charset=utf-8.  The client and service bindings may be mismatched.</Message>
<StackTrace>
at System.ServiceModel.Channels.HttpInput.ThrowHttpProtocolException(String message, HttpStatusCode statusCode, String statusDescription)
at System.ServiceModel.Channels.HttpInput.ValidateContentType()
at System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(Exception&amp; requestException)
at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()
at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, Action callback)
at System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult result)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandleRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(Object state)
at System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback callback, Object state)
at System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequestWithFlow(Object state)
at System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
at System.Runtime.Fx.IOCompletionThunk.UnhandledExceptionFrame(UInt32 error, UInt32 bytesRead, NativeOverlapped* nativeOverlapped)
at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
</StackTrace>
<ExceptionString>System.ServiceModel.ProtocolException: Content Type application/soap+xml; charset=utf-8 was sent to a service expecting text/xml; charset=utf-8.  The client and service bindings may be mismatched.</ExceptionString>
<DataItems>
<Data>
<Key>System.ServiceModel.Channels.HttpInput.HttpStatusCode</Key>
<Value>UnsupportedMediaType</Value>
</Data>
<Data>
<Key>System.ServiceModel.Channels.HttpInput.HttpStatusDescription</Key>
<Value>Cannot process the message because the content type 'application/soap+xml; charset=utf-8' was not the expected type 'text/xml; charset=utf-8'.</Value>
</Data>
</DataItems>
</Exception>
</TraceRecord>
</DataItem>
</TraceData>
</ApplicationData>
</E2ETraceEvent>

131075
3.
0
2.
WASWK060
http://msdn.microsoft.com/en-US/library/System.ServiceModel.Diagnostics.ThrowingException.aspx
正在引发异常。
4eaf36d4-1-129984204415953508
System.ServiceModel.ProtocolException,System.ServiceModel,版本=4.0.0.0,区域性=中性,PublicKeyToken=b77a5c561934e089
内容类型应用程序/soap+xml;charset=utf-8已发送到需要text/xml的服务;字符集=utf-8。客户端和服务绑定可能不匹配。
位于System.ServiceModel.Channels.HttpInput.ThrowHttpProtocolException(字符串消息、HttpStatusCode状态代码、字符串状态描述)
位于System.ServiceModel.Channels.HttpInput.ValidateContentType()处
位于System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(异常和请求异常)
在System.ServiceModel.Channels.HttpRequestContext.CreateMessage()中
位于System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext上下文,操作回调)
位于System.ServiceModel.Activation.HostedHttpTransportManager.HttpContextReceived(HostedHttpRequestAsyncResult结果)
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.HandlerRequest()中
在System.ServiceModel.Activation.HostedHttpRequestAsyncResult.BeginRequest()中
位于System.ServiceModel.Activation.HostedHttpRequestAsyncResult.OnBeginRequest(对象状态)
位于System.ServiceModel.AspNetPartialTrustHelpers.PartialTrustInvoke(ContextCallback回调,对象状态)
位于System.ServiceModel.Activation.HostedHttPrequestStatAsyncResult.OnBeginRequestWithFlow(对象状态)
在System.Runtime.IOThreadScheduler.ScheduledOverlapped.IOCallback(UInt32错误代码,UInt32个字节,NativeOverlapped*NativeOverlapped)
在System.Runtime.Fx.IOCompletionThunk.Unhan
<behaviors>
  <serviceBehaviors>
    <behavior name="SomeServiceServiceBehavior">      
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
    </behavior>
  </serviceBehaviors>
</behaviors>