title: Decimal byte array constructor in Binaryformatter Serialization
body: <p>I am facing a very nasty problem that I cannot identify.<br>
  I am running a very large business ASP.Net application containing many thousands of objects; It uses serialization/deserialization in memory with MemoryStream to clone the state of the application (insurance contracts) and pass it on to other modules. It worked fine for years.  Now sometimes, <strong>not systematically</strong>, in the serialization it throws the exception</p>
  
  <blockquote>
    <p>Decimal byte array constructor requires an array of length four containing valid decimal bytes.</p>
  </blockquote>
  
  <p>Running the same application with the same data, 3 times out of 5 it works.
  I enabled all the CLR exceptions, Debug - Exceptions - CLR Exception - Enabled,
  so I guess that if a wrong initialization/assignment to decimal field occurs, the program should stop. It doesn't happen.<br>
  I tried to split serialization in more elementary objects but it's very difficult, to try to identify the field causing problem.
  From the working version in production and this one I passed from .Net 3.5 to .NET 4.0 and consistent changes to the UI part, not the business part, have been made.
  Patiently I'll go through all the changes.</p>
  
  <p>It looks like old fashioned C problems when <code>char *p</code> is writing where it shouldn't, and only in the serialization process when it examines all the data the problem pops out.</p>
  
  <p>Is something like this possible in the managed environment of .Net ?
  The application is huge but I cannot see abnormal memory growths.
  What could be a way to debug and track down the problem?</p>
  
  <p>Below is part of the stacktrace</p>
  
  <pre><code>[ArgumentException: Decimal byte array constructor requires an array of length four containing valid decimal bytes.]
     System.Decimal.OnSerializing(StreamingContext ctx) +260
  
  [SerializationException: Value was either too large or too small for a Decimal.]
     System.Decimal.OnSerializing(StreamingContext ctx) +6108865
     System.Runtime.Serialization.SerializationEvents.InvokeOnSerializing(Object obj, StreamingContext context) +341
     System.Runtime.Serialization.Formatters.Binary.WriteObjectInfo.InitSerialize(Object obj, ISurrogateSelector surrogateSelector, StreamingContext context, SerObjectInfoInit serObjectInfoInit, IFormatterConverter converter, ObjectWriter objectWriter, SerializationBinder binder) +448
     System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Write(WriteObjectInfo objectInfo, NameInfo memberNameInfo, NameInfo typeNameInfo) +969
     System.Runtime.Serialization.Formatters.Binary.ObjectWriter.Serialize(Object graph, Header[] inHeaders, __BinaryWriter serWriter, Boolean fCheck) +1016
     System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph, Header[] headers, Boolean fCheck) +319
     System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize(Stream serializationStream, Object graph) +17
     Allianz.Framework.Helpers.BinaryUtilities.SerializeCompressObject(Object obj) in D:\SVN\SUV\branches\SUVKendo\DotNet\Framework\Allianz.Framework.Helpers\BinaryUtilities.cs:98
     Allianz.Framework.Session.State.BusinessLayer.BLState.SaveNewState(State state) in 
  </code></pre>
  
  <p>Sorry for the long story and the undetermined question, I'll really appreciate any help.</p>