如何在WCF中发送已构造的SOAP消息
我在客户端有一个字符串形式的SOAP消息如何在WCF中发送已构造的SOAP消息,wcf,serialization,soap,deserialization,Wcf,Serialization,Soap,Deserialization,我在客户端有一个字符串形式的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>
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消息。。。
我想将我的原始消息转换为SOAP结构您不能使用Soap11作为消息版本,也不能使用BasicHttpBinding。尝试:
但是无论如何,如果您有SOAP请求,为什么不直接使用WebClient或HttpWebRequest将请求发布到服务器?我从这个问题中得到了答案
您可以将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,您可以轻松地调整它的结构,例如在反序列化之前删除或更改名称空间。谢谢:我找到了如下答案
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;
}