By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,496 Members | 1,517 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,496 IT Pros & Developers. It's quick & easy.

Exceptions not serializable due to new data property?

P: n/a
When attempting to debug a webservice, I get the error:

Cannot serialize member System.Exception.Data of type
System.Collections.IDictionary, because it implements IDictionary.

In reading about web services
(http://msdn2.microsoft.com/en-us/library/ds492xtk.aspx), the documentation
states that exceptions will automatically be handled as a fault element in
the message and then converted to a SoapException in the client.

Additionally the Exception class implements the ISerializable interface
which should be handling the fact that its data member implements the
IDictionary interface.

I pass a collection of data transfer objects to the webservice. Each DTO
has a persistenceException property which will be populated if there is a
problem persisting the individual DTO. This way, the service is not halted
due to an error with a single DTO. DTO's which are updated during
persistence or which experienced an exception are returned to the client in a
collection.

Am I missing something here? Shouldn't exceptions be serializable since
they implement ISerializable othwise, what's the point of implementing the
ISerializable interface for the Exception class? Thank you for your
assistance.

The full error trace follows:

[NotSupportedException: Cannot serialize member System.Exception.Data of
type System.Collections.IDictionary, because it implements IDictionary.]
System.Xml.Serialization.TypeScope.GetDefaultIndex er(Type type, String
memberInfo) +419
System.Xml.Serialization.TypeScope.GetCollectionEl ementType(Type type,
String memberInfo) +30
System.Xml.Serialization.TypeScope.ImportTypeDesc( Type type, MemberInfo
memberInfo, Boolean directReference) +1491
System.Xml.Serialization.TypeScope.GetTypeDesc(Typ e type, MemberInfo
source, Boolean directReference, Boolean throwOnError) +285
System.Xml.Serialization.StructModel.GetPropertyMo del(PropertyInfo
propertyInfo) +132
System.Xml.Serialization.StructModel.GetFieldModel (MemberInfo memberInfo)
+148

System.Xml.Serialization.XmlReflectionImporter.Imp ortStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a) +2331

System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType, XmlAttributes a,
Boolean repeats, Boolean openModel) +2080

[InvalidOperationException: There was an error reflecting type
'System.Exception'.]

System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType, XmlAttributes a,
Boolean repeats, Boolean openModel) +2716

System.Xml.Serialization.XmlReflectionImporter.Imp ortAccessorMapping(MemberMapping
accessor, FieldModel model, XmlAttributes a, String ns, Type
choiceIdentifierType, Boolean rpc, Boolean openModel) +18454

System.Xml.Serialization.XmlReflectionImporter.Imp ortFieldMapping(StructModel
parent, FieldModel model, XmlAttributes a, String ns) +292

System.Xml.Serialization.XmlReflectionImporter.Imp ortStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a) +2454

[InvalidOperationException: There was an error reflecting property
'persistenceException'.]

System.Xml.Serialization.XmlReflectionImporter.Imp ortStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a) +3961

System.Xml.Serialization.XmlReflectionImporter.Imp ortStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a) +1569

System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType, XmlAttributes a,
Boolean repeats, Boolean openModel) +2080

[InvalidOperationException: There was an error reflecting type
'com.hsb.peoplesoftservices.CustomerDTO'.]

System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType, XmlAttributes a,
Boolean repeats, Boolean openModel) +2716

System.Xml.Serialization.XmlReflectionImporter.Imp ortTypeMapping(TypeModel
model, String ns, ImportContext context, String dataType, XmlAttributes a) +49

System.Xml.Serialization.XmlReflectionImporter.Cre ateArrayElementsFromAttributes(ArrayMapping
arrayMapping, XmlArrayItemAttributes attributes, Type arrayElementType,
String arrayElementNs) +471

System.Xml.Serialization.XmlReflectionImporter.Imp ortArrayLikeMapping(ArrayModel model, String ns) +381

