我用 C# 编写了一个多线程 Windows 服务。由于某种原因,每次生成线程时都会启动 csc.exe。我怀疑它与线程本身有关,但事实上它是在每个线程的基础上发生的,而且这些线程是短暂的,这使得问题非常明显:许多 csc.exe 进程不断启动和停止。
性能仍然相当不错,但我希望如果我能消除它,它会有所改善。然而,更让我担心的是 McAfee 正在尝试扫描 csc.exe 实例并最终终止该服务,显然当其中一个实例在扫描过程中退出时。我需要商业部署此服务,因此更改 McAfee 设置不是解决方案。
我假设我的代码中的某些东西正在触发动态编译,但我不确定是什么。还有其他人遇到这个问题吗?有什么解决办法吗?
更新 1:
根据@sixlettervariables 的建议和链接进一步研究后,问题似乎源于 XML 序列化的实现,如 Microsoft's documentation on XmlSerializer: 中所示。
To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types.
Microsoft 在同一文档中进一步说明了一项优化:
The infrastructure finds and reuses those assemblies. This behavior occurs only when using the following constructors:
XmlSerializer.XmlSerializer(Type)
XmlSerializer.XmlSerializer(Type, String)
这似乎表明代码生成和编译只会发生一次,在第一次使用时,只要使用两个指定的构造函数之一。但是,我没有从这种优化中受益,因为我使用的是另一种形式的构造函数,具体而言:
public XmlSerializer(Type type, Type[] extraTypes)
进一步阅读,事实证明这也恰好是我在代码执行时观察到的内存泄漏的可能解释。同样,来自同一个文档:
If you use any of the other constructors, multiple versions of the same assembly are generated and never unloaded, which results in a memory leak and poor performance. The easiest solution is to use one of the previously mentioned two constructors. Otherwise, you must cache the assemblies in a Hashtable.
微软在上面建议的两个解决方法是我的最后一招。转到另一种形式的构造函数不是首选(我使用“extratypes”形式来序列化派生类,这是微软文档支持的用法),而且我不确定我是否喜欢管理缓存的想法跨多个线程使用的程序集。
所以,我有 sgen 'd,并查看按预期生成的针对我的类型生成的序列化器程序集,但是当我的代码执行时,未加载 sgen 生成的程序集(根据融合日志查看器和进程监视器中的观察)。我目前正在探索为什么会这样。
更新 2:
当我使用两个“更友好”的 XmlSerializer 构造函数之一时,sgen 程序集加载正常(请参阅上面的更新 1)。例如,当我使用 XmlSerializer(Type)
时,sgen'd 程序集加载并且不执行运行时代码生成/编译。但是,当我使用 XmlSerializer(Type, Type[])
时,程序集不会加载。找不到任何合理的解释。
所以我要恢复使用受支持的构造函数之一和 sgen'ing。这种组合消除了我原来的问题(启动 csc.exe),加上另一个相关问题(上面更新 1 中提到的由 XmlSerializer 引起的内存泄漏)。但是,这确实意味着我必须恢复为派生类型的不太理想的序列化形式(在基类型上使用 XmlInclude
),直到框架发生某些变化以解决这种情况。
最佳答案
心理调试:
- Your Windows Service does XML serialization/deserialization
To increase performance, the XML serialization infrastructure dynamically generates assemblies to serialize and deserialize specified types.
If this is the case you can build these XML Serializer Assemblies a-priori.
关于c# - 为什么我的 Windows 服务启动 csc.exe 的实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6286127/