消息合同转换为SOAP消息

Res*_*ing 6 .net c# wcf serialization soap

.NET框架如何从消息契约中创建SOAP消息?哪个序列化程序类用于序列化邮件合同?

Res*_*ing 11

在封面下方,SOAP消息主要是使用SerializeReply类实现System.ServiceModel.Dispatcher.IDispatchMessageFormatter接口的方法构造的.有两个内部格式化程序使用XmlObjectSerializerXmlSerializer实现序列化消息标题和正文.

幸运的是,还有另一个提供所需功能的公共课程.在TypedMessageConverter以类似的方式在内部创建调度消息格式来格式化设置调度操作.它GetOperationFormatterCreate静态方法重载中使用私有方法,以便创建内部System.ServiceModel.Description.XmlMessageConverter类的实例.

创建TypedMessageConverter实现实例后,可以将消息契约实例传递给ToMessage方法.最后,ToStringMessage实例上的方法调用返回预期的SOAP消息字符串.

TypedMessageConverter converter = TypedMessageConverter.Create(
    typeof( CustomMessage ),
    "http://schemas.cyclone.com/2011/03/services/Service/GetData",
    "http://schemas.cyclone.com/2011/03/data",
    new DataContractFormatAttribute() { Style = OperationFormatStyle.Rpc } );
CustomMessage body = new CustomMessage()
{
    // Setting of properties omitted
};
Message message = converter.ToMessage( body, MessageVersion.Soap12 );
string soapMessage = message.ToString();
Run Code Online (Sandbox Code Playgroud)