'Binary stream '111' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change
In our asp.net application we are implementing memcached caching technique. For this we are storing class of object serialized format and stored with key into memcache server.
While retriving we are using key and retrive the data and deserialize return back to actual class.
The error is not occurring in local machine, it is occurred in server in rare scnairo.
While Deserialize we face the error below,
Exception:Binary stream '111' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
Stack Trace:System.Runtime.Serialization.SerializationException: Binary stream '111' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
at TUtil.DeserializeFromStream(MemoryStream stream)
The below code for serialize & Deserialize we used in our application.
public static MemoryStream SerializeToStream(object o)
{
try
{
MemoryStream stream = new MemoryStream();
IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(stream, o);
return stream;
}
catch (Exception)
{
throw;
}
}
public static object DeserializeFromStream(MemoryStream stream)
{
try
{
IFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
stream.Position = 0;
stream.Seek(0, SeekOrigin.Begin);
object o = formatter.Deserialize(stream);
return o;
}
catch (Exception)
{
throw;
}
}
The calling funtion for above serialize and deserialize below,
public static T FetchCache<T>(string Keyname)
{
try
{
mStr = (MemoryStream)mCache.Get(Keyname);
if (mStr != null)
{
return (T)TUtil.DeserializeFromStream(mStr);
}
else { return default(T); }
}
catch
{
throw;
}
}
public static bool StoreCache<T>(string Keyname, T Obj, DateTime expirelimit)
{
try
{
mStr = TUtil.SerializeToStream(Obj);
return mCache.Store(StoreMode.Set, Keyname, mStr, expirelimit);
}
catch (Exception exp)
{
Log.WriteError("StoreCache<T>(): {0}", exp);
return false;
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
