.NET 框架如何从消息契约创建 SOAP 消息?哪个序列化器类用于序列化消息协定?
最佳答案
在表面之下,SOAP 消息主要是使用实现System.ServiceModel.Dispatcher.IDispatchMessageFormatter
接口(interface)的类的SerializeReply
方法构造的。有两个内部格式化程序使用 XmlObjectSerializer
和 XmlSerializer
实现来序列化消息 header 和正文。
幸运的是,还有另一个提供所需功能的公共(public)类。 TypedMessageConverter
在内部创建调度消息格式化程序,其方式类似于为调度操作设置的格式化程序。它在 Create
静态方法重载中使用私有(private)的 GetOperationFormatter
方法来创建内部 System.ServiceModel.Description.XmlMessageConverter
类的实例。
创建TypedMessageConverter
实现实例后,可以将消息契约实例传递给ToMessage
方法。最后,调用 Message
实例上的 ToString
方法返回预期的 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();
关于c# - 消息协定转换为 SOAP 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5174353/