如果我使用“仅链接 SDK”构建我的应用程序以发布版本(用于在没有 Mono 的机器上进行 beta 测试),它不再启动——它似乎立即崩溃。
这是我运行我的应用程序命令行时的崩溃:
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: An exception was thrown by the type initializer for System.Xml.Serialization.XmlSerializer ---> System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system. ---> System.MissingMethodException: Default constructor not found for type System.Configuration.ExeConfigurationHost.
at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
at System.Configuration.InternalConfigurationSystem.Init (System.Type typeConfigHost, System.Object[] hostInitParams) [0x00000] in <filename unknown>:0
at System.Configuration.InternalConfigurationFactory.Create (System.Type typeConfigHost, System.Object[] hostInitConfigurationParams) [0x00000] in <filename unknown>:0
at System.Configuration.ConfigurationManager.OpenExeConfigurationInternal (ConfigurationUserLevel userLevel, System.Reflection.Assembly calling_assembly, System.String exePath) [0x00000] in <filename unknown>:0
at System.Configuration.ClientConfigurationSystem.get_Configuration () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Configuration.ClientConfigurationSystem.get_Configuration () [0x00000] in <filename unknown>:0
at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection (System.String configKey) [0x00000] in <filename unknown>:0
at System.Configuration.ConfigurationManager.GetSection (System.String sectionName) [0x00000] in <filename unknown>:0
at System.Configuration.ConfigurationSettings.GetConfig (System.String sectionName) [0x00000] in <filename unknown>:0
at System.Xml.Serialization.XmlSerializer..cctor () [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
有什么方法可以在此处添加适当的 [Preserve]
属性?或者强制链接器保留这个类?
更多信息:
- 我的应用程序是 MonoGame游戏,名为Draw a Stickman: EPIC
- 如果我不链接任何程序集,它运行得很好,但我的应用程序要大 10MB
- 最初我从 Tao.Sdl.dll 的链接器中得到了一些错误,但我在我的主项目中引用了它,并修复了它。
- 我的目标是 10.6 及更高版本
最佳答案
与 MonoTouch 的 mtouch
相比,MMP (MonoMac Packer) 的主要区别在于 MonoMac 使用完整的 .NET 框架。这使得链接应用程序变得更加复杂。
为什么?主要是因为 System.Configuration
(它是一个程序集,但它也是其他几个程序集中的命名空间)。它使用反射和 .config
文件来设置自己 - 链接器无法知道(因为它可以在执行之间更改)您的应用程序究竟需要什么。
MMP 尝试涵盖最常见的情况(例如 System.Net),但目前并不能处理在 BCL 内部使用 System.Configuration 的所有情况。
现在要解决这个问题...您需要告诉链接器包含您的应用程序在运行时需要的所有内容。例如
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException:
An exception was thrown by the type initializer for System.Xml.Serialization.XmlSerializer
---> System.Configuration.ConfigurationErrorsException: Error Initializing the configuration system.
---> System.MissingMethodException: Default constructor not found for type System.Configuration.ExeConfigurationHost.
这意味着您的应用程序正在使用(即使是间接使用)System.Configuration.ExeConfigurationHost
并且链接器无法检测到它(它是通过反射使用的)。
从那里你需要build an XML file 教导链接器关于那些必需的类型(会有其他类型,可能还有很多其他类型)。然后将该 XML 文件反馈给 MMP (--xml=FILE),这样链接器就不会尝试从链接的应用程序中删除这些类型/方法。
备选方案是不链接或不链接某些程序集(例如--linkskip=System
)。 YMMV.
更新:新的 Xamarin.Mac ,MonoMac 的超集,包含一个增强版本的链接器,可以解决这个异常(但可能不是所有其他的,请为它们填写错误报告)。
关于c# - MonoMac - 链接器中断应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13801861/