views:

75

answers:

2

I have a WCF service I use to submit bugs for my project. Snippet of the data class:

Private _exception As Exception
<DataMember()> _
 Public Property Exception As Exception
    Get
        Return _exception
    End Get
    Set(ByVal value As Exception)
        _exception = value
    End Set
End Property

I have a Silverlight app that uses the WCF service to send any bugs home if and when they occur. This is the error I'm testing with:

 Dim i As Integer = 5
 i = i / 0

The problem is SL is banging on with this message:

System.ServiceModel.CommunicationException was unhandled by user code Message=There was an error while trying to serialize parameter :bug. The InnerException message was 'Type 'System.OverflowException' with data contract name 'OverflowException:http://schemas.datacontract.org/2004/07/System' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.

Is there some trick to get a generic .NET Exception (any InnerException) to serialize properly? I'm not doing anything funky with the exception - it's just a plain 'ol exception

Thanks for any help.

+1  A: 

I doubt very much that you can serialize a .NET-specific type like an Exception. I recommend you create your own class to hold the parts of the exception you want serialized.

John Saunders
Thanks for the reply. MSDN says WCF can serialize a generic System.Exception...
Bram
@Bram: Link, please.
John Saunders
WCF will return a fault wrapping your System.Exception (take the Message, stick into a fault) if an exception is bubbled up into the WCF stack. This does not mean that you can "magically" serialize any exception.
Alex
@Alex: I think Bram is not trying to catch the WCF fault, but catch an exception in Silverlight and send the Exception to a logging service via WCF.
Scott P
A: 

This may be a problem with implicitly casting the OverflowException into a System.Exception.

The data contract serializer is very specific. This can be good and bad.

I would try just throwing a new System.Exception to see if this works OK.

If this is the case, you may need to dumb down the exception, creating a new System.Exception with the original exception message in it.

Or, like John said, you might have a better go of it if you create a custom error class that holds the exception info.

Scott P