c#WCF捕获基类型的故障异常

c#WCF捕获基类型的故障异常,c#,wcf,C#,Wcf,我一直在到处寻找如何在c#中捕获基本错误契约类型。我想让我的所有错误契约都从一个类继承,并且在MVC控制器中有一个catch(FaultException fex) 数据合同 [DataContract] public class BaseClass1 { } [DataContract] public class Class2 : BaseClass1 { } 服务 [ServiceContract] public interface IService1 { [Operatio

我一直在到处寻找如何在c#中捕获基本错误契约类型。我想让我的所有错误契约都从一个类继承,并且在MVC控制器中有一个catch(FaultException fex)

数据合同

[DataContract]
public class BaseClass1 
{ }

[DataContract]
public class Class2 : BaseClass1 
{ }
服务

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [FaultContract(typeof(BaseClass1))]
    [FaultContract(typeof(Class2))]  //Do I need this one?
    void ThrowClass2();
}

public class Service1 : IService1
{
    public void ThrowClass2()
    {
        throw new FaultException<Class2>(new Class2(), "Class2 Reason");
    }
}
[服务合同]
公共接口IService1
{
[经营合同]
[合同类型(基本类别1))]
[FaultContract(typeof(Class2))//我需要这个吗?
void ThrowClass2();
}
公共类服务1:IService1
{
公共void ThrowClass2()
{
抛出新的FaultException(新的Class2(),“Class2原因”);
}
}
服务消费者

FaultTestService.Service1Client client = null;
try
{
    client = new FaultTestService.Service1Client();
    client.ThrowAmpFaults("InvalidParameter", 0);
}
catch (FaultException<Namespace.BaseClass1> fex)
{
    //DOES NOT GO IN HERE AS I WOULD EXPECT   
}
catch (FaultException fex)
{
    //Other Possible Option
    MessageFault fault = fex.CreateMessageFault();  
    var fe1 = fault.GetDetail<BaseClass1>();
    //This throws a serialization exception it needs <Class2>
}
FaultTestService.Service1Client客户端=null;
尝试
{
client=newfaulttestservice.Service1Client();
客户端.ThrowAmpFaults(“InvalidParameter”,0);
}
捕获(故障异常fex)
{
//不像我预料的那样进入这里
}
捕获(故障异常fex)
{
//其他可能的选择
MessageFault=fex.CreateMessageFault();
var fe1=fault.GetDetail();
//这会引发它需要的序列化异常
}

请让我知道这些catch语句中的任何一个是否可以修复,以满足我的需要。

对不起,没有办法做到这一点。
FaultException
FaultException
之间没有关系,这仅仅是因为
T1
可能是
T2
的一个子类,语法在C中不起作用。考虑一下“工作解决办法”,


另一个要考虑的问题是,是否应该使用<代码>抛出新的故障异常(new派生类2)(< /代码>)。这种抛出方式可以让您捕获最初提供的代码。

您的客户需要。我们有一个类似的例子,服务器抛出各种异常,它们都以FaultException结束

但这种行为的原因是什么?
try
{
    throw new FaultException<DerivedClass2>(new DerivedClass2());
}
catch (FaultException fex)
{
    bool handled = false;
    Type fexType = fex.GetType();
    if (fexType.IsGenericType && fexType.GetGenericTypeDefinition() == typeof(FaultException<>))
    {
        if (typeof(BaseClass1).IsAssignableFrom(fexType.GetGenericArguments()[0]))
        {
            object detail = fexType.GetProperty("Detail").GetValue(fex, null);

            // Use detail here.

            handled = true;
        }
    }

    if (!handled)
        throw; // Don't know how to handle. Re-throw.
}
try
{
    throw new FaultException<DerivedClass2>(new DerivedClass2());
}
catch (FaultException fex)
{
    bool handled = false;
    Type fexType = fex.GetType();
    if (fexType.IsGenericType && fexType.GetGenericTypeDefinition() == typeof(FaultException<>))
    {
        object detail = ((dynamic)fex).Detail;
        if (detail is BaseClass1) // true for subclasses too!
        {
            // Use detail here.
        }

    }

    if (!handled)
        throw; // Don't know how to handle. Re-throw. 
}