System.Xml.Serialization.XmlReflectionImporter.Imp ortAccessorMapping(MemberMapping
accessor, FieldModel model, XmlAttributes a, String ns, Type
choiceIdentifierType, Boolean rpc, Boolean openModel) +10751

System.Xml.Serialization.XmlReflectionImporter.Imp ortMemberMapping(XmlReflectionMember
xmlReflectionMember, String ns, XmlReflectionMember[] xmlReflectionMembers,
Boolean rpc, Boolean openModel) +1710

System.Xml.Serialization.XmlReflectionImporter.Imp ortMembersMapping(XmlReflectionMember[]
xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc,
Boolean openModel) +291

[InvalidOperationException: There was an error reflecting 'Customers'.]

System.Xml.Serialization.XmlReflectionImporter.Imp ortMembersMapping(XmlReflectionMember[]
xmlReflectionMembers, String ns, Boolean hasWrapperElement, Boolean rpc,
Boolean openModel) +1042

System.Xml.Serialization.XmlReflectionImporter.Imp ortMembersMapping(String
elementName, String ns, XmlReflectionMember[] members, Boolean
hasWrapperElement, Boolean rpc, Boolean openModel, XmlMappingAccess access)
+183

System.Web.Services.Protocols.SoapReflector.Import MembersMapping(XmlReflectionImporter
xmlImporter, SoapReflectionImporter soapImporter, Boolean
serviceDefaultIsEncoded, Boolean rpc, SoapBindingUse use, SoapParameterStyle
paramStyle, String elementName, String elementNamespace, Boolean nsIsDefault,
XmlReflectionMember[] members, Boolean validate, Boolean openModel, String
key, Boolean writeAccess) +765

System.Web.Services.Protocols.SoapReflector.Reflec tMethod(LogicalMethodInfo
methodInfo, Boolean client, XmlReflectionImporter xmlImporter,
SoapReflectionImporter soapImporter, String defaultNs) +4263

[InvalidOperationException: Method CustomerService.createOrUpdateCustomers
can not be reflected.]

System.Web.Services.Protocols.SoapReflector.Reflec tMethod(LogicalMethodInfo
methodInfo, Boolean client, XmlReflectionImporter xmlImporter,
SoapReflectionImporter soapImporter, String defaultNs) +10259
System.Web.Services.Description.SoapProtocolReflec tor.ReflectMethod() +149

System.Web.Services.Description.ProtocolReflector. ReflectBinding(ReflectedBinding reflectedBinding) +1980
System.Web.Services.Description.ProtocolReflector. Reflect() +734

System.Web.Services.Description.ServiceDescription Reflector.ReflectInternal(ProtocolReflector[] reflectors) +501
System.Web.Services.Description.ServiceDescription Reflector.Reflect(Type
type, String url) +165
System.Web.Services.Protocols.DocumentationServerT ype..ctor(Type type,
String uri) +213
System.Web.Services.Protocols.DocumentationServerP rotocol.Initialize() +376
System.Web.Services.Protocols.ServerProtocol.SetCo ntext(Type type,
HttpContext context, HttpRequest request, HttpResponse response) +70
System.Web.Services.Protocols.ServerProtocolFactor y.Create(Type type,
HttpContext context, HttpRequest request, HttpResponse response, Boolean&
abortProcessing) +104

[InvalidOperationException: Unable to handle request.]
System.Web.Services.Protocols.ServerProtocolFactor y.Create(Type type,
HttpContext context, HttpRequest request, HttpResponse response, Boolean&
abortProcessing) +303

System.Web.Services.Protocols.WebServiceHandlerFac tory.CoreGetHandler(Type
type, HttpContext context, HttpRequest request, HttpResponse response) +169

[InvalidOperationException: Failed to handle request.]

System.Web.Services.Protocols.WebServiceHandlerFac tory.CoreGetHandler(Type
type, HttpContext context, HttpRequest request, HttpResponse response) +345

System.Web.Services.Protocols.WebServiceHandlerFac tory.GetHandler(HttpContext
context, String verb, String url, String filePath) +150
System.Web.HttpApplication.MapHttpHandler(HttpCont ext context, String
requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig)
+401

