c# - 何时更改 Generate Serialization Assembly 值?

标签 c# .net performance wcf

我有一个连接到 WCF 本地网络服务器的客户端 winform 应用程序。 客户端存在性能问题,我搜索了解决方案并找到了 this发布。

上面写着:

This sounds like the serialization assemblies being created at runtime. Try changing the settings of the Serialization Assembly dropdown at the bottom of the Build pane of the properties window for the project.

我的问题是何时更改Generate Serialization Assembly 值以及我应该将其更改为什么值以提高客户端应用程序的性能?

我的代码在 C# 中,框架 4,在 VS2010Pro 中构建。

最佳答案

为了序列化类/结构,需要生成序列化程序集。这可能发生在编译时或运行时。 Sgen.exe用于在编译时生成序列化程序集;正如您所发现的,Visual Studio 可以选择性地自动执行此过程。

  • Off:调试配置的默认值(感谢 @Alexandru Lache)。不要在编译时生成序列化程序集。根据MSDN,每次应用程序运行时都会生成序列化程序集。 :

    When the XML Serializer Generator is not used, a XmlSerializer generates serialization code and a serialization assembly for each type every time an application is run. To improve the performance of XML serialization startup, use the Sgen.exe tool to generate those assemblies the assemblies in advance. These assemblies can then be deployed with the application.

  • 开启:使用Sgen.exe在编译时生成序列化程序集。这可以节省启动时间,但会增加部署规模。
  • Auto:发布配置的默认值。根据 MSDN,只有在您的代码中使用 XmlSerializer 时,才正式生成程序集(谢谢,@L-Three)。在我的测试中,这并不总是有效,因此如果您使用 XmlSerializer,我建议将其显式设置为 On

因此,我的回答是:如果您担心启动时间,并且您甚至使用了一次 Serializable 属性,请将该选项设置为 On。如果您更关心部署大小,请将其更改为关闭。我再也不会把它留在 Auto 上了,因为我不信任它。就像我说的,它似乎与 Off 一样,但我不会指望它。

编辑:我在区分“关闭”和“自动”时肯定遇到了一些麻烦。差异在任何地方都没有明确定义。如果您完全使用 Serializable 属性,我会坚持使用 On,如果不使用,我会坚持使用 Off。我不会考虑部署大小或启动时间。如果我坚持这条规则,我似乎遇到的与序列化相关的错误会更少。

更新:

在查阅了所提到的资源后,我相信“启动”指的是第一次在任何给定类型上使用 XmlSerializer,而不是初始应用程序启动。我不能确定;这有点模棱两可。

关于c# - 何时更改 Generate Serialization Assembly 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9187248/

相关文章:

c# 指针与 IntPtr

.net - TPL DataFlow 与 BlockingCollection

c# - 如何将 C++ 代码包装到具有大量 float*(代表点)的 CLI 中?

performance - 为什么 SQL Server ce 中的 Select * FROM Table where ID NOT IN (list of int ids) 查询速度很慢?

Python-什么词可以删除最多的连续字母并且仍然是字典有效词?

java - 加载 JasperPrint 文件时 JasperReports 非常慢

c# - 在 WPF DataGrid Row 中禁用焦点

C# 简单的 2d 游戏 - 制作基本的游戏循环

c# - (unity)有没有办法设置粒子系统中每个粒子的位置?

c# - 有没有办法区分 .NET 中的 SQL Money 和 Decimal 类型?