简体   繁体   English

C#中的对象序列化和联网

[英]Object serialization and networking in C#

I'm working on a simple network project, and would like to transfer objects directly using a TCPListener / Client connection. 我正在研究一个简单的网络项目,并希望使用TCPListener /客户端连接直接传输对象。 I would like to avoid the WCF overhead, and just have a simple way of serializing the object on the way out, sending it over the network, and finally restoring it back to the original on the other end. 我想避免WCF开销,只有一种简单的方法可以在出局时序列化对象,通过网络发送它,最后将其恢复到另一端的原始对象。

Thanks 谢谢

Remoting is out of favor now that there is WCF. 现在有了WCF,远程处理已不再受欢迎。 WCF is highly optimized for performance and will win over remoting in most cases. WCF已针对性能进行了高度优化,在大多数情况下将赢得远程控制。 See http://msdn.microsoft.com/en-us/library/bb310550.aspx . 请参阅http://msdn.microsoft.com/en-us/library/bb310550.aspx You don't mention whether you are worried about runtime overhead or the overhead of learning how to use WCF. 您无需担心您担心运行时开销还是学习如何使用WCF的开销。 That being said, you can reduce the runtime overhead by using the binary TCP transport instead of the HTTP one. 话虽如此,您可以通过使用二进制TCP传输而不是HTTP传输来减少运行时开销。 It works well, though HTTP (SOAP) is, of course, highly popular now. 尽管HTTP(SOAP)现在很受欢迎,但是它工作得很好。 Your service can support multiple transports (ie, TCP and HTTP) to work well with .NET clients (TCP transport) and other standards-compliant clients (HTTP SOAP transport). 您的服务可以支持多种传输方式(即TCP和HTTP),以便与.NET客户端(TCP传输)和其他符合标准的客户端(HTTP SOAP传输)一起正常工作。

Look into .NET Remoting; 研究.NET Remoting; it makes things easy! 这使事情变得容易!
And it's a big topic to display a sample in a comment here =) 在此处的评论中显示示例是一个大话题=)
Read here: http://msdn.microsoft.com/en-us/library/kwdt6w2k(VS.71).aspx 在这里阅读: http : //msdn.microsoft.com/en-us/library/kwdt6w2k(VS.71).aspx

Well if you need ONLY serialization/deserialization behavior without involving WCF or Remoting, there is a plenty ways: 好吧,如果您只需要序列化/反序列化行为而不涉及WCF或Remoting,则有很多方法:

  1. Standard serialization via SerializableAttribute and BinaryFormatter 通过SerializableAttributeBinaryFormatter进行标准序列
  2. XML Serialization XML序列化
  3. Json.NET Json.NET
  4. Google's Protocol Buffers via ProtoBuf.NET 通过ProtoBuf.NET的 Google协议缓冲区

All approaches have strong and weak sides, but I believe that for your needs #1 will be enough. 所有方法都有优势和劣势,但我相信满足您的需求#1就足够了。 Reasonable compact and without external libraries need. 合理紧凑,无需外部库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM