Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>NicklasJepsen, you were close to the right answer. I've just had the same problem with IEnumerable, but managed to solve it, not changing the return type.</p> <p>In my case the problem reason was that the method was using the yield return statement. As you probably know, the method that returns IEnumerable and uses yield return to return the elements, is compiled into a "state machine". In fact it returns the IEnumerable instance, which returns the iterator, which methods implement that state machine. This great feature lies in the core of LINQ, and it allows deferred execution - the state machine works only when you need another element.</p> <p>In case of WCF, I guess I know what happens. WCF acts kinda like this:</p> <ol> <li><p>It calls the method.</p></li> <li><p>If the method fails, WCF passes the exception to your ErrorHandler methods (both of them).</p></li> <li><p>If the method succeeds, WCF prepares the response. It serializes the object returned by the method in accordance with the response format.</p></li> <li><p>If serialization fails (what is less expected than the method failure), WCF passes the exception to your ErrorHandler, but only to method HandleError. For some reason ProvideFault is not called at this stage.</p></li> <li><p>If serialization succeeds, the serialized object is sent to the client.</p></li> </ol> <p>Now consider these 2 examples:</p> <pre><code>public bool DoStuff(); public IEnumerable&lt;bool&gt; DoALotOfStuff(); </code></pre> <p>DoStuff certainly fails at step 2. But DoALotOfStuff does not! Remember, it's a state machine, which execution is deferred till the moment you query the first element. So, the method does not fail, it only returns the IEnumerable instance, which is ready to give you the enumerator once you need the elements. Consequently, WCF is happy with what is returned by the method, so it proceeds to serialization. And that's where it gets your insidious exception!</p> <p>Now, the solution is quite simple - just do not defer execution. Return a "real" enumerable instance like List instead of the "state machine".</p> <pre><code>public IEnumerable&lt;bool&gt; DoALotOfStuff() { return _DoALotOfStuff().ToList(); } protected IEnumerable&lt;bool&gt; _DoALotOfStuff() { yield return true; yield return false; throw new Exception( "Testing..." ); } </code></pre> <p>Now if the method fails, it fails in the right place, so you have your ProvideFault called.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload