locked
Binary deserialization error on decimal types RRS feed

  • Question

  • Hey Guys,

    Seeing a bit of an odd error here. I'm returning a stream over wcf service for large datasets, actually based off this post: http://blogs.msdn.com/b/webapps/archive/2012/09/06/custom-wcf-streaming.aspx.

    It's not rocket science, effectively a query for a set of records, like so:

    GLDistDTOCommon distModel = (from dist in dataContext.glDists
            orderby dist.PK
            select new GLDistDTOCommon
            {
         PK = dist.PK,
         SystemEntered = dist.SystemEntered ?? 0,
         DistAmount = dist.DistAmount ?? 0,                                                        
         TablePK = dist.TablePK,
         Extracted = dist.Extracted,
         AccountString = dist.AccountString,
         Description = dist.Description
            }).Skip(i).Take(20).SingleOrDefault();

    where GLDistDTOCommon is just a data transfer object. I then simply serialize the data and write it to a stream like this:

    IFormatter formatter = new BinaryFormatter();

    formatter.Serialize(memStream, distModel);

    Lastly, I deserialize on the client side like this:

    m = formatter.Deserialize(s) as GLDistDTOCommon;

    So, on to the actual issue. This entire process works really well until I hit very certain types of decimal types. When I hit the very first decimal type with a cent amount, for instance:

    22.0300

    I get a first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in mscorlib.dll.

    So, through a little trial and error, I determined if I change the value from a decimal to a double, it works. My initial thought was simply the encoding formats, and it somehow messing up the binary deserialization.

    Has anyone run into this? Is there an known issue with serializing decimals to binary streams that I'm just not finding?

    Thanks,

    Ryan

    Monday, October 22, 2012 1:47 PM

All replies

  • Encoding shouldn't matter in Binary serialization. Can you post the property get;set; for the property that holds that value. Might be something weird there that's causing the error. Maybe some validation that it's "money" format and it's breaking the deserialization. I know it's a reach, but I use serialization very extensively and have never ran into this before.
    Sunday, November 4, 2012 12:09 AM
  • Sure, pretty simple:

        [Serializable]   
        public class GLDistDTOCommon
        {

            public int PK { get; set; }
            public int SystemEntered { get; set; }
            public decimal DistAmount { get; set; }
            public int TablePK { get; set; }
            public string Extracted { get; set; }
            public string AccountString { get; set; }
            public string Description { get; set; }
           
        }

    I actually implemented sharpserializer from nuget and the issue went away.

    Ryan

    Monday, November 5, 2012 4:26 PM