我需要将从磁盘加载的SOAP消息解析为生成的代理的类型。WCF在接收到来自http服务器的消息时执行此操作,因此我应该能够从磁盘执行此操作。
我通过WCF使用WCF服务,我从远程WSDL生成了代理客户端。
下面是我从网络收到的XML结构(它是用System.ServiceModel.MessageLogging记录的),我希望将其解析为生成的类CRResponse:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="urn:PourtuIntf" xmlns:ns2="ns2:PourtuIntf-IPourtu">
<SOAP-ENV:Header/>
<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<ns2:GetCRResponse>
<return>
<ResultCode>0</ResultCode>
<CR>
<Theme SOAP-ENC:arrayType="ns1:ItemType[5]">
<item>
<Key/>
<Section SOAP-ENC:arrayType="ns1:Section[3]">
...
当我调用web服务的操作'GetCR‘时,消息被正确地转换为WCF生成的代理客户端类型GetCRResponse,但我不知道WCF是如何工作的,我需要解析磁盘中的文件。
我试着这样解析消息:
GetCRResponse body;
using (var xmlReader = XmlReader.Create("Requests\\CR.xml"))
{
Message m = Message.CreateMessage(xmlReader, int.MaxValue, MessageVersion.Soap11);
body = m.GetBody<GetCRResponse>();
}
在GeyBody方法中,会引发此异常:
Expected element 'ActGetCRResponse' from namespace 'http://schemas.datacontract.org/2004/07/Pourtu.PourtuClient'.. Detecting 'Element' with name 'ActGetCRResponse', namespace 'urn:PourtuIntf-IPourtu'
。
我尝试使用SoapFormatter:
using ( FileStream fs = new FileStream("Requests\\CR.xml", FileMode.Open) )
{
SoapFormatter formatter = new SoapFormatter();
body = (ActGetCRResponse)formatter.Deserialize(fs);
}
..the反序列化引发以下异常:分析错误,没有与XML键“”ns2 GetCRResponse“”关联的程序集。“”
我不能使用xml序列化程序将其反序列化为GetCRResponse,因为soap序列化程序需要解释SOAP-ENC:arrayType属性。
发布于 2017-04-29 05:48:33
更新:
Message m = Message.CreateMessage(XmlReader.Create("C:\\testSvc\\login.xml"), int.MaxValue, MessageVersion.Soap11);
SoapReflectionImporter importer = new SoapReflectionImporter(new SoapAttributeOverrides(), "urn:PlotiIntf-IPloti");
XmlTypeMapping mapp = importer.ImportTypeMapping(typeof(ActGetCRResponse));
XmlSerializer xmlSerializer = new XmlSerializer(mapp);
var o = (ActGetCRResponse)xmlSerializer.Deserialize(m.GetReaderAtBodyContents());
https://stackoverflow.com/questions/43682043
复制相似问题