mscorlib.XmlSerializers.DLL的FileNotFoundException,不存在

时间:2020-03-05 18:39:51  来源:igfitidea点击:

我正在使用XmlSerializer反序列化mscorelib.dll中的特定类型

XmlSerializer ser = new XmlSerializer( typeof( [.Net type in System] ) );
return ([.Net type in System]) ser.Deserialize( new StringReader( xmlValue ) );

加载程序集时,这将引发捕获的FileNotFoundException

"Could not load file or assembly
  'mscorlib.XmlSerializers,
  Version=2.0.0.0, Culture=neutral,
  PublicKeyToken=b77a5c561934e089' or
  one of its dependencies. The system
  cannot find the file specified."

FusionLog:

=== Pre-bind state information ===
LOG: User = ###
LOG: DisplayName = mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
 (Fully-specified)
LOG: Appbase = file:///C:/localdir
LOG: Initial PrivatePath = NULL
Calling assembly : System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\localdir\bin\Debug\appname.vshost.exe.Config
LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
LOG: Post-policy reference: mscorlib.XmlSerializers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=x86
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers/mscorlib.XmlSerializers.DLL.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers.EXE.
LOG: Attempting download of new URL file:///C:/localdir/bin/Debug/mscorlib.XmlSerializers/mscorlib.XmlSerializers.EXE.

据我所知,没有mscorlib.XmlSerializers.DLL,我认为DLL名称已经由.Net自动生成,它正在寻找序列化程序。

我们可以选择在编译以优化序列化时创建myApplication.XmlSerializers.DLL,因此我认为这是框架对其进行检查的一部分。

问题在于,这似乎导致加载应用程序出现延迟,此时它似乎挂了几秒钟。

有什么想法如何避免这种情况或者加快速度吗?

解决方案

回答

延迟的原因是,由于找不到自定义序列化程序dll,系统正在动态构建等效代码(这非常耗时)。

避免延迟的方法是让系统生成DLL,并确保它对.EXE可用。

回答

我现在猜。但:

  • 系统可能正在为整个mscorlib生成一个序列化程序,这可能会非常慢。
  • 我们可以通过将系统类型包装为我们自己的类型并对其进行序列化来避免这种情况-然后我们将为自己的程序集获取序列化程序。
  • 我们也许可以使用sgen.exe为mscorlib构建序列化程序,这是在将序列化程序dll集成到VS中之前的一种古老方法。

回答

好的,所以我遇到了这个问题,并找到了针对我所在地区的解决方案。

发生这种情况是因为我试图将列表序列化为没有XML根属性的XML文档(文件)。添加以下文件后,错误消失了。

XmlRootAttribute rootAttribute = new XmlRootAttribute();
rootAttribute.ElementName = "SomeRootName";
rootAttribute.IsNullable = true;

Dunno是否可以解决问题,但可以解决我的问题。