获取响应消息为字符串--WCF客户端的System.ServiceModel.CommunicationException

时间:2017-08-15 16:38:22

标签: c# .net xml wcf soap

我正在使用使用我用来调用SOAP Web服务的WSDL文档生成的WCF服务客户端。有时,我会遇到XML反序列化错误,这些错误通常用类似于下面的调用堆栈表示:

System.ServiceModel.CommunicationException: Error in deserializing body of reply message for operation '(any operation...)'. No characters can appear before the XML declaration. Line 1, position 2704. ---> System.Xml.XmlException: No characters can appear before the XML declaration. Line 1, position 2704.
   at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, String res, String arg1, String arg2, String arg3)
   at System.Xml.XmlUTF8TextReader.ReadDeclaration()
   at System.Xml.XmlUTF8TextReader.Read()
   at System.Xml.XmlBaseReader.ReadEndElement()
   at System.ServiceModel.Channels.Message.ReadFromBodyContentsToEnd(XmlDictionaryReader reader, EnvelopeVersion envelopeVersion)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeBodyContents(Message message, Object[] parameters, Boolean isRequest)
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   --- End of inner exception stack trace ---
   at System.ServiceModel.Dispatcher.OperationFormatter.DeserializeReply(Message message, Object[] parameters)
   at System.ServiceModel.Dispatcher.ProxyOperationRuntime.AfterReply(ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   at System.ServiceModel.Channels.ServiceChannel.EndCall(String action, Object[] outs, IAsyncResult result)
   at System.ServiceModel.Channels.ServiceChannelProxy.TaskCreator.<>c__DisplayClass5`1.<CreateGenericTask>b__4(IAsyncResult asyncResult)
   at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at (etc, etc...)

我的问题是:

  • 如何获取响应消息流,以便我可以将其作为XML字符串进行记录和调查,以发现语法错误和架构违规?

0 个答案:

没有答案