Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# WCF catch Fault Exceptions of Base type

Tags:

c#

wcf

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.

like image 834
ca_allicat Avatar asked Nov 10 '10 00:11

ca_allicat


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

Is C language easy?

Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.


2 Answers

That syntax will not work in C#. Consider the folowing "workaround" instead.

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.
}

This can be simplified if we disregard the unusual case where Detail == null but the constructed generic type matches. I'll also use the C# dynamic keyword to simplify it a little further.

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. 
}

The other thing to consider is whether you should just use throw new FaultException<BaseClass1>(new DerivedClass2()). This way of throwing will let you catch using the code you originally provided.

like image 80
Jason Kresowaty Avatar answered Sep 19 '22 12:09

Jason Kresowaty


Sorry, there is no way to do this. There is no relationship between FaultException<T1> and FaultException<T2> simply because T1 may be a subclass of T2.

like image 22
John Saunders Avatar answered Sep 20 '22 12:09

John Saunders