Raja
Raja

Reputation: 391

Exception with WCF

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

Upvotes: 0

Views: 384

Answers (3)

Sam
Sam

Reputation: 29009

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.

Upvotes: 0

Krzysztof Kozmic
Krzysztof Kozmic

Reputation: 27384

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.

Upvotes: 1

Related Questions