3

Why I get such exception and how can I deserialize the data? Note Deserialize(Stream serializationStream) method throws the exception. The Uri is correct. The file is located in my computer

    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;
    }

The exception thrown is:

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'

EDIT It was serialized with:

    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();
        }
    }

And it is possible to deserialize it using a call of the function

    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;
    }

called by a Winforms application. So the serialization itself shouldn't be the problem I think, but it's a matter of priviledges.

lixonn
  • 1,033
  • 1
  • 12
  • 28
  • How was it serialized? How does structure look like? – rene Jun 16 '13 at 20:19
  • Did you serialize in the xbap? Or are you transferring data between xbap and "full" .net? If the latter, I'd suggest trying a different serializer; XmlSerializer or protobuf-net maybe – Marc Gravell Jun 16 '13 at 20:25
  • It wasn't serialized in XBAP, actually I am 90% sure that it was in some WinForms application. The Struct is not XML but binary data – lixonn Jun 16 '13 at 20:26
  • @lukasz.pek the struct is neither XML nor BinaryFormatter data - it is simply: a struct. However, to do what you want serialization is the tool you need - but BinaryFormatter may simply not work here. – Marc Gravell Jun 16 '13 at 20:29

1 Answers1

1

BinaryFormatter is a field-level serializer. It is not surprising that it might not work in a sandbox. I suggest you simply try a different serializer - XmlSerializer would be a good start, or protobuf-net maybe.

BinaryFormatter is not a good choice for transferring data between different setups. It barely works even n a single setup :p

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Well... Can I deserialize an object binary serialized with a different serializer (BinaryFormatter in this case) with protobuf-net? – lixonn Jun 16 '13 at 20:33
  • 1
    @lukasz.pek no, the deserializer must match the serializer; however, at the moment you can't deserialize *at all* - so something needs to be changed – Marc Gravell Jun 16 '13 at 20:36
  • Well I think I might have the solution. I've got the WinForms application and I can deserialize with it then serialize with protobuf-net. Having the new file I can deserialize in XBAP with protobuf-net. I will try it in a moment... – lixonn Jun 16 '13 at 20:47
  • 1
    @lukasz so you have the actual data persisted via BinaryFormatter? I have to advise you that *regardless* of what happens with the xbap, using BinaryFormatter to *persist* data is more than a little risky, in my opinion as someone who spends a lot of time in serialisation, and answering "how come I can't get my data back any more" questions. – Marc Gravell Jun 16 '13 at 20:53
  • @lukasz when it comes to uni, do *mainly* what the tutor wants to get through the course - however, keep in mind that it isn't the real world - be cautious of coding practices that only work on trivial short-lived projects ;p – Marc Gravell Jun 16 '13 at 21:03