Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message contract translation to SOAP message

How .NET framework creates SOAP message from message contract? Which serializer class is used to serialize the message contract?

like image 499
Rest Wing Avatar asked Mar 02 '11 22:03

Rest Wing


2 Answers

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();
like image 98
Rest Wing Avatar answered Nov 15 '22 08:11

Rest Wing


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

like image 43
Darin Dimitrov Avatar answered Nov 15 '22 07:11

Darin Dimitrov