1

I want to serialize an array of structs with binary formatter to send it over network or save it to file in this case.
It is subscribed to the Form.Closing event of my Form

void writeHistoryToFile(object sender, CancelEventArgs e)
{
    ListView.ListViewItemCollection coll = historyListView.Items;
    int count = coll.Count;
    if(count == 0)
        return;
    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream memStr = new MemoryStream();
    searchResult[] container = new MainForm.searchResult[count];
    for(int i = 0; i < count; i++)
    {
        searchResult tagged = (searchResult)coll[i].Tag;
        container[i] = tagged;
    }
    byte[] bytesToWrite;
    bf.Serialize(memStr, container);   //HERE
    bytesToWrite = memStr.ToArray();   //BREAKPOINT

    List<FileInfo> hisFls = historyFiles;
    if(hisFls.Count != 0)
    {
        foreach(FileInfo element in hisFls)
        {
            element.Delete();
        }
    }

    FileInfo serFile = getTempFile(".avsh");
    using(FileStream writeStream = serFile.OpenWrite())
    {
        writeStream.Write(bytesToWrite, 0, bytesToWrite.Length);
        writeStream.Flush();
    }
}

What it does:
I close the form and the function gets called (breakpoints set above HERE are triggered).
When let running loose, the form doesn't close and becomes responsive again.
When stepping through the code, the debugger shortly hangs at HERE and then the code runs continuously again.
Breakpoints set behind HERE are never reached.

So it seems that the binary formatter returns the function or something, but without any Exception or such.

UPATE
Looks like my struct is not serializable, duh.
Added [Serializable] and it works now. See comments as to why there were no exceptions etc.. -_- Errormessage and stacktrace

The Type "ExportGrepper.MainForm+searchResult" in Assembly Arbeitsvorräte duchsuchen, Version=1.0.5351.15603, Culture=neutral, PublicKeyToken=null" is not marked as serializable.

   at System.Runtime.Serialization.FormatterServices.InternalGetSerializableMembers(RuntimeType type)

   at System.Runtime.Serialization.FormatterServices.GetSerializableMembers(Type type, StreamingContext context)

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitMemberInfo()

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)

   at System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.Serialize(Type objectType, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteArray(WriteObjectInfo objectInfo, NameInfo memberNameInfo, WriteObjectInfo memberObjectInfo)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo)

   at System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck)

   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck)

   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph)

   at ExportGrepper.MainForm.writeHistoryToFile(Object sender, CancelEventArgs e)
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mark
  • 419
  • 1
  • 6
  • 21
  • 1
    Try adding a Try..Catch into your function (or just around your serialization). What Exception do you get? – AlexS Aug 26 '14 at 07:35
  • 1
    http://stackoverflow.com/questions/4933958/vs2010-does-not-show-unhandled-exception-message-in-a-winforms-application-on-a/4934010#4934010 – Hans Passant Aug 26 '14 at 07:43
  • @HansPassant `It swallows exceptions` ?!??! wha..? That could've taken me a while, because I was sure it was the formatter itself not my struct as I now found out. – Mark Aug 26 '14 at 07:46

1 Answers1

1

"Answer" to be able to close this question: Try adding a Try..Catch to your code and check which error occurs. Then make your struct serializable. ;)

AlexS
  • 344
  • 4
  • 14