tags:

views:

195

answers:

3

Whats the best way handle exception from a WCF service? How can you throw the exception from a WCF service?

+2  A: 

Using faultcontracts.

http://msdn.microsoft.com/en-us/library/ms752208.aspx

or

Jean-Paul Smits blogg

Richard L
+2  A: 

FaultContract is the way to go. MSDN link given in the other response is a good place to look. One thing to note however, is:

Resist the temptation to put Exception-derived classes in your fault contrant

Don't do FaultContract<ArgumentException>

Rather create FaultContract<NameCanNotHaveDigitsFault>

where NameCanNotHaveDigitsFault is your domain specific class, not tied to particular framework.

Krzysztof Koźmic
A: 

You can throw FaultExceptions from a wcf service, they will be transferred back to the client.

Any other type of exception will put your communication channel into faulted mode, which will render it unusable.

So the easiest (quick & dirty) way to throw exceptions in a wcf service is to wrap/change them into FaultExceptions.

Sam