D
DougBurke
I'm having a problem with a SqlException that I am serializing and
deserializing. I do not know if this is the correct place to post,
but I thought someone might know.
A particular error that occurs, if the connection string does not pass
correct login details, is a sql exception with the following message:
"Login failed for user 'dbuser'." This exception is of type
System.Data.SqlClient.SqlException. This exception has a normal
StackTrace property.
I serialize the exception, either with a binaryformatter or a
soapformatter and then deserialize and the resulting SqlException has
a StackTrace of null (I have verified this by re-serializing and
inspecting the serialized exception). The problem is in the
deserialization, since the first serialized exception (by inspection)
has the stacktrace string contained.
Has anyone seen this behaviour and is it a bug or am I missing
something? In other words, should the stack trace disappear?
Doug
As an aside, the stack trace only disappears for this specific
exception... a different sql exception (with incorrect command sytax
for example) does not behave this way.
Example code here:
-----------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace SqlExcepConsole
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
using (SqlConnection connection = new SqlConnection("user
id=user;password=;initial catalog=eRStdv99;data
source=melwks209;Connect Timeout=30;Application
Name=eRStd.WorkFlow;"))
{
// this should throw a sqlexception
connection.Open();
// break if we get here
System.Diagnostics.Debugger.Break();
}
}
catch(SqlException exThrown)
{
SerializeToFile(exThrown, @"C:\sqlexcep_before.txt");
Console.WriteLine("The stacktrace before serialisation is: {0}",
exThrown.StackTrace);
SqlException exDeserialized =
DeserializeFromFile(@"C:\sqlexcep_before.txt");
Console.WriteLine("\nThe stacktrace after serial/deserial is:
{0}", exDeserialized.StackTrace);
SerializeToFile(exDeserialized, @"C:\sqlexcep_after.txt");
}
}
private static SqlException DeserializeFromFile(string path)
{
SqlException sqlEx = null;
using(FileStream fs = new FileStream(path, FileMode.Open ) )
{
fs.Position = 0;
SoapFormatter sf = new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
sqlEx = (SqlException)sf.Deserialize(fs);
fs.Close();
} return sqlEx;
}
private static void SerializeToFile(SqlException ex, string path)
{
using ( FileStream fs = new FileStream(path, FileMode.Create ) )
{
System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf =
new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
sf.Serialize(fs, ex);
fs.Close();
}
}
}
}
------------
Output from screen:
The stacktrace before serialisation is: at
System.Data.SqlClient.ConnectionPo
ol.GetConnection(Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConn
ectionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at SqlExcepConsole.Class1.Main(String[] args) in c:\documents and
settings\bu
rked\my documents\visual studio
projects\sqlexcepconsole\class1.cs:line 19
The stacktrace after serial/deserial is:
---------------
deserializing. I do not know if this is the correct place to post,
but I thought someone might know.
A particular error that occurs, if the connection string does not pass
correct login details, is a sql exception with the following message:
"Login failed for user 'dbuser'." This exception is of type
System.Data.SqlClient.SqlException. This exception has a normal
StackTrace property.
I serialize the exception, either with a binaryformatter or a
soapformatter and then deserialize and the resulting SqlException has
a StackTrace of null (I have verified this by re-serializing and
inspecting the serialized exception). The problem is in the
deserialization, since the first serialized exception (by inspection)
has the stacktrace string contained.
Has anyone seen this behaviour and is it a bug or am I missing
something? In other words, should the stack trace disappear?
Doug
As an aside, the stack trace only disappears for this specific
exception... a different sql exception (with incorrect command sytax
for example) does not behave this way.
Example code here:
-----------------
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
namespace SqlExcepConsole
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
try
{
using (SqlConnection connection = new SqlConnection("user
id=user;password=;initial catalog=eRStdv99;data
source=melwks209;Connect Timeout=30;Application
Name=eRStd.WorkFlow;"))
{
// this should throw a sqlexception
connection.Open();
// break if we get here
System.Diagnostics.Debugger.Break();
}
}
catch(SqlException exThrown)
{
SerializeToFile(exThrown, @"C:\sqlexcep_before.txt");
Console.WriteLine("The stacktrace before serialisation is: {0}",
exThrown.StackTrace);
SqlException exDeserialized =
DeserializeFromFile(@"C:\sqlexcep_before.txt");
Console.WriteLine("\nThe stacktrace after serial/deserial is:
{0}", exDeserialized.StackTrace);
SerializeToFile(exDeserialized, @"C:\sqlexcep_after.txt");
}
}
private static SqlException DeserializeFromFile(string path)
{
SqlException sqlEx = null;
using(FileStream fs = new FileStream(path, FileMode.Open ) )
{
fs.Position = 0;
SoapFormatter sf = new
System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
sqlEx = (SqlException)sf.Deserialize(fs);
fs.Close();
} return sqlEx;
}
private static void SerializeToFile(SqlException ex, string path)
{
using ( FileStream fs = new FileStream(path, FileMode.Create ) )
{
System.Runtime.Serialization.Formatters.Soap.SoapFormatter sf =
new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
sf.Serialize(fs, ex);
fs.Close();
}
}
}
}
------------
Output from screen:
The stacktrace before serialisation is: at
System.Data.SqlClient.ConnectionPo
ol.GetConnection(Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnectionPoolManager.GetPooledConnection(SqlConn
ectionString options, Boolean& isInTransaction)
at System.Data.SqlClient.SqlConnection.Open()
at SqlExcepConsole.Class1.Main(String[] args) in c:\documents and
settings\bu
rked\my documents\visual studio
projects\sqlexcepconsole\class1.cs:line 19
The stacktrace after serial/deserial is:
---------------