简体   繁体   English

C#XmlSerializer BindingFailure

[英]C# XmlSerializer BindingFailure

I get a BindingFailure on a line of code using the XmlSerializer: 我使用XmlSerializer在一行代码上得到一个BindingFailure:

XmlSerializer s = new XmlSerializer(typeof(CustomXMLSerializeObject));

The assembly with display name CustomXMLSerializeObject.XmlSerializers' failed to load in the 'LoadFrom' binding context of the AppDomain with ID 1. The cause of the failure was: System.IO.FileNotFoundException: Could not load file or assembly XMLSerializeObject.XmlSerializers, Version=1.4.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 具有显示名称CustomXMLSerializeObject.XmlSerializers'的程序集无法加载到ID为1的AppDomain的“LoadFrom”绑定上下文中。失败的原因是:System.IO.FileNotFoundException:无法加载文件或程序集XMLSerializeObject.XmlSerializers,Version = 1.4.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一。 The system cannot find the file specified. 该系统找不到指定的文件。

The error is quite long and goes on to explain pre-bind state information and the places it looked to try and find the file. 错误很长,继续解释预绑定状态信息以及它试图找到文件的位置。

The custom object I am trying to desrialize is relatively simple - just a bunch of private integers and strings that have public accessors. 我试图去序列化的自定义对象相对简单 - 只是一堆私有整数和具有公共访问器的字符串。 I do have a private variable that is another custom serializeable class but that one has nothing but private strings with public accessors in it. 我有一个私有变量,它是另一个自定义的可序列化类,但它只有私有字符串,其中包含公共访问器。

The awkward part? 尴尬的部分? This only happens when I deserialize. 这只在我反序列化时发生。 That line of code runs fine when I serialize the object. 当我序列化对象时,那行代码运行正常。 It works fine and the object gets deserialized and populated perfectly. 它工作正常,对象被反序列化并完美填充。 Don't really notice any loss of performance or long loading time. 不要注意任何性能损失或加载时间过长。

What exactly is this warning (not an error or exception, program runs fine afterwards)? 这个警告究竟是什么(不是错误或异常,之后程序运行良好)? Why does it happen? 为什么会这样? How do I prevent it without simply disabling the warning? 如何在不简单禁用警告的情况下阻止它?

According to Strange XmlSerializer error : 根据Strange XmlSerializer错误

This exception is a part of the XmlSerializer's normal operation. 此异常是XmlSerializer正常操作的一部分。 It is expected and will be caught and handled inside of the Framework code. 它是预期的,将在Framework代码中捕获和处理。 Just ignore it and continue. 只需忽略它并继续。 If it bothers you during debugging, set the Visual Studio debugger to only stop on unhandled exceptions instead of all exceptions. 如果在调试期间困扰您,请将Visual Studio调试器设置为仅停止未处理的异常而不是所有异常。

Its probably being caused based on your exceptions that you are choosing to monitor. 它可能是由您选择监控的例外情况引起的。

Can you tell me how your exceptions are setup: Debug -> Exceptions 你能告诉我你的异常是如何设置的:Debug - > Exceptions

If you uncheck the "Thrown" checkbox for the BindingFailure under the Managed Debugging Assistants the exception should go away. 如果取消选中Managed Debugging Assistants下BindingFailure的“Thrown”复选框,则异常应该消失。 Or if you dont want to do this, you can just continue since this exception is by design 或者如果你不想这样做,你可以继续,因为这个例外是设计的

Use the following method to construct your xmlSerializer instance will fix the problem: 使用以下方法构造xmlSerializer实例将解决问题:

XmlSerializer s = XmlSerializer.FromTypes(new[] { typeof(CustomXMLSerializeObject) })[0];

then, you don't need to turn off the exception handlings. 那么,您不需要关闭异常处理。

According to MS VS 2010 Feedback this is how it was designed. 根据MS VS 2010反馈,这是它的设计方式。 In order to prevent this exception and prevent a slow-down during run-time execution you need to generate a XML Serializer assembly. 为了防止此异常并防止在运行时执行期间减慢,您需要生成XML Serializer程序集。

There are three tools I could find: Microsoft SGen , XGenPlus and Mvp.Xml.XGen . 我可以找到三种工具: Microsoft SGenXGenPlusMvp.Xml.XGen As of this post, unfortunately, none of these has been updated since 2007. 不幸的是,自2007年以来,这些帖子都没有更新过。

Alright I've found a solution. 好吧,我找到了解决方案。 I never could accept turning off exceptions as an answer. 我永远不能接受将异常作为答案。 Just seems somehow wrong.... 只是看起来有些不对......

What seems to be happening is that in previous assemblies, or previous versions of your current assembly, certain references were used externally. 似乎正在发生的事情是,在以前的程序集或当前程序集的先前版本中,某些引用是在外部使用的。 Even though your code may have long since abandoned those references, the names are still, some mysterious somewhere, being searched for in the assembly. 尽管你的代码可能早已放弃了这些引用,但在程序集中搜索的名称仍然是某些神秘的东西。

Go to your AssemblyInfo.cs files and find ThemeInfo: 转到AssemblyInfo.cs文件并找到ThemeInfo:

[assembly: ThemeInfo(
ResourceDictionaryLocation.ExternalAssembly, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries))]

Change the first location to 'None': 将第一个位置更改为“无”:

[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page, 
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page, 
// app, or any theme specific resource dictionaries))]

And keep your exceptions turned on! 并保持您的例外开启! I will be posting this answer to various questions of this similar nature. 我将把这个答案发给这个类似性质的各种问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM