In DataContract case can KnownType of parent class can work for its child class?

145 Views Asked by At

I have a class ExceptionDTO in my multi layered project, it has a method LogException which takes 4 properties, one of them is exception. I have made DataContract resolver for resolving the type of class.

[DataContract]
[KnownType("GetKnownTypes")]
public class ExceptionDTO
{
    [DataMember]
    public string CenterCode { get; set; }
    [DataMember]
    public string userCode { get; set; }
    [DataMember]
    public string WindowCode { get; set; }
    [DataMember]
    public Exception Exception { get; set; }
    public static Type[] GetKnownTypes()
    {
        List<Type> result = new List<Type>();
        result.Add(typeof(Exception));
        result.Add(typeof(SystemException));
        result.Add(typeof(ApplicationException));
        result.Add(typeof(ArgumentException));
        result.Add(typeof(Dictionary<string, object>));
        result.Add(typeof(IDictionary).Assembly.GetType("System.Collections.ListDictionaryInternal"));
        return result.ToArray();
    }
}

My question is: It works find if i send some exception of type SystemException and but it doesn't work if I send a child of SystemException that can be IndexOutOfRangeException. So is there any thing that I can do for creating such relationship.

I am getting this error message - There was an error while trying to serialize parameter http://tempuri.org/:ex. The InnerException message was 'Type 'System.NullReferenceException' with data contract name 'NullReferenceException:http://schemas.datacontract.org/2004/07/System' is not expected. Consider using a DataContractResolver or 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.

My code which sends exception is

ExceptionDTO exp = new ExceptionDTO { CenterCode = "30134", Exception = new NullReferenceException(), userCode = "ss", WindowCode = "ss" };
Logger.LogException(exp);

   public static class Logger
{
    public static void LogException(ExceptionDTO exception) 
    {
        LogManagerServices.LogManagerClient _objLogManager = null;
        _objLogManager = new LogManagerServices.LogManagerClient();
        _objLogManager.LogException(exception);

    }
}
0

There are 0 best solutions below