我阅读了有关通过网络发送序列化对象的好处的文章。性能和尺寸成本。
一些关于 DataContract 和使用 JSON 或 XML 序列化的解释。
但是我找不到关于如果我不使用这些属性会发生什么的文章,并且我假设我的对象的属性和状态是原始类型并返回一个对象。显然,当我具体使用像 DataContractJsonSerializer 这样的序列化程序及其 WriteObject 方法时,它会抛出异常。
例如,如果我在 Web 服务方法中返回一个具有属性“Serializable”的 Employee 类型的对象,会发生什么情况。我的意思是 .net 会如何处理这个结果。
[WebMethod]
public Employee SendEmployeeData()
{
}
对象将作为二进制流或什么通过网络发送?
是否需要序列化一个对象才能通过网络发送它?
我还读到 DataContractSerializer 会解释 Serializable 属性。
difference between DataContract attribute and Serializable attribute in .net
请您参考如下方法:
I mean what would .net would treat this result.
那将完全取决于您正在使用的框架。 WCF 与 ASMX 的工作方式不同,而 ASMX 的工作方式与 MVC 不同,后者将......等等等等。
The object would be send through network as binary stream or what?
是的; TCP流是流
Is it required to serialize an object to send it throught network?
是的;您只能通过数据发送一系列字节;将对象转换为一系列字节的过程称为序列化。
I also read that DataContractSerializer would interpret the Serializable attribute.
嗯...有点...这不是很好的用法,但类似于使用 BinaryFormatter(它可能使用 NetDataContractSerializer,但是这有大部分相同的问题)- 基本上:不要那样做。如果您使用的 API 旨在交换数据契约(Contract),请使用数据契约(Contract)。这里没有“最佳”方法。这一切都取决于你想做什么以及你的优先事项是什么。 JSON 和 XML 与 BinaryFormatter 等相比具有互操作性优势,但也有其他选项具有相同的优势。
