jonorossi
asked on
Cannot serialize member System.Exception.Data
Hello
I am writing a web service and am one of the objects I am trying to send is an exception object. Why can't I do this, do i have to pull out the properties and dump them into string or use tostring? I am using .NET 2.0. Here is the error that the web service emits:
-------------------------- ---------- --------
Cannot serialize member System.Exception.Data of type System.Collections.IDictio nary, because it implements IDictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedExcepti on: Cannot serialize member System.Exception.Data of type System.Collections.IDictio nary, because it implements IDictionary.
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
...
-------------------------- ---------- --------
I am writing a web service and am one of the objects I am trying to send is an exception object. Why can't I do this, do i have to pull out the properties and dump them into string or use tostring? I am using .NET 2.0. Here is the error that the web service emits:
--------------------------
Cannot serialize member System.Exception.Data of type System.Collections.IDictio
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NotSupportedExcepti
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
...
--------------------------
ASKER
I might be a better idea for me to just make some string and ints to store the properties because another problem I would encounter is that if a custom exception is thrown the web service wont have that class available to deserialize. Let me know if you think this is the best solution.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I just raised (not thrown) a DivideByZeroException to see what the Data property contains and it is empty. What data is usually in there?
ASKER
I had a look in the object browser and it says this:
-------------------------- ---------- --
public virtual System.Collections.IDictio nary Data { get; }
Member of System.Exception
Summary:
Gets a collection of key/value pairs that provide additional, user-defined information about the exception.
Returns:
An object that implements the System.Collections.IDictio nary interface and contains a collection of user-defined key/value pairs. The default is an empty collection.
-------------------------- ---------- --
If Data is user defined how do you put information in here?
--------------------------
public virtual System.Collections.IDictio
Member of System.Exception
Summary:
Gets a collection of key/value pairs that provide additional, user-defined information about the exception.
Returns:
An object that implements the System.Collections.IDictio
--------------------------
If Data is user defined how do you put information in here?
Data property contains extra information about the exception, you can put here variables names and values, datatables and other significant info. You put data in it this way:
try
{
Do something ;
}
catch (Exception e)
{
e.Data["method Name"] = "Method";
e.Data[HeaderRow"] = table.Rows[0];
throw e;
}
}
The exception could be handled at another level. If you don't rethrow it, it doesn't make much sense to add entries to Data collection.
See
http://msdn2.microsoft.com/en-us/library/system.exception.data.aspx
try
{
Do something ;
}
catch (Exception e)
{
e.Data["method Name"] = "Method";
e.Data[HeaderRow"] = table.Rows[0];
throw e;
}
}
The exception could be handled at another level. If you don't rethrow it, it doesn't make much sense to add entries to Data collection.
See
http://msdn2.microsoft.com/en-us/library/system.exception.data.aspx
ASKER
Thats makes sense now, thanks for that. You wouldn't put the method name as a data key because can access the method from the Exception but it was just to demostrate how you can use it.
Thanks for your assistance, Jono
Thanks for your assistance, Jono
Hi,
This is the way XmlSerialization is implemented, it won't serialize objects that implements IDictionary interface. You can define a custom serialization, here is a way to do that:
http://msdn.microsoft.com/msdnmag/issues/03/06/XMLFiles/default.aspx (look for IDictionary)