C# 序列化程序集。需要还是不需要?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/926919/
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
Serialization Assembly. Is it needed or not?
提问by AngryHacker
I have a .net 2.0 c# ClickOnce app and it connects to its data via Web Services. I've been told that one way to potentially speed up the application is to generate a serialization assembly beforehand. I have several questions on this front.
我有一个 .net 2.0 c# ClickOnce 应用程序,它通过 Web 服务连接到它的数据。有人告诉我,一种可能加速应用程序的方法是预先生成序列化程序集。我在这方面有几个问题。
The default setting to whether to generate a serialization assembly is Auto. What criteria does VS2005 use to decide whether to generate a serialization assembly or not? It seems like it does not generate under Debug configuration, but it does under Release configuration, but I can't tell for sure and can't the information anywhere.
Does serialization assembly actually improve the startup of the application? Specifically what does it improve? Do I actually need a serialization assembly?
是否生成序列化程序集的默认设置为 Auto。VS2005 使用什么标准来决定是否生成序列化程序集?似乎它不会在 Debug 配置下生成,但它会在 Release 配置下生成,但我不能确定,也不能在任何地方提供信息。
序列化程序集真的能改善应用程序的启动吗?具体有什么改进?我真的需要序列化程序集吗?
采纳答案by Wyatt Barnett
It is really asking "Shall I pre-generate the serialization assemblies and include it in the deployed project or shall I fall back on the default of generating the assemblies on the fly?" Generally that won't hurt too much after the first hit perf-wise. Where it can play in is that the serialization assemblies are generated in %SYSTEMROOT%\TEMP
. Which, in some cases, the process can't access, leading to fatal exceptions in most cases.
它真的是在问“我应该预先生成序列化程序集并将其包含在部署的项目中,还是应该回退到即时生成程序集的默认设置?” 一般来说,在第一次命中后不会伤害太多。它可以发挥作用的地方是序列化程序集是在%SYSTEMROOT%\TEMP
. 其中,在某些情况下,进程无法访问,在大多数情况下会导致致命异常。
回答by Marc Gravell
In most cases, you aren't likely to see a huge benefit from this, especially if you keep the app open for a while. Pre-generating a serialization assembly mainly helps the firsttime (in an exe lifetime) that you serialize a specific type as xml.
在大多数情况下,您不太可能从中看到巨大的好处,特别是如果您将应用程序保持打开状态一段时间。预生成序列化程序集主要有助于第一次(在 exe 生命周期中)将特定类型序列化为 xml。
回答by Dave Cluderay
This is not relevant to your situation, but there's another good reason for pre-generating the serialization assembly - it's necessary when hosting your code in SQL Server (i.e. SQLCLR). SQL Server doesn't allow these assemblies to be generated dynamically, so your serialization code would fail inside SQL Server.
这与您的情况无关,但是预先生成序列化程序集还有另一个很好的理由 - 在 SQL Server(即 SQLCLR)中托管您的代码时这是必要的。SQL Server 不允许动态生成这些程序集,因此您的序列化代码将在 SQL Server 中失败。
回答by Achilles
According to Intellitrace, only the first time you XML-serialize a type, a FileNotFoundException is thrown and then caught. This means CLR expects to load an assembly containing all the XML-Serializers for that specific Assembly and when it's not found, a FileNotFoundException is thrown to signal the XmlSerializer: "Hey! Generate the darn assembly!" and this is what happens during that "Catch" and then the previously not found file exists.
根据 Intellitrace 的说法,只有在您第一次对类型进行 XML 序列化时,才会抛出 FileNotFoundException 异常然后被捕获。这意味着 CLR 期望加载一个包含该特定程序集的所有 XML 序列化程序的程序集,当未找到它时,将抛出 FileNotFoundException 以向 XmlSerializer 发出信号:“嘿!生成该死的程序集!” 这就是在“Catch”期间发生的事情,然后之前未找到的文件存在。
I've read somewhere that using try-catch for logic is a bad exercise. IDK why Microsoft has used this approach...
我在某处读到使用 try-catch 进行逻辑是一个糟糕的练习。IDK 为什么微软使用这种方法......