XmlSerializer保存临时文件的最安全地方 [英] Safest place for the XmlSerializer to save temp files

查看:141
本文介绍了XmlSerializer保存临时文件的最安全地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到XmlSerializer需要使用磁盘空间来进行出价。如果没有可写的%temp%文件夹,则它将失败,并显示以下错误:

It's come to my attention that the XmlSerializer needs to use disk space to do its bidding. If there is no writeable %temp% folder, then it fails with an error as follows:


源:系统.Xml消息:无法生成临时类(结果= 1)。错误CS2001:找不到源文件'C:\Windows\TEMP\c1ls4elp.0.cs'错误CS2008:未指定输入StackTrace:在System.Xml.Serialization.Compiler.Compile(汇编父,字符串ns ,System.Xml.Serialization.TempAssembly.GenerateAssembly(XmlMapping [] xmlMappings,Type []类型,字符串defaultNamespace,证据,XmlSerializerCompilerParameters参数,装配体,哈希表装配体)处的XmlSerializerCompilerParameters xmlParameters,证据证据)。 XmlSerializer.GenerateTempAssembly(XmlMapping xmlMapping,Type type,String defaultNamespace)在System.Xml.Serialization.XmlSerializer..ctor(Type type,String defaultNamespace)在StreamLib.Tuna.SerializationHelper.Deserialize [T](String预置字符串)... <

作为参考, StreamLib.Tuna.SerializationHelper.Deserialize [T ] 如下所示:

    public static T Deserialize<T>(this string data) where T:class
    {
        var type = typeof(T);

        XmlSerializer serializer = new XmlSerializer(type);
        using (TextReader reader = new StringReader(data))
        {
            try
            {
                return (T)serializer.Deserialize(reader);

            }
            catch
            {

                return null;
            }
        }
    }

更改文件夹的权限我认为最好留给用户,而不是给狡猾的序列化器打补丁,所以我想通过在其他地方给序列​​化器写废话来解决问题。这可以通过在 app.config / web.config 中添加以下内容来实现:

Changing permissions of folders is something I think best left to the user rather than a patch for a dodgy serializer, so instead I want to fix the problem by giving the serializer somewhere else to write its crap. This can be achieved by adding the following to app.config/web.config:

<system.xml.serialization> 
  <xmlSerializer tempFilesLocation="c:\\foo"/> 
</system.xml.serialization>

我的问题是,此设置是否有防弹位置使用,在某些客户端上不会失败机器?如果没有,我有什么选择? DataContractJsonSerializer 是否同样需要磁盘空间吗?

My question is, is there a bulletproof location use for this setting that won't fail on some client machines? If not, what are my alternatives? Does the DataContractJsonSerializer also require disk space in the same way?

推荐答案

DataContractSerializer,NetDataContractSerializer和DataContractJsonSerializer都是您的理想选择。它们不需要磁盘空间,也不会将程序集释放到磁盘。取而代之的是,它们会在内存中动态生成IL,并在随后的序列化情节中使用它来在其运行的AppDomain中进行序列化和反序列化。正如您所发现的,XmlSerializer确实需要磁盘空间。从好的方面来说,您不需要更改任何类型-只需替换序列化程序,您就应该会做的很好,因为DataContractSerializer支持Microsoft曾经发布的所有其他序列化程序的序列化格式,模型和范例在.NET

DataContractSerializer, NetDataContractSerializer and DataContractJsonSerializer would all be good alternatives for you. They do NOT require disk space and do NOT emit assemblies to disk. Instead, they generate IL on the fly (in memory) and use it during subsequent serialization episodes to do serialization and deserialization all within the AppDomain they're operating in. XmlSerializer does require disk space, as you found out. On the plus side, you shouldn't need to change any of your types -- just replace the serializer and you should be good to go, since DataContractSerializer supports the serialization formats, models and paradigms of all the other serializers that Microsoft has ever shipped in .NET

这篇关于XmlSerializer保存临时文件的最安全地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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