【问题标题】:How deserialize a Stream with BinaryFormatter in XBAP application?如何在 XBAP 应用程序中使用 BinaryFormatter 反序列化流?
【发布时间】:2013-06-16 20:05:27
【问题描述】:

为什么会出现这种异常,如何反序列化数据? 注意 Deserialize(Stream serializationStream) 方法抛出异常。 Uri 是正确的。该文件位于我的计算机中

    public Structure DeserializeStructForXBAPApplication()
    {
        var uri = new Uri(@"myserialization.bin", UriKind.Relative);

        var info = Application.GetContentStream(uri);

        Debug.Assert(info != null, "info != null");

        var final = (Structure)new BinaryFormatter().Deserialize(info.Stream);

        return final;
    }

抛出的异常是:

A first chance exception of type 'System.Security.SecurityException' occurred in mscorlib.dll
Step into: Stepping over non-user code 'System.RuntimeType.CreateInstanceImpl'
Step into: Stepping over non-user code 'MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance'
Step into: Stepping over non-user code 'System.Windows.Markup.WpfXamlLoader.Load'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'System.Windows.Markup.XamlReader.LoadBaml'
Step into: Stepping over non-user code 'System.Windows.Application.LoadBamlStreamWithSyncInfo'
Step into: Stepping over non-user code 'System.Windows.Threading.DispatcherOperation.InvokeImpl'
Step into: Stepping over non-user code 'System.Threading.ExecutionContext.RunInternal'
Step into: Stepping over non-user code 'System.Windows.Threading.Dispatcher.LegacyInvokeImpl'
Step into: Stepping over non-user code 'System.Windows.Threading.Dispatcher.PushFrameImpl'
Step into: Stepping over non-user code 'System.Windows.Application.StartDispatcherInBrowser'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen'
Step into: Stepping over non-user code 'System.Windows.Threading.DispatcherOperation.InvokeImpl'
Step into: Stepping over non-user code 'System.Threading.ExecutionContext.RunInternal'
Step into: Stepping over non-user code 'System.Windows.Threading.Dispatcher.LegacyInvokeImpl'

编辑 它被序列化为:

    public void SerializationStruct(Structure struc)
    {
        const string path = @"C:\myserialization.bin";


        //byte[] result;
        //Structure final;
        //using (var stream = new MemoryStream())

        using (var stream = new FileStream(path, FileMode.Create))
        {
            new BinaryFormatter().Serialize(stream, struc);
            stream.Flush();
            stream.Close();
            stream.Dispose();
            //result = stream.ToArray();
        }
    }

并且可以通过调用函数来反序列化它

    public Structure  DeserializationStruct()
    {
        Structure final;
        const string path = @"C:\myserialization.bin";

        using (var rStream = new FileStream(path, FileMode.Open))
        {

             final =  (Structure)new BinaryFormatter().Deserialize(rStream);
             rStream.Close();
             rStream.Dispose();
        }

        return final;
    }

由 Winforms 应用程序调用。所以序列化本身不应该是我认为的问题,而是特权问题。

【问题讨论】:

  • 它是如何序列化的?结构如何?
  • 你在xbap中序列化了吗?还是您在 xbap 和“完整”.net 之间传输数据?如果是后者,我建议尝试不同的序列化程序; XmlSerializer 或 protobuf-net 可能
  • 它没有在 XBAP 中序列化,实际上我 90% 确定它在某些 WinForms 应用程序中。 Struct 不是 XML 而是二进制数据
  • @lukasz.pek 该结构既不是 XML 也不是 BinaryFormatter 数据——它很简单:一个结构。然而,做你想做的事情,序列化是你需要的工具——但是 BinaryFormatter 在这里可能根本不起作用。

标签: c# .net serialization xbap


【解决方案1】:

BinaryFormatter 是一个字段级序列化器。它可能无法在沙盒中运行也就不足为奇了。我建议您简单地尝试不同的序列化器 - XmlSerializer 将是一个好的开始,或者可能是 protobuf-net。

BinaryFormatter 不是在不同设置之间传输数据的好选择。即使在单个设置中它也几乎无法工作:p

【讨论】:

  • 嗯...我可以使用 protobuf-net 反序列化使用不同序列化程序(在本例中为 BinaryFormatter)序列化的对象二进制文件吗?
  • @lukasz.pek 不,反序列化器必须与序列化器匹配;但是,目前您无法反序列化根本 - 所以需要更改一些内容
  • 好吧,我想我可能有解决方案。我有 WinForms 应用程序,我可以用它反序列化,然后用 protobuf-net 序列化。有了新文件,我可以使用 protobuf-net 在 XBAP 中反序列化。我一会儿试试……
  • @lukasz 所以你有通过 BinaryFormatter 保存的实际数据吗?我必须告诉您,不管 xbap 会发生什么,使用 BinaryFormatter 来持久化 数据是有点冒险的,在我看来,作为一个花费大量时间的人序列化的时间,并回答“为什么我不能再取回我的数据”的问题。
  • @lukasz 谈到大学,主要做导师想要通过课程完成的事情 - 但是,请记住,这不是现实世界 - 是谨防仅适用于琐碎的短期项目的编码实践;p
猜你喜欢
  • 1970-01-01
  • 2011-05-10
  • 1970-01-01
  • 2012-08-12
  • 2014-12-10
  • 2010-12-10
  • 2013-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多