wcflog4nethandleerrorierrorhandler

Log4Net & WCF IErrorHandler - ServiceSecurityContext.Current.WindowsIdentity.Name returns NullReferenceException


I am using a WCF Service and I have implemented IErrorHandler. In the HandleError method, I want to retrieve the caller's username in order to set a ThreadContext property for Log4Net (to save the username along with the exception details in SQL Server)

My code is working well if I avoid trying to retrieve the caller's username.

This is the line of code inside the HandleError method which is returning a NullReferenceException:

string username = ServiceSecurityContext.Current.WindowsIdentity.Name;

It seems that at the point the HandleError method of IErrorHandler kicks in, the original caller information has been disposed.

Does anyone have any idea how I can retrieve the caller's username inside the HandlerError method of IErrorHandler?

Thanks for your help!


Solution

  • Thanks to those who responded to help. My security appears to be configured correctly but I cannot retrieve the username in the HandleError method of the IErrorHandler interface in the WCF service. However, I am able to retrieve the username in the ProvideFault method. So, I declared a class level variable in the ErrorHandler (IErrorHandler) class which is set during the ProvideFault method and then read and logged with the exception in the ErrorHandler method. This is a "workaround" and not my preference, but unfortunately I cannot seem to access the security context inside the HandleError method.

    Here is a sample of the code:

    public class ErrorHandler : IErrorHandler, IServiceBehavior
    {
        private string username = null;
    
        public bool HandleError(Exception error)
        {
            //Log the exception along with the username.
            //...logging call including the username class member string...
    
            //Return true to indicate we have performed our behaviour.
            return true;
        }
    
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
                //Retrieve the username.
                username = ServiceSecurityContext.Current.WindowsIdentity.Name;
        }
    }