2

我的多层项目中有一个 ExceptionDTO 类,它有一个 LogException 方法,它需要 4 个属性,其中一个是异常。我已经制作了 DataContract 解析器来解析类的类型。

[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();
    }
}

我的问题是:如果我发送一些 SystemException 类型的异常,它会起作用,但如果我发送一个 SystemException 的子可能是 IndexOutOfRangeException,它就不起作用。那么我可以做些什么来建立这种关系。

我收到此错误消息 - 尝试序列化参数http://tempuri.org/:ex时出错。InnerException 消息是 'Type 'System.NullReferenceException' 与数据合同名称 'NullReferenceException: http://schemas.datacontract.org/2004/07/System ' 不是预期的。考虑使用 DataContractResolver 或将任何静态未知的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将它们添加到传递给 DataContractSerializer 的已知类型列表中。有关更多详细信息,请参阅 InnerException。

我发送异常的代码是

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);

    }
}
4

0 回答 0