SOAP Serialization


As we mentioned earlier, SOAP is a standard based on XML for describing properties and methods . SOAP is used by Web services as well as .NET Remoting precisely for these abilities . In this sense, SOAP serialization is similar to remote procedure calls (RPCs) but is mainly concerned with standardizing message passing. SOAP provides a standard of describing these remote procedures and properties. The IFormatter interface described in the Binary Serialization section earlier in this chapter offers a way of generating SOAP messages by creating an instance of the SoapFormatter class.

You follow the same guidelines to use SoapFormatter as you do to use BinaryFormatter , as described in the Binary Serialization section. The only difference is that the IFormatter instance used is a SoapFormatter that is, the class being serialized as well as any inherited classes must have the Serializable attribute set. The assembly containing the serialized object definition must be available at both the source and the destination. The serialized stream can also be customized in the same way by adding the NonSerialized attribute or by implementing the ISerializable interface, as you saw earlier. The following code serializes an object with the SoapFormatter :

C#

 //UsingtheMyBasicDataclassdefinedearlier MyBasicDatabasicData=newMyBasicData(); IFormattersoapFormatter=newSoapFormatter(); FileStreamfileStream; fileStream=newFileStream(soapformatter.xml", FileMode.Create, FileAccess.Write, FileShare.None); soapFormatter.Serialize(fileStream,basicData); 

Visual Basic .NET

 DimbasicDataasMyBasicData=newMyBasicData() DimsoapFormatterasIFormatter=newSoapFormatter() DimfileDataStreamasFileStream fileDataStream=newFileStream(_  soapformatter.cml",_ FileMode.Create,_ FileAccess.Write,_ FileShare.None_) soapFormatter.Serialize(fileDataStream,basicData) 

As weve seen with the previous serialization types, there is always a way to customize the serialization output. For SOAP serialization using the SoapFormatter class, it is the same as for the binary formattereither the NonSerializable attribute can be applied to class properties or the class to be serialized can implement the ISerializable interface.

To see SoapFormatter in action, take a look at the binsoapserial.cs or binsoapserial.vb sample. SoapFormatter is illustrated in this sample because it is a type of IFormatter and the rules for controlling SOAP serialization are the same as for using the BinaryFormatter .




Network Programming for the Microsoft. NET Framework
Network Programming for the MicrosoftВ® .NET Framework (Pro-Developer)
ISBN: 073561959X
EAN: 2147483647
Year: 2003
Pages: 121

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net