带有插件外观的 XmlSerializers [英] XmlSerializers with addin outlook

查看:24
本文介绍了带有插件外观的 XmlSerializers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 Outlook 创建加载项,但在反序列化和防病毒程序方面存在问题.

I’m trying to create add-in for outlook and have issue with deserialization and antivirus program.

我注意到,当我的加载项尝试反序列化任何数据时,.NET 框架会在 C:\Users\{UserName}\AppData\Local\Temp\" 中创建临时 dll文件夹.
这个dll存在的时间很短,但有时杀毒软件会锁定它,加载项会抛出文件被另一个进程使用的错误消息.

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.
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 并找到使用 sgen 工具创建 XmlSerializers.dll 的建议.

I’m tried to get rid of temporary dll and found recommendations to use sgen tool for creation of XmlSerializers.dll.

我生成了 MyAssembly.具有强名称的 XmlSerializers.dll 并将其放置到带有加载项的文件夹 (C:\Program Files (x86)\MyAddin).但这无济于事.

I generated MyAssembly. XmlSerializers.dll with strong name and placed it to the folder with add-in (C:\Program Files (x86)\MyAddin). But it doesn’t help.

然后我尝试放置 MyAssembly.XmlSerializers.dll 到 GAC,然后到 Outlook 文件夹,但没有成功.当从 GAC 调用 dll 时,我收到以下错误消息,但 dll 没有任何引用.

Then I tried to place MyAssembly. XmlSerializers.dll to GAC and then to outlook folder, but had no success. When dll was called from GAC I got following error message, but dll has no any reference.

"System.IO.FileNotFoundException: 无法加载文件或程序集或它的依赖项之一.系统找不到指定的文件."

"System.IO.FileNotFoundException: Could not load file or assembly or one of its dependencies. The system cannot find the file specified."

请添加任何想法如何摆脱临时 dll

Please add any thoughts how can I to get rid of temporary dll

推荐答案

XmlSerializer先构造一个类型,内部XML序列化引擎生成c#代码对类型进行序列化和反序列化,写出来到临时文件中的 %TEMP%,然后编译并加载生成的程序集,最后删除所有临时文件.(XmlSerializer 的后续用法重用创建的程序集,请参阅 此处 了解详情.)

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. (Subsequent usages of XmlSerializer reuse the created assembly, see here for details.)

看起来您的防病毒软件在扫描 Outlook 进程创建的任何意外"文件方面表现得非常积极,立即捕获文件的创建并扫描结果.虽然在大多数情况下这看起来值得称赞,但它显然与 Microsoft 的 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. While under most circumstances this would seem praiseworthy, it clearly conflicts with Microsoft's design for XmlSerializer.

那么,该怎么办呢?您有多种选择:

So, what to do about this? You have several options:

  1. 切换到 DataContractSerializer,它不使用这种架构.DataContractSerializer 不如 XmlSerializer 灵活,并且可能无法在不预处理的情况下解析您的 XML.

  1. Switch to DataContractSerializer, which does not use this architecture. DataContractSerializer is less flexible that XmlSerializer, however, and may not be able to parse your XML without preprocessing it.

启用预编译的序列化程序集.仅仅按照 文档,您必须跳过几个环节才能使其真正起作用.请参阅生成 Xml 序列化程序集作为一部分的答案我的构建的方法.我能够做出 接受的答案 使用我的旧 Visual Studio 2008 编辑我的项目文件,然后删除 Platform="$(Platform)" 属性.您可能需要针对您的 VS 版本对答案进行不同的调整.

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. See answers to Generating an Xml Serialization assembly as part of my build for ways to do it. 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. You might need to tweak the answer differently for your VS version.

在实际启用预生成的序列化 DLL 后,我使用 Process Monitor 在测试控制台应用程序中反序列化 XML 时,没有文件被写入 %TEMP%.

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.

另见此处:使用预先生成的 XmlSerializers 提高性能.

更新

经过一些测试,我发现,如果您正在构建的程序集中不存在您的根对象,或者是像 ListT 这样的通用集合[],则不使用预编译的序列化程序集.但是,创建 List 的非通用子类会重新启用序列化程序集,例如public class RootObjectList : List{ }.

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. However, making a non-generic subclass of List<T> re-enables serializer assemblies, e.g. public class RootObjectList : List<RootObject> { }.

有关更多信息,请参见此处:有关 XmlSerializer 性能的所有信息和 Sgen.

For more, see here: All about XmlSerializer Performance and Sgen.

使用 Json.NET 将您的 XML 转换为 JSON,然后反序列化 JSON.

如果您的 XML 很简单,您可以将其加载到 XDocument 并使用 Linq to XML.

If your XML is simple, you could load it into an XDocument and query it with Linq to XML.

这篇关于带有插件外观的 XmlSerializers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