wcf soap message反序列化错误

时间:2011-06-07 09:53:50

标签: wcf serialization soap deserialization

我在进行服务电话时遇到以下错误

反序列化操作'IbankClientOperation'的请求消息正文时出错。 OperationFormatter遇到无效的Message正文。预计会找到名为“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);

1 个答案:

答案 0 :(得分:0)

假设您的requestMessage在your other post中是相同的(似乎是这种情况,因为错误消息显示它正在接收字符串),您使用的是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);