1

I read articles about the benefits of sending serialized objects through network. Performance and size cost.

Some explained about DataContract and using JSON or XML serialization.

But I can't find articles about what happens if I don't use these attributes and I return a object assuming that my object's properties and states are of primitive types. Obviously when I use a serializer like DataContractJsonSerializer and its WriteObject method concretely, it's gonna throw an exception.

What would happen if I return for example in a web service method, an object of Employee type which has the attribute 'Serializable'. I mean what would .net would treat this result.

[WebMethod]
public Employee SendEmployeeData()
{
}

The object would be sent through network as binary stream or what?

Is it required to serialize an object to send it throught network?

I also read that DataContractSerializer would interpret the Serializable attribute.

difference between DataContract attribute and Serializable attribute in .net

Community
  • 1
  • 1
Maximus Decimus
  • 4,901
  • 22
  • 67
  • 95
  • 2
    A quick note: use Json.NET, rather than DataContractSerializer. The performance is indisputably better, and the attribute annotation is cleaner IMO. Even Microsoft are using Json.NET in WebAPI 2. – pixelbadger Feb 20 '15 at 14:49
  • Thanks for the approach. It's not the first time that I heard that and I'm starting to read and explore about it. – Maximus Decimus Feb 20 '15 at 14:58

2 Answers2

2

I mean what would .net would treat this result.

That would intend entirely on the framework / frameworks you are using. WCF will work differently to ASMX, which will work differently to MVC, which will ... blah blah blah.

The object would be send through network as binary stream or what?

Yes; TCP streams are streams

Is it required to serialize an object to send it throught network?

Yes; you can only send a series of bytes through a data; the process of converting an object to a series of bytes is called serialization.

I also read that DataContractSerializer would interpret the Serializable attribute.

Well... sort of... that isn't a great usage, though, and is akin to using BinaryFormatter (it might use NetDataContractSerializer, but this has most of the same problems) - basically: don't do that. If you are using an API that is intended to exchange data-contracts, use a data-contract. There is no "best" approach here; it all comes down to what you are trying to do and what your priorities are. JSON and XML have interoperability advantages over BinaryFormatter et al, but there are other options too with the same benefits.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • 1
    Thanks for your answer. According to your TCP streams phrase. Does .Net require that my objects have the Serializable attribute or not? – Maximus Decimus Feb 20 '15 at 15:02
  • 2
    @MaximusDecimus pretty much the only thing that cares about `[Serializable]` is `BinaryFormatter`, which you shouldn't be using anyway; the main thing that uses `BinaryFormatter` *without you knowing* is *remoting*, which you shouldn't be using anyway. So if you aren't using things that require `[Serializable]`, no you don't need to add `[Serializable]` – Marc Gravell Feb 20 '15 at 15:05
  • 1
    I'll add to @MarcGravell's comment, that I think it would be advisable to do a little more research on TCP streams (and other transport mechanisms), and ask another question specifically related to that topic. – pixelbadger Feb 20 '15 at 15:06
2

If I recall correctly, DataContractSerializer is pretty explicit about requiring attribute annotations on all properties and objects that need to be serialized. I might be wrong about this though: it's been a while since I've used it.

They will definitely not be automagically serialized using a BinaryFormatter. The Serializable attribute is just a marker to tell a BinaryFormatter to serialize an object.

Json.NET (which I've referenced in my comment) will use reflection to build a serialized structure of a given object - even dynamic ones. The following code will construct a dynamic object and convert it to a JSON string, using JSON.net:

//build request
var dynamicObject = new {
    message = "Hello World",
    number = 32,
    date = DateTime.Now
};

//convert to JSON string
string json = JsonConvert.SerializeObject(dynamicObject);
Console.WriteLine(json);
pixelbadger
  • 1,556
  • 9
  • 24
  • If you don't annotate *at all*, IIRC it falls back to behaviour much like `BinaryFormatter` - which is *not* to be recommended – Marc Gravell Feb 20 '15 at 14:58
  • @MarcGravell - Yes, thanks for clarifying this. As you've said in your answer, a well-defined contract is definitely the way to go. – pixelbadger Feb 20 '15 at 14:59