WCF Stripping detail from System.SecurityException
<p>I am implementing IErrorHandler for a WCF service, with the intention of automatically logging the messages.</p>

<p>During testing I decided to throw a <code>System.SecurityException</code> with an inner exception and some user defined data:</p>

<pre><code>try
{
    LevelTwo();
}
catch (Exception innerEx)
{
    var outerEx = new SecurityException("Is security special?", innerEx);
    outerEx.Data.Add("foo", "bar");
    throw outerEx;
} 
</code></pre>

<p>Yet when it hits my <code>ProvideFault</code> method in my error handler it has already been converted to a <code>System.ServiceModel.FaultException</code> with a Message of "Access is denied". The original message, exception type, data and inner exception have all been lost, and I cannot log them. Other exception types work as expected, exposing all those properties for logging.</p>

<p>I presume that there is a framework implementation of <code>IErrorHandler</code> that is hit first, and converts it, stripping the detail and providing a generic message, which is all well and good generally, but not when I want to log the details.</p>

<p>Somebody <a href="https://social.msdn.microsoft.com/Forums/vstudio/en-US/a6f786f8-93a7-4f62-a233-e53a2df7110e/wcf-converts-securityexception?forum=wcf" rel="nofollow">has posted</a> the same issue on MSDN but didn't get any particularly good answers.</p>

<p><strong><em>Ideally I would like to change the order of the error handlers so that mine is hit first - is this possible?</em></strong></p>

<p>If not - is it possible to remove the existing handler, and if so, are there any undesirable consequences?</p>

<p><strong><em>EDIT</em></strong>
Following Christian's advice below it appears that the exception is getting caught by the code in <code>SyncMethodInvoker.Invoke()</code></p>

<pre><code>catch (System.Security.SecurityException e)
{
    DiagnosticUtility.TraceHandledException(e, TraceEventType.Warning);
    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError
        (AuthorizationBehavior.CreateAccessDeniedFaultException());
}
</code></pre>

<p>I think this means that Security Exceptions are a very special case, and that there probably isn't a good way around this, but I would appreciate any suggestions.</p>