System.Web.MapHandlerExecutionStep.System.Web.Http Application.IExecutionStep.Execute() +183
System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean&
completedSynchronously) +191


Jun 14 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi Rcollette1,

Thank you for posting in the webservice newsgroup.

From your description, I understand that you're developing an ASP.NET 2.0
webservice, and in the webservice's exception handling code, you'll store
some custom data in the exception object through the "Data" property.
However, at runtime, you are encountering the following exception:

========
Cannot serialize member System.Exception.Data of type
System.Collections.IDictionary, because it implements IDictionary.
========

when the exception handilng code through a exception object contains some
of your custom class objects (in the Data property), correct? If there is
anything I missed ,please feel free to let me know.

As for the ASP.NET Webservice , it is based on XML SOAP message, that means
all the objects/data transfered through webservie will be serialized as XML
stream and contained in the SOAP message. Therefore, the runtime
serialization error you encountered is a XML serialization error and it
indicate that the IDictionary object doesn't support XMLSerialization. And
the ISerializable interface is used for supporting binary serialization
rather than XML serialization. So in webservice, if the class object
(which will be transfered between cilent proxy and server service )
contains any sub object that can not be serializecd, the runtime will
report such serialization errorl.

For your scenario, you want to pass some custom data in the exception
object, would you consider define some other custom class (rather than the
IDictionary class) to hold them? We can use some XML serialization
attributes to control the XML serialization behavior of our custom class:

#XML and SOAP Serialization
http://msdn2.microsoft.com/en-us/library/90c86ass.aspx

Also, .net framework 2.0 provide the "IXmlSerializable" public interface to
let us completely define our own xml serialization code logic of our custom
class:

#IXmlSerializable Interface
http://msdn2.microsoft.com/en-us/lib...ion.ixmlserial
izable.aspx

In addition, for throwing exception to webservice client proxy with some
additional custom data, we recommend you consider manually constructnig a
SoapException and use its "Detail" property(a xmlNode) to hold any
additional data as XML based content as mentioned in the article you
mentioned (http://msdn2.microsoft.com/en-us/library/ds492xtk.aspx).

Just some of my understanding and suggestion. Hope this helps. If there is
anything else we can help, please feel free to post here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)





Jun 15 '06 #2

P: n/a
Thank you for your response.

You suggested throwing a custom soap exception, however, I do not want to
throw an exception but rather return the exception that was experienced while
processing an individual DTO.

It is a common design pattern to have web services process collections of
objects as a means of reducing network communication/overhead. I do not
want the service to stop processing the collection when an exception is
experienced with an individual item. I do want to let the client know that
an exception occurred while processing a particular item in the collection.
Ideally, I should be able to return the exception to the client as a property
of the DTO that experienced the problem, regardless of whether it is a web
service client or a native client.

The restriction of not being able to serialize an exception as XML causes me
to have to dumb down my DTO for web services, returning some other form of
error description besides an exception. This forces developers to use some a
home grown error handling system rather than relying on standard exceptions.
I cannot return a SoapException as a property of my DTO because
SoapException has Exception as an ancestor and therefore also implements the
IDictionary interface.

Additionally I do not want to have to maintain two code bases, one for web
services and one for native services. So now, If I call this code using a
native client, I will not be able to get a standard exception as a property
of the DTO that I passed in.

Microsoft should have figured out how to serialized the IDictionary
interface or not put the data property into the exception class. They
could have left the exception class without the data property and added it to
a subclass or subclasses so that there was at least some opportunity to
serialize a standard exception.
"Steven Cheng[MSFT]" wrote:
Hi Rcollette1,

Thank you for posting in the webservice newsgroup.

From your description, I understand that you're developing an ASP.NET 2.0
webservice, and in the webservice's exception handling code, you'll store
some custom data in the exception object through the "Data" property.
However, at runtime, you are encountering the following exception:

========
Cannot serialize member System.Exception.Data of type
System.Collections.IDictionary, because it implements IDictionary.
========

