当我调用服务时,我收到以下错误
反序列化操作"IbankClientOperation"的请求消息正文时出错。OperationFormatter遇到无效的消息正文。应找到名称为"doClient_ws_IbankRequest"且命名空间为"的节点类型"Element"http://www.informatica.com/wsdl/"。找到名称为"string"且命名空间为"的节点类型"Element"http://schemas.microsoft.com/2003/10/Serialization/'
我使用以下代码呼叫服务
Message requestMsg = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IService1/IbankClientOperation", requestMessage);
Message responseMsg = null;
BasicHttpBinding binding = new BasicHttpBinding();
IChannelFactory<IRequestChannel> channelFactory = binding.BuildChannelFactory<IRequestChannel>();
channelFactory.Open();
EndpointAddress address = new EndpointAddress(this.Url);
IRequestChannel channel = channelFactory.CreateChannel(address);
channel.Open();
responseMsg = channel.Request(requestMsg);
假设你的requestMessage在你的另一篇文章中是相同的(事实似乎是这样,因为错误消息说它正在接收字符串),你使用了错误的message.CreateMessage重载。你使用的被定义为
Message.CreateMessage(MessageVersion version, string action, object body);
而你传递给它的"请求消息"就是整个消息信封。您正在使用的这个将尝试序列化主体(由于它是一个字符串,它将把它序列化为<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">...</string>
——它正好映射到您的错误消息。
您需要使用的是一个重载,因为您已经有了SOAP信封,它需要这个重载,例如下面的一个:
Message.CreateMessage(XmlReader enveloperReader, int maxSizeOfHeaders, MessageVersion version);
代码看起来像:
string requestMessageString = @"<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:inf="http://www.informatica.com/"
xmlns:wsdl="http://www.informatica.com/wsdl/">
<soapenv:Header>
<inf:Security>
<UsernameToken>
<Username>john</Username>
<Password>jhgfsdjgfj</Password>
</UsernameToken>
</inf:Security>
</soapenv:Header>
<soapenv:Body>
<wsdl:doClient_ws_IbankRequest>
<wsdl:doClient_ws_IbankRequestElement>
<!--Optional:-->
<wsdl:Client_No>00460590</wsdl:Client_No>
</wsdl:doClient_ws_IbankRequestElement>
</wsdl:doClient_ws_IbankRequest>
</soapenv:Body>
</soapenv:Envelope>";
XmlReader envelopeReader = XmlReader.Create(new StringReader(requestMessageString));
Message requestMsg = Message.CreateMessage(envelopeReader, int.MaxValue, MessageVersion.Soap11);