【问题标题】:how to send a already constructed SOAP message in WCF如何在 WCF 中发送已构建的 SOAP 消息
【发布时间】:2011-06-07 12:14:35
【问题描述】:

我在客户端的字符串中有一条 SOAP 消息

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>"

我正在发送这样的消息

Message requestMsg = Message.CreateMessage(MessageVersion.Soap11, "http://tempuri.org/IService1/IbankClientOperation", requestMessageString );

            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);

但问题是通过线路发送的实际消息在 SOAP 消息中包含 SOAP 消息... 我想以某种方式将我的 RAW 消息转换为 SOAP 结构

【问题讨论】:

    标签: wcf serialization soap deserialization


    【解决方案1】:

    您不能使用Soap11 作为消息版本,也不能使用BasicHttpBinding。试试:

    Message requestMsg = Message.CreateMessage(MessageVersion.None, "http://tempuri.org/IService1/IbankClientOperation", requestMessageString );
    
    CustomBinding binding = new CustomBinding(new HttpTransportBindingElement());
    IChannelFactory<IRequestChannel> channelFactory = binding.BuildChannelFactory<IRequestChannel>();
    channelFactory.Open();
    

    但无论如何,如果您有 SOAP 请求,为什么不简单地使用 WebClientHttpWebRequest 将请求发布到服务器?

    【讨论】:

    • 谢谢 :) 我找到了如下答案
    【解决方案2】:

    我从这个问题得到了答案 wcf soap message deserialization error

    【讨论】:

      【解决方案3】:

      您可以将 SOAP 消息转换(反序列化)为您的服务期望的对象。这是对我有用的草图:

      var invoice = Deserialize<Invoice>(text);
      var result = service.SubmitInvoice(invoice);
      

      反序列化在哪里:

      private T Deserialize<T>(string text)
      {
        T obj;
        var serializer = new DataContractSerializer(typeof(T));
        using (var ms = new MemoryStream(Encoding.Default.GetBytes(text)))
        {
            obj = (T)serializer.ReadObject(ms);
        }  
        return obj;
      }
      

      由于 SOAP 是 XML,您可以在反序列化之前轻松调整其结构(例如删除或更改命名空间)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多