when the exception handilng code through a exception object contains some
of your custom class objects (in the Data property), correct? If there is
anything I missed ,please feel free to let me know.

As for the ASP.NET Webservice , it is based on XML SOAP message, that means
all the objects/data transfered through webservie will be serialized as XML
stream and contained in the SOAP message. Therefore, the runtime
serialization error you encountered is a XML serialization error and it
indicate that the IDictionary object doesn't support XMLSerialization. And
the ISerializable interface is used for supporting binary serialization
rather than XML serialization. So in webservice, if the class object
(which will be transfered between cilent proxy and server service )
contains any sub object that can not be serializecd, the runtime will
report such serialization errorl.

For your scenario, you want to pass some custom data in the exception
object, would you consider define some other custom class (rather than the
IDictionary class) to hold them? We can use some XML serialization
attributes to control the XML serialization behavior of our custom class:

#XML and SOAP Serialization
http://msdn2.microsoft.com/en-us/library/90c86ass.aspx

Also, .net framework 2.0 provide the "IXmlSerializable" public interface to
let us completely define our own xml serialization code logic of our custom
class:

#IXmlSerializable Interface
http://msdn2.microsoft.com/en-us/lib...ion.ixmlserial
izable.aspx

In addition, for throwing exception to webservice client proxy with some
additional custom data, we recommend you consider manually constructnig a
SoapException and use its "Detail" property(a xmlNode) to hold any
additional data as XML based content as mentioned in the article you
mentioned (http://msdn2.microsoft.com/en-us/library/ds492xtk.aspx).

Just some of my understanding and suggestion. Hope this helps. If there is
anything else we can help, please feel free to post here.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)






Jun 15 '06 #3

P: n/a
Hi Rcollette1,

Thanks for your detailed response and let me know your concerns and
consideration here.

Yes, I can fully your concerns here as you want to make your exception
management code be consistent among both winform and webservice
application. However, currently the exception classes in .net framework are
still limited to applications which use will always access these class
instances in memory or through binary serialization. Also, I know that
you won't prefer creating additional class to do the exception information
representation, but I would still post the following sugestion on creating
a custom helper class which defining both binary and XML serialization
capabiilty so that it can help encapsulate exception information in both
scenario(with the consistent code logic).

My suggestion is defining a custom class to wrapper the exception class
object:

======================================
[Serializable]
[XmlRoot("ExceptionInfo")]
public class ExceptionVO
{

private Exception _exception;
public ExceptionVO()
{

}

public ExceptionVO(Exception ex)
{
_exception = ex;
}

[XmlElement("ExceptionTypeName")]
public string ExceptionType
{
get { return _exception.GetType().AssemblyQualifiedName; }
set{}
}
[XmlElement("Message")]
public string Message
{
get { return _exception.Message; }
set{}
}

[XmlElement("Source")]
public string Source
{
get { return _exception.Source; }
set{}
}

[XmlElement("StackTrace")]
public string StackTrace
{
get { return _exception.StackTrace; }
set{}
}

[XmlElement("HelpLink")]
public string HelpLink
{
get { return _exception.HelpLink; }
set{}
}
[XmlIgnore()]
public Exception OriginalException
{
get { return _exception; }
set { _exception = value; }
}

}
==================================

In the above wrapper class, I define many public properties which expose
the actual exception object's certain properties( those ones that can be
xml seriaizable). Also, the actual exception can be filled in the
constructor. And one thing important is that I define the following
property in this class:

[XmlIgnore()]
public Exception OriginalException
{
get { return _exception; }
set { _exception = value; }
}

this is a pubilc property, however, since I apply the "XmlIgnoreAttribute"
on it, this property won't be exposed(serialized ) in XML serialization
scenario, but still will be serialized and visible in binary serialization
scenario.

Thus, in our DTO class ,we can define the sub error property as this
wrapper class type:

==========
[Serializable]
public class DTO
{
...........

public ExceptionVO Error;

.....................
}
================

