OracleException has no public constructors nor any way to get a new instance. I tried my XmlSerializerHelper class, but it requires a public parameterless constructor.
I used BinaryFormatter to serialize the OracleException and wrote it to a file.
How can I serialize OracleException in a file, and deserialize too using XmlSerializer -for testing reasons-?.
Reference: http://geekswithblogs.net/WillSmith/archive/2008/07/25/testing-oracleexception.aspx
PD: Is better SoapFormatter or BinaryFormatter ?
Code
SerializationHelper.Serialize(@"C:\Temp\ExcepcionOracle.bin", ex);
var exOra = SerializationHelper.Deserialize(@"C:\Temp\ExcepcionOracle.bin");
public static void Serialize(string fileName, Object obj)
{
var binaryFormatter = new BinaryFormatter();
var fileStream = new FileStream(fileName, FileMode.Create);
try
{
binaryFormatter.Serialize(fileStream, obj);
}
catch (SerializationException ex)
{
throw new ApplicationException("The object graph could not be serialized", ex);
}
finally
{
fileStream.Close();
}
}
public static object Deserialize(string fileName)
{
var binaryFormatter = new BinaryFormatter();
var fileStream = new FileStream(fileName, FileMode.Open);
try
{
fileStream.Seek(0, SeekOrigin.Begin);
return binaryFormatter.Deserialize(fileStream);
}
catch (SerializationException ex)
{
throw new ApplicationException("Serialization Exception: " + ex.Message);
}
finally
{
fileStream.Close();
}
return null;
}
things like
Exceptionsimply aren't very suitable for xml serializers (andXmlSerializerin particular). In addition to the constructor issues (which some serializers can work around, and some can't), you are also likely to get issues with unexpected subclasses and arbitrary data in the collection.If you are serializing as xml, you should probably just capture the key information you need - maybe the
.Messageand a few other things. Note also that in a client/server application the client doesn't really need to know much of the particulars of the failure - that should remain at the server. Either it is an exected error (invalid parameters, login issues, quota restrictions, etc), or it is an unexpected error. In the latter case: just say an unexpected error happened. The details would only be useful to a developer, and a developer should already have access to the server's error log.