tags:

views:

1856

answers:

8

i have a wcf service that does an operation. and in this operation there could be a fault. i have stated that there could be a fault in my service contract.

here is the code below;

public void Foo()
{
        try
        {
            DoSomething(); // throws FaultException<FooFault>
        }
        catch (FaultException)
        {
            throw;
        }
        catch (Exception ex)
        {
            myProject.Exception.Throw<FooFault>(ex);
        }
}

in service contract;

[FaultException(typeof(FooFault))]
void Foo();

when a FaultException was thrown by DoSomething() method while i was running the application, firstly the exception was caught at "catch(Exception ex)" line and breaks in there. then when i pressed f5 again, it does what normally it has to. i wonder why that break exists? and if not could it be problem on publish?

A: 

Take a closer look at catched exception. Was it FaultException< FooFault> or FaultException ? There are 2 version of FaultException class: generic and non-generic

aku
maybe *you* should have a closer look ;)[SerializableAttribute]public class FaultException<TDetail> : FaultExceptionvar fooFault = new FaultException<Foo>();fooFault is FaultException;//is trueso catch block withcatch (FaultException ex)will catch FaultException<Foo>*just saw your comments above... this comment is still misleading
RhysC
A: 

yes it is FaultException< FooFault>.

but generic FaultException inherits from FaultException. so i guess, it should catch the generic one too?

yapiskan
A: 

@yapiskan,

C# is a strong typed language Foo< X> != Foo. So if you need to catch some exception, provide exact type in catch clause.

You can learn more on exception handling reading this MSDN article.

aku
A: 

but how System.Exception caught InvalidOperationException, it is normal to FaultException caught FaultException< FooFault>?

actually, it is not my exact problem. i know that "catch(Exception ex)" does not caught it. because where i call this service and the exception was thrown, the return exception is FaultException< FooFault>. it is ok!

but i wonder why a break occurs on line "catch(Exception)" and it forces me to push f5 button to continue run.

yapiskan
A: 

@yapiskan,

You are right, since generic FaultExcepton class inherits non-generic one, your exception should be catched in first catch clause. I'm not sure why it doesn't work as expected.

aku
+1  A: 

Actually your exception is caught but you fail to notice it since visual studio highlights the next line, not the line throwing the exception. Replace

throw;

with some other lines and see them in action.

Serhat Özgel
This is not working. Becuase the throw line is not in the try block so it has to be caught by the parent catch block.
yapiskan
A: 

The problem is that exceptions are checked in the order they are declared. Try putting the Exception catch block first and you will see that the compiler complains: other catch blocks will NEVER be evaluated. The following code is generally what .Net is doing in your case:

        // Begin try
             DoSomething(); // throws FaultException<FooFault>
        // End try
        if (exceptionOccured)
        {
            if(exception is FaultException) // FE catch block.
            {
                throw;
                // Goto Exit
            }
            if(exception is Exception) // EX catch block
            {
                throw new FaultException<FooFault>();
                // Goto Exit
            }
        }

        // Exit

As you can see your FaultException never re-enters the try-catch-finally (i.e. try-catch-finally is not recursive in nature).

Try this instead:

        try
        {
            try
            {
                DoSomething(); // throws FaultException<FooFault>
            }
            catch (Exception ex)
            {
                if (ex is FaultException<FooFault>)
                    throw;
                else
                    myProject.Exception.Throw<FooFault>(ex);
            }
        }
        catch (FaultException)
        {
            throw;
        }

HTH.

Jonathan C Dickinson
I knew that the catch blocks are not recursive, but using catch blocks as you mentioned is going to make the code like stairs and I don't like it. And I am not at the point of why the exception is not caught. It is caught but there exists a break in there and forces me to push f5 button to continue.
yapiskan
A: 

Are you consuming the WCF service from Silverlight? If so, a special configuration is needed to make the service return a HTTP 200 code instead of 500 in case of error. The details are here: http://msdn.microsoft.com/en-us/library/dd470096%28VS.96%29.aspx

Konamiman