<p>We're currently having a debate whether it's better to throw faults over a WCF channel, versus passing a message indicating the status or the response from a service.</p>

<p>Faults come with built-in support from WCF where by you can use the built-in error handlers and react accordingly. This, however, carries overhead as throwing exceptions in .NET can be quite costly.</p>

<p>Messages can contain the necessary information to determine what happened with your service call without the overhead of throwing an exception. It does however need several lines of repetitive code to analyze the message and determine actions following its contents.</p>

<p>We took a stab at creating a generic message object we could utilize in our services, and this is what we came up with:</p>

<pre><code>public class ReturnItemDTO&lt;T&gt;
{
    [DataMember]
    public bool Success { get; set; }

    [DataMember]
    public string ErrorMessage { get; set; }

    [DataMember]
    public T Item { get; set; }
}
</code></pre>

<p>If all my service calls return this item, I can consistently check the "Success" property to determine if all went well. I then have an error message string in the event indicating something went wrong, and a generic item containing a Dto if needed.</p>

<p>The exception information will have to be logged away to a central logging service and not passed back from the service.</p>

<p>Thoughts? Comments? Ideas? Suggestions?</p>

<p><strong>Some further clarification on my question</strong></p>

<p>An issue I'm having with fault contracts is communicating business rules.</p>

<p>Like, if someone logs in, and their account is locked, how do I communicate that? Their login obviously fails, but it fails due to the reason "Account Locked".</p>

<p>So do I:</p>

<p>A) use a boolean, throw Fault with message account locked</p>

<p>B) return AuthenticatedDTO with relevant information</p>

[{'answer_id': 81339, 'author': 'Paul van Brenk', 'author_id': 1837197, 'author_profile': 'https://Stackoverflow.com/users/1837197', 'pm_score': 6, 'selected': True, 'text': '<blockquote>\n  <p>This however carries overhead as throwing exceptions in .NET can be quite costly.</p>\n</blockquote>\n\n<p>You\'re serializing and de-serializing objects to XML and sending them over a slow network.. the overhead from throwing an exception is negligable compared to that.</p>\n\n<p>I usually stick to throwing exceptions, since they clearly communicate something went wrong and <em>all</em> webservice toolkits have a good way of handling them.</p>\n\n<p>In your sample I would throw an UnauthorizedAccessException with the message "Account Locked".</p>\n\n<p><em>Clarification:</em> The .NET wcf services translate exceptions to FaultContracts by default, but you can change this behaviour. <a href="http://msdn.microsoft.com/en-us/library/ms733721.aspx" rel="noreferrer">MSDN:Specifying and Handling Faults in Contracts and Services</a></p>\n'}, {'answer_id': 81374, 'author': 'ZombieSheep', 'author_id': 377, 'author_profile': 'https://Stackoverflow.com/users/377', 'pm_score': 0, 'selected': False, 'text': "<p>I would seriously consider using the FaultContract and FaultException objects to get around this.  This will allow you to pass meaningful error messages back to the client, but only when a fault condition occurs.</p>\n\n<p>Unfortunately, I'm in a training course at the moment, so can't write up a full answer, but as luck would have it I'm learning about exception management in WCF applications.  I'll post back tonight with more information.  (Sorry it's a feeble answer)</p>\n"}, {'answer_id': 12576782, 'author': 'Despertar', 'author_id': 1160036, 'author_profile': 'https://Stackoverflow.com/users/1160036', 'pm_score': 3, 'selected': False, 'text': "<p>If you think about calling the service like calling any other method, it may help put things into perspective. Imagine if every method you called returned a status, and you it was up to you to check whether it was true or false. It would get quite tedious. </p>\n\n<pre><code>result = CallMethod();\nif (!result.Success) handleError();\n\nresult = CallAnotherMethod();\nif (!result.Success) handleError();\n\nresult = NotAgain();\nif (!result.Success) handleError();\n</code></pre>\n\n<p>This is one of the strong points of a structured error handling system, is that you can separate your actual logic from your error handling. You don't have to keep checking, you know it was a success if no exception was thrown. </p>\n\n<pre><code>try \n{\n    CallMethod();\n    CallAnotherMethod();\n    NotAgain();\n}\ncatch (Exception e)\n{\n    handleError();\n}\n</code></pre>\n\n<p>At the same time, by returning a result you're putting more responsibility on the client. You may well know to check for errors in the result object, but John Doe comes in and just starts calling away to your service, oblivious that anything is wrong because an exception is not thrown. This is another great strength of exceptions is that they give us a good slap in the face when something is wrong and needs to be taken care of.</p>\n"}]
['https://Stackoverflow.com/questions/81306', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/15360/']