none
Mysterious error, using the Autostart function RRS feed

  • Question

  • Hello,

    I have an error that I can't understand:

    "System.Runtime.Serialization.SerializationException: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization."

    I have made an application in VB.NET on Visual Studio 2017 that runs on a device with Windows 10 IoT operating system. The application works great when I start it in a normal way. However, it is necessary to start when the device is powered. And accordingly I put a shortcut in "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup" to the exe-file.

    This is also working fine, until we test the device for crashes.

    The application uses serialization  and I make a file like this:

                StringFile = "C:\Users\Public\data.stn"

            Dim f As New FileStream(StringFile, IO.FileMode.Create)
            Dim fo As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
            Try
                fo.Serialize(f, variable)

    more data....

                f.Close()
            Catch ex As Exception
                EventLog("Exception when saving file data.stn." & ex.ToString)
            End Try

    when I run the application and close the application correctly, I can see that the file is there and data is written in the file.

    The application also reads the file by using deserialization. And then when I turn off the device the autorun starts up but gives the error as above, and when I check the file data.stn its surprisingly empty...

    Typically it works the first time the device is restarted, by crash, but when it is run again it throws the exception. Actually it seems not depend on if the application is terminated correctly or when I turn it of while the application is running. The error message is displayed.

    Is there any help to get to understand why the file is empty? I suppose the error is simply explaining that it reads nothing?

    Much appreciate to get help with this!

    Best Regards,

    Per


    Wednesday, October 16, 2019 1:20 PM

Answers

  • Finally I believe that I have found the solution. Rather brutal, but I changed the settings and disconnected the cache write function. Now I have taken in and out the cord for an hour or so and no failure occur. I'm a bit concerned about if this slows down the prestanda, so it could be an idea to partion the disk and put the files there and  have the cache write unabled on that partion, but keep it for the C:!?

    Some times solutions are surprising, as its easy to get lost in all the possible source of the error, but this time it seems not to be a programming error?? 

    Thanks to everyone who read this, and perhaps someone can get help from this solution idea!

    Cheers,

    Per


    Friday, October 18, 2019 8:51 AM

All replies

  • Hi,

    There are several possible reasons for this problem.

    1. Byte and String have errors in type conversion, you can try to use convert.ToBaseString or convert.FromBaseString.

    2. The current read and write position of stream has an error. You can debug to check it.

    Someone has encountered a similar error and hopes to help you.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, October 18, 2019 5:43 AM
    Moderator
  • Hi Julie,

    Many thanks for your reply!

    Problem is that I can't debug the application as it occur on the target machine where I have a minimum of software. On my developer PC it works perfect, and as long as I run the application on the target machine (Window 10 IoT) it also works fine when I close it down from the device.

    The problem is when there is a power failure, most of the times it uses a recovery and ask me to restart the device and again it works, but sometimes it comes up, without a recovery message, and again sometimes the error is displayed. When checking the directory the file data.stn is empty (All data is gone). 

    Thus I did a backup file with the same data - wrote it down with the same values (but not a direct copy) 100ms after creating the first file. But at the crashes BOTH files are empty. 

    I also added f.Dispose() after close, but this failed as well.

    The device machine has an extra 60ms at a power failure and my thinking now is that for some reason(?) when the power failure occur, windows open the files in the cache to write it on a recovery and then this fails leaving the files empty? But now I'm out on my depth.

    The device needs to return after a power failure as it will be in use where power failures are frequent.

    Best Regards, Per

    Friday, October 18, 2019 7:28 AM
  • Finally I believe that I have found the solution. Rather brutal, but I changed the settings and disconnected the cache write function. Now I have taken in and out the cord for an hour or so and no failure occur. I'm a bit concerned about if this slows down the prestanda, so it could be an idea to partion the disk and put the files there and  have the cache write unabled on that partion, but keep it for the C:!?

    Some times solutions are surprising, as its easy to get lost in all the possible source of the error, but this time it seems not to be a programming error?? 

    Thanks to everyone who read this, and perhaps someone can get help from this solution idea!

    Cheers,

    Per


    Friday, October 18, 2019 8:51 AM