1

我正在使用一些改编自 Lowy 的 ServiceModelEx 的代码将异常打包到FaultException.NET 中,通过网络发送,然后在客户端将其解包为 .NET Exception。我的服务器理论上可以抛出任何类型的异常,但我们以 aSqlException为例。

Lowy使用反射来解包,所以代码需要检查异常的类型来判断是否可以构造一个有效的对象:

    static Exception ExtractException(ExceptionDetail detail)
    {
        Exception innerException = null;
        if (detail.InnerException != null)
        {
            innerException = ExtractException(detail.InnerException);
        }
        Type type = Type.GetType(detail.Type);
        Debug.Assert(type != null, "Make sure this assembly contains the definition of the custom exception");
        Debug.Assert(type.IsSubclassOf(typeof(Exception)));

        //...
    }

两个问题

  1. 使用 SqlExceptions,第一个 Debug.Assert 失败。为了让他们在客户端上解决,我设置了对 的引用System.Data,但没有运气。我假设这是因为System.Data在该客户端没有任何实际调用的情况下,编译器会删除引用?在短期内,我可以做些什么来让我的客户解决这个.GetType电话?

  2. 在不丢失细节粒度(例如)并且不需要客户端的任何程序集引用的情况下“重新抛出”LambdaException作为基础的正确方法是什么?这甚至可能吗?ExceptionDetailStackTrace

编辑:这是在服务器上包装ExceptionDetail为 a的源。FaultException同样,这主要来自 Lowy 的 ServiceModelEx:

    public static void PromoteException(Type serviceType, Exception error, MessageVersion version, ref Message fault)
    {
        //Is error in the form of FaultException<T> ? 
        if (error.GetType().IsGenericType && error is FaultException)
        {
            Debug.Assert(error.GetType().GetGenericTypeDefinition() == typeof(FaultException<>));
            return;
        }

        ExceptionDetail exdetail = new ExceptionDetail(error);
        FaultException<ExceptionDetail> faultException = new FaultException<ExceptionDetail>(exdetail);

        MessageFault messageFault = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version, messageFault, faultException.Action);

        //...
    }
4

0 回答 0