开发者

how to send a already constructed SOAP message in WCF

I have a SOAP message in a string at my client side

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> 
          </ws开发者_JAVA技巧dl:doClient_ws_IbankRequestElement> 
       </wsdl:doClient_ws_IbankRequest> 
    </soapenv:Body> 
</soapenv:Envelope>"

and i am sending the message like this

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

but the problem is that the actual message which is sent over wire has a SOAP message inside a SOAP message... i somehow want to convert my RAW message into SOAP structure


You can't use Soap11 as message version and you cannot use BasicHttpBinding. Try:

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

But anyway if you have SOAP request why don't you simply use WebClient or HttpWebRequest to post the request to the server?


I got the answer from this question wcf soap message deserialization error


You can convert (deserialize) your SOAP message into an object that your service expects. Here's a sketch of what works for me:

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

where Deserialize is this:

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

Since SOAP is XML, you can easily adjust it structure (remove or change namespace, for example) before deserializing.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