How .NET framework creates SOAP message from message contract? Which serializer class is used to serialize the message contract?
Deep below the cover, the SOAP message is mainly constructed using SerializeReply
method of class implementing System.ServiceModel.Dispatcher.IDispatchMessageFormatter
interface. There are two internal formatters using XmlObjectSerializer
and XmlSerializer
implementations to serialize message headers and body.
Luckily, there is another, public class that provides wanted functionality. The TypedMessageConverter
internally creates dispatch message formatter in similar fashion to formatter set for a dispatch operation. It uses private GetOperationFormatter
method in Create
static method overloads in order to create instance of internal System.ServiceModel.Description.XmlMessageConverter
class.
After creating the TypedMessageConverter
implementation instance, one can pass message contract instance into ToMessage
method. Finally, call to ToString
method on Message
instance returns expected SOAP message string.
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();
This will depend on your configuration. By default basicHttpBinding
and wsHttpBinding
use the DataContractSerializer class. As far as the SOAP envelopes are concerned I don't know what classes are used and I am not sure if they would be public (I might be wrong on this).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With