Hope this helps some.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Jun 16 '06 #4

P: n/a
Steven,

Thank you for the code. I had written something similar yesterday, but I
had been storing all those properties in member variables. I like your
solution better, keeping only the exception as the wrapper class member and
returning its properties through wrapper properties.

Thanks,
Rich

"Steven Cheng[MSFT]" wrote:
Hi Rcollette1,

Thanks for your detailed response and let me know your concerns and
consideration here.

Yes, I can fully your concerns here as you want to make your exception
management code be consistent among both winform and webservice
application. However, currently the exception classes in .net framework are
still limited to applications which use will always access these class
instances in memory or through binary serialization. Also, I know that
you won't prefer creating additional class to do the exception information
representation, but I would still post the following sugestion on creating
a custom helper class which defining both binary and XML serialization
capabiilty so that it can help encapsulate exception information in both
scenario(with the consistent code logic).

My suggestion is defining a custom class to wrapper the exception class
object:

======================================
[Serializable]
[XmlRoot("ExceptionInfo")]
public class ExceptionVO
{

private Exception _exception;
public ExceptionVO()
{

}

public ExceptionVO(Exception ex)
{
_exception = ex;
}

[XmlElement("ExceptionTypeName")]
public string ExceptionType
{
get { return _exception.GetType().AssemblyQualifiedName; }
set{}
}
[XmlElement("Message")]
public string Message
{
get { return _exception.Message; }
set{}
}

[XmlElement("Source")]
public string Source
{
get { return _exception.Source; }
set{}
}

[XmlElement("StackTrace")]
public string StackTrace
{
get { return _exception.StackTrace; }
set{}
}

[XmlElement("HelpLink")]
public string HelpLink
{
get { return _exception.HelpLink; }
set{}
}
[XmlIgnore()]
public Exception OriginalException
{
get { return _exception; }
set { _exception = value; }
}

}
==================================

In the above wrapper class, I define many public properties which expose
the actual exception object's certain properties( those ones that can be
xml seriaizable). Also, the actual exception can be filled in the
constructor. And one thing important is that I define the following
property in this class:

[XmlIgnore()]
public Exception OriginalException
{
get { return _exception; }
set { _exception = value; }
}

this is a pubilc property, however, since I apply the "XmlIgnoreAttribute"
on it, this property won't be exposed(serialized ) in XML serialization
scenario, but still will be serialized and visible in binary serialization
scenario.

Thus, in our DTO class ,we can define the sub error property as this
wrapper class type:

==========
[Serializable]
public class DTO
{
...........

public ExceptionVO Error;

.....................
}
================

Hope this helps some.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Jun 16 '06 #5

P: n/a
Thanks for the followup Rich,

I'm glad that the suggestion helps you some. Also, if you meet any other
problem or anything we can help, please feel free to post here.

Thanks again for using Microsoft newsgroup.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
Jun 16 '06 #6

P: n/a
Hi,

I am facing a similar problem and I need some help.

We are working on an application using SOA for a client. The client has an
existing application with Framework. They asked us to log the exceptions
using their framework instead of logging it directly from our application.

For this purpose, we have written a method in our webservice that calls the
logging method from framework with the required parameters. The parameter to
be passed is of type "Exception"

When I ran the webservice, the error mentioned above has occurred. Keeping
your suggestions in view, I had written a wrapper class.

However, the wrapper class does not meet the need, as we have to identify
the type of exception and then pass the parameters accordingly. Using the
wrapper class across the applications, I am unable to get the exception type.

I am trying for alternate ways, but, also thought of posting the question
here and get some info.

Can you please help us out?

"Steven Cheng[MSFT]" wrote:
Thanks for the followup Rich,

I'm glad that the suggestion helps you some. Also, if you meet any other
problem or anything we can help, please feel free to post here.

Thanks again for using Microsoft newsgroup.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights
Jul 12 '06 #7

P: n/a
I was worried about preserving the original data thrown by the exception and
tried to find ways for that. Finally, I am able to make it by passing the
Exception as an Object type.

