//Runs impersonated.
        protected override IAsyncResult OnBeginWriteMessage(XmlDictionaryWriter writer, AsyncCallback callback, object state)
        {
            ImpersonateOnSerializingReplyMessageProperty impersonationProperty = null;
            IDisposable impersonationContext = null;
            IPrincipal  originalPrincipal    = null;
            bool        isThreadPrincipalSet = false;

            if (!ImpersonateOnSerializingReplyMessageProperty.TryGet(this.innerMessage, out impersonationProperty))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.UnableToImpersonateWhileSerializingReponse)));
            }

            try
            {
                impersonationProperty.StartImpersonation(out impersonationContext, out originalPrincipal, out isThreadPrincipalSet);
                return(this.innerMessage.BeginWriteMessage(writer, callback, state));
            }
            finally
            {
                try
                {
                    impersonationProperty.StopImpersonation(impersonationContext, originalPrincipal, isThreadPrincipalSet);
                }
#pragma warning suppress 56500 // covered by FxCOP
                catch
                {
                    string message = null;
                    try
                    {
                        message = SR.GetString(SR.SFxRevertImpersonationFailed0);
                    }
                    finally
                    {
                        DiagnosticUtility.FailFast(message);
                    }
                }
            }
        }
        void ImpersonateCall(Action callToImpersonate)
        {
            ImpersonateOnSerializingReplyMessageProperty impersonationProperty = null;
            IDisposable impersonationContext = null;
            IPrincipal  originalPrincipal    = null;
            bool        isThreadPrincipalSet = false;

            if (!ImpersonateOnSerializingReplyMessageProperty.TryGet(this.innerMessage, out impersonationProperty))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.UnableToImpersonateWhileSerializingReponse)));
            }

            try
            {
                impersonationProperty.StartImpersonation(out impersonationContext, out originalPrincipal, out isThreadPrincipalSet);
                callToImpersonate();
            }
            finally
            {
                try
                {
                    impersonationProperty.StopImpersonation(impersonationContext, originalPrincipal, isThreadPrincipalSet);
                }
#pragma warning suppress 56500 // covered by FxCOP
                catch
                {
                    string message = null;
                    try
                    {
                        message = SR.GetString(SR.SFxRevertImpersonationFailed0);
                    }
                    finally
                    {
                        DiagnosticUtility.FailFast(message);
                    }
                }
            }
        }