Microsoft Knowledge Base Article
This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved.
Terms
of Use |
Trademarks
Article ID: 947983 - Last Review: April 12, 2008 - Revision: 1.2
Binary serialization exceptions occur when you serialize or deserialize the DateTimeFormatInfo class or the TextInfo class between different versions of the .NET Framework
When you serialize or deserialize the
DateTimeFormatInfo class or the
TextInfo class between a computer that is running the Microsoft .NET Framework 2.0 Service Pack 1 (SP1) and a computer that is running a pre-SP1 version of the .NET Framework 2.0, you receive one of the following binary serialization exceptions:
Exception 1Exception type: System.Runtime.Serialization.Serialization
Exception
Message: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
Exception 2Exception type: System.ArgumentNull
Exception
Message: Value cannot be null.
This problem occurs when you serialize the
DateTimeFormatInfo class and the
TextInfo class by setting the
TypeFormat property of the
BinaryFormatter class to
FormatterTypeStyle.XsdString.
This problem occurs because the
FormatterTypeStyle.XsdString serialization formatting and the
FormatterTypeStyle.TypesWhenNeeded serialization formatting drop type information for performance reasons when you serialize types across different versions of the .NET Framework.
To work around this issue, use one of the following methods.
Method 1
Use the same version of the .NET Framework for both serialization and deserialization.
Method 2
Do not set the
TypeFormat property of the
BinaryFormatter class to
FormatterTypeStyle.XsdString or to
FormatterTypeStyle.TypesWhenNeeded.
Note When you do not use the
FormatterTypeStyle.XsdString serialization formatting or the
FormatterTypeStyle.TypesWhenNeeded serialization formatting, performance may decrease.
Method 3
To use the
FormatterTypeStyle.XsdString serialization formatting because of the performance benefits, create a surrogate for each affected type that you have to serialize. In this manner, you can control serialization for the types that are affected. However, no performance decrease occurs for the types that are not affected.
Any time that you are using the
FormatterTypeStyle.XsdString serialization formatting or the
FormatterTypeStyle.TypesWhenNeeded serialization formatting to serialize types across different versions of the .NET Framework, serialization issues may occur. This behavior is by design. Therefore, we do not recommend that you use the
FormatterTypeStyle.XsdString serialization formatting and the
FormatterTypeStyle.TypesWhenNeeded serialization formatting when you serialize types or deserialize types across different versions of the .NET Framework.
Steps to reproduce the problem
- In Visual Studio 2005, click the File menu, click New, and then click Project.
- Click Visual C#, click Console Application, type Program1 in the Name box, and then click OK.
- Replace the code in the Program1.cs file by using the following code:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Globalization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
namespace Program1
{
class Program
{
static void Main(string[] args)
{
if (args[0].ToLower() == "-s")
{
FileStream fs = File.OpenWrite(args[1]);
BinaryFormatter fmt = new BinaryFormatter();
fmt.TypeFormat = FormatterTypeStyle.XsdString;
fmt.Serialize(fs, new DateTimeFormatInfo());
fs.Close();
}
else if (args[0].ToLower() == "-d")
{
FileStream fs = File.OpenRead(args[1]);
BinaryFormatter fmt = new BinaryFormatter();
fmt.TypeFormat = FormatterTypeStyle.XsdString;
DateTimeFormatInfo dtfi = (DateTimeFormatInfo)fmt.Deserialize(fs);
fs.Close();
}
}
}
}
- Click the Build menu, and then click Rebuild Solution.
- On a computer that is running the Microsoft .NET Framework 2.0 SP1, browse to the directory that contains the Program1.exe file.
- Create a new empty file that is named ser.bin.
- Click Start, click Run, type cmd, and then click OK.
- Move to the directory that contains the Program1.exe file.
- Type Program1 -s ser.bin, and then press ENTER.
- On a computer that is running a pre-SP1 version of the .NET Framework 2.0, copy the ser.bin file that you created in step 7 to a directory that contains the Program1.exe file.
- Click Start, click Run, type cmd, and then click OK.
- Move to the directory that contains the Program1.exe file.
- Type Program1 -d ser.bin, and then press ENTER.
APPLIES TO
- Microsoft .NET Framework 2.0
- Microsoft .NET Framework 2.0 x64 Edition
- Microsoft .NET Framework 2.0 IA64 Edition
| kbtshoot kbpubtypekc kbprb KB947983 |
Community Feedback System
Very often, it takes hours to solve a problem. Very often, you've looked high
and low, and have tried a lot of solutions. When you finally found it, chances
are, it was because someone else helped you. Here's your chance to give back.
Use our community feedback tool to let others know what worked for you and what
didn't.
Please also understand that the community feedback system is not warranted to be
correct, it's simply a system that we've built to let people try and help each
other. If something in a feedback response doesn't make sense to you, or you're
not comfortable making changes that the feedback talks about (like registry
edits), please consult a professional.
Thank you for using kbAlertz.com Feedback System.
-- Scott Cate