views:

81

answers:

4

I am reading a book that is talking about serializing and deserializing files or sending data via web services. My question is.. Is it mandatory to use serialization when using web services. Also when saving files locally using serialization, what is the significants of doing so ?

I know that it saves the data into binary data. Is this to compress the data ??

I Appreciate the responses. thanks!

A: 

Binary data is more compact than SOAP serialization, but SOAP serialization is optimized for web-use, not local storage. With regards to web services, most web services will expect their data to arrive in a very specific format, but usually, .NET and Visual Studio can take care of that for you. If you're manually trying to create SOAP for a webservice, you're probably doing something wrong. You should be able to add a reference to a web service and have Visual Studio generate a nice object model you can use that abstracts away all the SOAP and serialization issues.

GWLlosa
A: 

Is it mandatory to use serialization when using web services.

Yes, it is. The network transports bytes, not objects, so your objects have to be converted to bytes (whether it is in raw binary form, XML, SOAP, JSON or anything else). The same goes for saving to a file on disk : a disk can contain bytes, not objects...

Thomas Levesque
A: 

You've got a few questions here.

First, is it mandatory to use serialization when using web services: I'd say yes. Serializing something usually (always?) refers to taking some object from memory and translating it to another format; for example, from memory to a file on a disk drive. With this (crappy) definition, a web service will, at the very least internally, be serializing some objects based on the request (to determine what method is be called, etc). If your web site has any parameters or return values those also must be serialized.

Second, serializing to a binary format is usually for size and speed. It's generally much faster to serialize an object (in .Net) to a binary object and also much smaller than an xml representation.

akmad
A: 

Yes; to send data out-of-process (especially over a network) you need to serialize it. Mostly this is implicit in .NET - you just tell it how. With "asmx" it'll use XmlSerializer, so not much needed (except a public .ctor()). With WCF, it is best to use explicit [DataContract] etc. Note that in both cases it uses xml-based variants; the description of "binary" is simple: everything is binary (or worse: electrons, or fields, or something) in computers. The text representation is just in our imagination ;-p

If you use "remoting", then it uses BinaryFormatter by default, which is (as it suggests) a true binary formatter, but it has a few niggles of its own.

I would draw a big distinction between using data for transport (comms etc), and for storage. In the first case, it is short-lived, and you can abuse a few more SOA techiques if you absolutely must (although I try not to) - for example, BinaryFormatter and NetDataContractSerializer include type metadata, which is loosely acceptable, but can be a pain if you store it anything other than short-term. Once stored, you need to be very careful that you can recreate it, and metadata/field-based storage is very brittle.

For storage, I stick with contract-based data:

  • xml (via XmlSerializer, or DataContractSerializer)
  • json
  • binary (perhaps protobuf-net)

(and in the bottom case, binary means: not text-based at all)

Marc Gravell