Microsoft Knowledge Base Email Alertz

KBAlertz.com: Binary serialization exceptions occur when you serialize or deserialize the DateTimeFormatInfo class or the TextInfo class between different versions of the .NET Framework

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








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.1

Binary serialization exceptions occur when you serialize or deserialize the DateTimeFormatInfo class or the TextInfo class between different versions of the .NET Framework

On This Page

SYMPTOMS

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 1
Exception 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 2
Exception 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.

CAUSE

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.

WORKAROUND

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.

MORE INFORMATION

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

  1. In Visual Studio 2005, click the File menu, click New, and then click Project.
  2. Click Visual C#, click Console Application, type Program1 in the Name box, and then click OK.
  3. Replace the code in the Program1.cs file by using the following code:
  4. 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();
                }
    
            }
        }
    }
    
  5. Click the Build menu, and then click Rebuild Solution.
  6. On a computer that is running the Microsoft .NET Framework 2.0 SP1, browse to the directory that contains the Program1.exe file.
  7. Create a new empty file that is named ser.bin.
  8. Click Start, click Run, type cmd, and then click OK.
  9. Move to the directory that contains the Program1.exe file.
  10. Type Program1 -s ser.bin, and then press ENTER.
  11. 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.
  12. Click Start, click Run, type cmd, and then click OK.
  13. Move to the directory that contains the Program1.exe file.
  14. 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
Keywords: 
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

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please