简体   繁体   English

具有插件外观的XmlSerializers

[英]XmlSerializers with addin outlook

I'm trying to create add-in for outlook and have issue with deserialization and antivirus program. 我正在尝试为Outlook创建加载项,并且反序列化和防病毒程序存在问题。

I've noticed that when my add-in tried to deserialize any data, .NET framework created temporary dll in “C:\\Users\\{UserName}\\AppData\\Local\\Temp\\" folder. 我注意到,当我的加载项试图反序列化任何数据时,.NET框架在“C:\\Users\\{UserName}\\AppData\\Local\\Temp\\"文件夹中创建了临时dll。
This dll existed very short time, but from time to time antivirus locked it and add-in thrown error message that file is used by another process . 该dll的存在时间很短,但是防病毒软件不时将其锁定,并抛出错误消息,指出该file is used by another process

I'm tried to get rid of temporary dll and found recommendations to use sgen tool for creation of XmlSerializers.dll. 我试图摆脱临时dll,并找到使用sgen工具创建XmlSerializers.dll的建议。

I generated MyAssembly. 我生成了MyAssembly。 XmlSerializers.dll with strong name and placed it to the folder with add-in (C:\\Program Files (x86)\\MyAddin). 具有强名称的XmlSerializers.dll,并将其放置在带有加载项(C:\\ Program Files(x86)\\ MyAddin)的文件夹中。 But it doesn't help. 但这没有帮助。

Then I tried to place MyAssembly. 然后,我尝试放置MyAssembly。 XmlSerializers.dll to GAC and then to outlook folder, but had no success. XmlSerializers.dll到GAC,然后到Outlook文件夹,但没有成功。 When dll was called from GAC I got following error message, but dll has no any reference. 从GAC调用dll时,出现以下错误消息,但dll没有任何引用。

"System.IO.FileNotFoundException: Could not load file or assembly or one of its dependencies. The system cannot find the file specified." “ System.IO.FileNotFoundException:无法加载文件或程序集或其依赖项之一。系统找不到指定的文件。”

Please add any thoughts how can I to get rid of temporary dll 请添加任何想法如何摆脱临时dll

When an XmlSerializer for a type is first constructed, internally the XML serialization engine generates c# code to serialize and deserialize the type, writes it to %TEMP% in temporary file(s), then compiles and loads the resulting assembly, finally deleting any temporary files. 首次构造用于类型的XmlSerializer ,XML序列化引擎在内部生成c#代码以序列化和反序列化该类型,将其写入临时文件中的%TEMP% ,然后编译并加载生成的程序集,最后删除所有临时文件文件。 (Subsequent usages of XmlSerializer reuse the created assembly, see here for details.) XmlSerializer后续用法将重新使用创建的程序集,有关详细信息,请参见此处 。)

It looks as though your antivirus software is being ultra-aggressive at scanning any "unexpected" files created by the Outlook process, immediately trapping the creation of the file and scanning the results. 您的防病毒软件似乎在扫描由Outlook进程创建的任何“意外”文件,立即捕获文件的创建并扫描结果方面过于激进。 While under most circumstances this would seem praiseworthy, it clearly conflicts with Microsoft's design for XmlSerializer . 尽管在大多数情况下这似乎值得称赞,但它显然与Microsoft的XmlSerializer设计冲突。

So, what to do about this? 那么,该怎么办? You have several options: 您有几种选择:

  1. Switch to DataContractSerializer , which does not use this architecture. 切换到不使用此体系结构的DataContractSerializer DataContractSerializer is less flexible that XmlSerializer , however, and may not be able to parse your XML without preprocessing it. 但是, DataContractSerializer的灵活性不如XmlSerializer ,并且如果不进行预处理,可能无法解析XML。

  2. Enable precompiled serialization assemblies. 启用预编译的序列化程序集。 It's not sufficient to simply set GenerateSerializationAssemblies = On as is specified in the documentation , you have to jump through several hoops to make this actually work. 仅仅按照文档中的说明设置GenerateSerializationAssemblies = On是不够的,您必须跳过几个步骤才能使其真正起作用。 See answers to Generating an Xml Serialization assembly as part of my build for ways to do it. 请参阅在我的构建中生成Xml序列化程序集的答案,以获取实现方法。 I was able to make the accepted answer work with my old Visual Studio 2008 by editing my project file as described and then removing the Platform="$(Platform)" attribute. 通过按照说明编辑项目文件,然后删除Platform="$(Platform)"属性,我可以使旧的Visual Studio 2008能够使用公认的答案 You might need to tweak the answer differently for your VS version. 您可能需要针对VS版本不同地调整答案。

    After actually enabling pre-generated serialization DLLs I verified with Process Monitor that no files were written to %TEMP% when deserializing XML in a test console application. 在实际启用了预生成的序列化DLL之后,我使用Process Monitor进行了验证,在测试控制台应用程序中反序列化XML时,没有文件写入%TEMP%

    See also here: Boost performance with Pre-generated XmlSerializers . 另请参见此处: 使用预生成的XmlSerializers提高性能

    Update 更新资料

    After a bit of testing, I found that, if your root object does not exist in the assembly you are building, or if is a generic collection like List<T> or T [] , then precompiled serializer assemblies are not used. 经过一些测试,我发现,如果您的根对象不存在于您正在构建的程序集中,或者是诸如List<T>T []类的通用集合,则不使用预编译的序列化程序程序集。 However, making a non-generic subclass of List<T> re-enables serializer assemblies, eg public class RootObjectList : List<RootObject> { } . 但是,使List<T>的非通用子类重新启用序列化程序程序集,例如, public class RootObjectList : List<RootObject> { }

    For more, see here: All about XmlSerializer Performance and Sgen . 有关更多信息,请参见此处: 有关XmlSerializer性能和Sgen的全部

  3. Convert your XML to JSON with Json.NET, then deserialize the JSON . 使用Json.NET 将XML转换为JSON ,然后反序列化JSON

  4. If your XML is simple, you could load it into an XDocument and query it with Linq to XML . 如果您的XML很简单,则可以将其加载到XDocument ,并使用Linq to XML查询。

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

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