C# 何时更改“生成序列化程序集”值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9187248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
When to change the Generate Serialization Assembly value?
提问by John Isaiah Carmona
I have a client winform application that connects to the local network server of WCF. There has a performance issue on the client side and I searched for the solution and found thispost.
我有一个客户端 winform 应用程序,它连接到 WCF 的本地网络服务器。客户端存在性能问题,我搜索了解决方案并找到了这篇文章。
It says there that:
那里说:
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.
这听起来像是在运行时创建的序列化程序集。尝试更改项目属性窗口“构建”窗格底部的“序列化程序集”下拉列表的设置。
My question is When to change the Generate Serialization Assemblyvalue and what value should I change it into to improve the performance of my client side application?
我的问题是何时更改Generate Serialization Assembly值以及我应该将其更改为什么值以提高客户端应用程序的性能?
My codes is in C#, framework 4, build in VS2010Pro.
我的代码是 C#,框架 4,在 VS2010Pro 中构建。
采纳答案by Zenexer
In order to serialize classes/structs, serialization assemblies need to be generated. This can happen at compiletime or runtime. Sgen.exe is used to generate serialization assemblies at compiletime; Visual Studio can optionally automate this process, as you have discovered.
为了序列化类/结构,需要生成序列化程序集。这可能发生在编译时或运行时。Sgen.exe 用于在编译时生成序列化程序集;正如您所发现的,Visual Studio 可以选择自动执行此过程。
- Off: Default for Debug configurations (thanks, @Alexandru Lache). Do not generate serialization assemblies at compiletime. Serialization assemblies will be generated each time the application runs, according to 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.
- On: Use Sgen.exe to generate a serialization assembly at compiletime. This saves startup time, but increases deployment size.
- Auto: Default for Release configurations. Officially, only generates assembly if
XmlSerializeris used in your code, per MSDN(thanks, @L-Three). In my tests, this didn't always work, so I recommend explicitly setting it toOnif you are usingXmlSerializer.
- Off:调试配置的默认值(谢谢,@Alexandru Lache)。不要在编译时生成序列化程序集。根据MSDN,每次应用程序运行时都会生成序列化程序集:
如果不使用 XML Serializer Generator,每次运行应用程序时,XmlSerializer 都会为每种类型生成序列化代码和序列化程序集。为了提高 XML 序列化启动的性能,请使用 Sgen.exe 工具预先生成那些程序集。然后可以随应用程序部署这些程序集。
- On:使用 Sgen.exe 在编译时生成序列化程序集。这会节省启动时间,但会增加部署规模。
- 自动:发布配置的默认值。正式地,
XmlSerializer根据MSDN,只有在您的代码中使用时才会生成程序集(谢谢,@L-Three)。在我的测试中,这并不总是有效,因此On如果您使用XmlSerializer.
So, my answer would be this: if you are concerned about startup time, and you use the Serializableattribute even once, set the option to On. If you are more concerned about deployment size, change it to Off. I never leave it on Auto anymore, because I don't trust it. Like I said, it seems to be the same as Off, but I wouldn't count on it.
因此,我的回答是:如果您担心启动时间,并且您Serializable甚至使用该属性一次,请将选项设置为 On。如果您更关心部署大小,请将其更改为关闭。我再也不会把它放在 Auto 上了,因为我不相信它。就像我说的,它似乎与 Off 相同,但我不会指望它。
Edit: I'm definitely having some trouble differentiating between Off and Auto. The difference isn't clearly defined anywhere. I'd stick with On if you use the Serializable attribute at all, and Off if you don't. I wouldn't take deployment size or startup time into account. I just seem to run into fewer serialization-related bugs if I stick to that rule.
编辑:我肯定在区分关闭和自动时遇到了一些麻烦。差异在任何地方都没有明确定义。如果你完全使用 Serializable 属性,我会坚持使用 On,如果你不使用,我会坚持使用 Off。我不会考虑部署规模或启动时间。如果我坚持该规则,我似乎只会遇到更少的与序列化相关的错误。
Update:
更新:
After a review of the sources mentioned, I believe "startup" refers to the first time an XmlSerializeris used on any given type, not initial application launch. I can't be sure; it's a bit ambiguous.
在对提到的来源进行后,我相信“启动”是指第一次XmlSerializer在任何给定类型上使用an ,而不是初始应用程序启动。我不能确定;这有点模棱两可。

