The Pademelon The Pademelon - 1 month ago 7
C# Question

NetDispatcherFaultException: ValueType 'System.Decimal' Cannot Be Null

I'm trying to make a WCF call but I keep getting this error when the call returns to the client. I've tried setting all the decimal values to not-null values like so:

[FaultContract(typeof(AdaptFaultDetail))]
[FaultContract(typeof(BLFaultDetail))]
public IRecord SaveRecord(IRecord theRecord, string transactionId, bool reloadRecordAfterSave)
{
var retVal = GeneralServiceCalls.SaveRecord(theRecord, transactionId, reloadRecordAfterSave);

foreach (var property in retVal.GetType().GetProperties())
{
if (property.PropertyType == typeof(decimal) && property.GetValue(retVal) == null)
{
property.SetValue(retVal, 0);
}
}

return retVal;
}


But I still get this error on the client side:


The formatter threw an exception while trying to deserialize the message: There was an error while trying to deserialize parameter http://tempuri.org/:SaveRecordResult. The InnerException message was 'ValueType 'System.Decimal' cannot be null.'. Please see InnerException for more details.


Inner Exception:


System.Runtime.Serialization.SerializationException: ValueType 'System.Decimal' cannot be null.
at ReadAssetCustomFromXml(XmlReaderDelegator , XmlObjectSerializerReadContext , XmlDictionaryString[] , XmlDictionaryString[] )


I don't know what's causing the error.

Answer

This exception was caused by the serializer of the message not having the same dll as the deserializer.

In this case, the class was outdated and as a result, the sender (serializer) serialized an object that didn't have the decimal field on it that the receiver (deserializer) was expecting and the exception was thrown.

I'm unsure as to why the WCF connection was even allowed to occur but I guess that's just how WCF works.