質問

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.

役に立ちましたか?

解決

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top