I have been looking everywhere to find out how to catch a base fault contract type in c#. I would like to have all my fault contracts inherit from one class and have one catch(FaultException fex) in the MVC controller.
DataContracts
[DataContract]
public class BaseClass1
{ }
[DataContract]
public class Class2 : BaseClass1
{ }
Service
[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");
}
}
Service Consumer
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>
}
Please let me know if either of these catch statements can be fixed to do what I am looking for.