Thanks for the valuable information given here :-)
"Kiran" <Ki***@discussions.microsoft.comwrote in message
news:A6**********************************@microsof t.com...
Hi,

I am facing a similar problem and I need some help.

We are working on an application using SOA for a client. The client has an
existing application with Framework. They asked us to log the exceptions
using their framework instead of logging it directly from our application.

For this purpose, we have written a method in our webservice that calls
the
logging method from framework with the required parameters. The parameter
to
be passed is of type "Exception"

When I ran the webservice, the error mentioned above has occurred. Keeping
your suggestions in view, I had written a wrapper class.

However, the wrapper class does not meet the need, as we have to identify
the type of exception and then pass the parameters accordingly. Using the
wrapper class across the applications, I am unable to get the exception
type.

I am trying for alternate ways, but, also thought of posting the question
here and get some info.

Can you please help us out?

"Steven Cheng[MSFT]" wrote:
>Thanks for the followup Rich,

I'm glad that the suggestion helps you some. Also, if you meet any other
problem or anything we can help, please feel free to post here.

Thanks again for using Microsoft newsgroup.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
================================================= =

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

================================================= =
This posting is provided "AS IS" with no warranties, and confers no
rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights

Jul 13 '06 #8

P: n/a
Thanks Steven...

I created the ExceptionVo and class in a .net library (say Framework.Utils).
I am using a SIlverlight application for presentation.how will i call the
webservice for loggin the exceptions since i am not able to add the Framework
dll to SL 2.0 beta 1???

Regards,
Anand

"Steven Cheng[MSFT]" wrote:
Hi Rcollette1,

Thanks for your detailed response and let me know your concerns and
consideration here.

Yes, I can fully your concerns here as you want to make your exception
management code be consistent among both winform and webservice
application. However, currently the exception classes in .net framework are
still limited to applications which use will always access these class
instances in memory or through binary serialization. Also, I know that
you won't prefer creating additional class to do the exception information
representation, but I would still post the following sugestion on creating
a custom helper class which defining both binary and XML serialization
capabiilty so that it can help encapsulate exception information in both
scenario(with the consistent code logic).

My suggestion is defining a custom class to wrapper the exception class
object:

======================================
[Serializable]
[XmlRoot("ExceptionInfo")]
public class ExceptionVO
{

private Exception _exception;
public ExceptionVO()
{

}

public ExceptionVO(Exception ex)
{
_exception = ex;
}

[XmlElement("ExceptionTypeName")]
public string ExceptionType
{
get { return _exception.GetType().AssemblyQualifiedName; }
set{}
}
[XmlElement("Message")]
public string Message
{
get { return _exception.Message; }
set{}
}

[XmlElement("Source")]
public string Source
{
get { return _exception.Source; }
set{}
}

[XmlElement("StackTrace")]
public string StackTrace
{
get { return _exception.StackTrace; }
set{}
}

[XmlElement("HelpLink")]
public string HelpLink
{
get { return _exception.HelpLink; }
set{}
}
[XmlIgnore()]
public Exception OriginalException
{
get { return _exception; }
set { _exception = value; }
}

}
==================================

In the above wrapper class, I define many public properties which expose
the actual exception object's certain properties( those ones that can be
xml seriaizable). Also, the actual exception can be filled in the
constructor. And one thing important is that I define the following
property in this class:

[XmlIgnore()]
public Exception OriginalException
{
get { return _exception; }
set { _exception = value; }
}

this is a pubilc property, however, since I apply the "XmlIgnoreAttribute"
on it, this property won't be exposed(serialized ) in XML serialization
scenario, but still will be serialized and visible in binary serialization
scenario.

Thus, in our DTO class ,we can define the sub error property as this
wrapper class type:

==========
[Serializable]
public class DTO
{
...........

public ExceptionVO Error;

.....................
}
================

Hope this helps some.

Regards,

Steven Cheng
Microsoft MSDN Online Support Lead
==================================================

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.

==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Jun 27 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.