public object BeforeInvoke(
            InstanceContext instanceContext,
            IClientChannel channel,
            Message message)
        {
            ComPlusServerSecurity serverSecurity = null;
            WindowsImpersonationContext impersonateContext = null;
            bool errorTraced = false;
            WindowsIdentity identity = null;
            Uri from = null;
            object instance = null;
            int instanceID = 0;
            string action = null;
            TransactionProxy proxy = null;
            Transaction tx = null;
            Guid incomingTransactionID = Guid.Empty;

            // The outer try block is to comply with FXCOP's WrapVulnerableFinallyClausesInOuterTry rule.
            try
            {
                try
                {

                    identity = MessageUtil.GetMessageIdentity(message);

                    if (message.Headers.From != null)
                        from = message.Headers.From.Uri;

                    instance = instanceContext.GetServiceInstance(message);
                    instanceID = instance.GetHashCode();
                    action = message.Headers.Action;



                    ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethod,
                        SR.TraceCodeComIntegrationInvokingMethod, this.info, from, action, identity.Name, iid, instanceID, false);

                    // Security
                    //

                    if (this.info.CheckRoles)
                    {
                        if (!this.comAuth.IsAuthorizedForOperation(identity))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.CallAccessDenied());
                        }
                    }

                    if (this.info.HostingMode != HostingMode.WebHostOutOfProcess)
                    {
                        // NOTE: This has the side effect of setting up
                        //       the COM server security thing, so be sure
                        //       to clear it with Dispose() eventually.
                        //
                        serverSecurity = new ComPlusServerSecurity(identity,
                                                                   this.info.CheckRoles);
                    }

                    // Transactions
                    //
                    proxy = instanceContext.Extensions.Find<TransactionProxy>();
                    if (proxy != null)
                    {
                        // This makes the Tx header Understood.
                        tx = MessageUtil.GetMessageTransaction(message);
                        if (tx != null)
                        {
                            incomingTransactionID = tx.TransactionInformation.DistributedIdentifier;
                        }
                        try
                        {
                            if (tx != null)
                            {
                                proxy.SetTransaction(tx);
                            }
                            else
                            {
                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.TransactionMismatch());
                            }
                            ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethodNewTransaction,
                            SR.TraceCodeComIntegrationInvokingMethodNewTransaction, this.info, from, action, identity.Name, iid, instanceID, incomingTransactionID);
                        }
                        catch (FaultException e)
                        {
                            Transaction txProxy = proxy.CurrentTransaction;
                            Guid currentTransactionID = Guid.Empty;
                            if (txProxy != null)
                                currentTransactionID = txProxy.TransactionInformation.DistributedIdentifier;

                            string identityName = String.Empty;

                            if (null != identity)
                                identityName = identity.Name;

                            DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus,
                                (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInvokingMethodFailedMismatchedTransactions,
                                incomingTransactionID.ToString("B").ToUpperInvariant(),
                                currentTransactionID.ToString("B").ToUpperInvariant(),
                                from.ToString(),
                                this.info.AppID.ToString("B").ToUpperInvariant(),
                                this.info.Clsid.ToString("B").ToUpperInvariant(),
                                iid.ToString(),
                                action,
                                instanceID.ToString(CultureInfo.InvariantCulture),
                                System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture),
                                SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture),
                                identityName,
                                e.ToString());
                            errorTraced = true;
                            throw;
                        }
                    }
                    else
                    {
                        ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInvokingMethodContextTransaction,
                        SR.TraceCodeComIntegrationInvokingMethodContextTransaction, this.info, from, action, identity.Name, iid, instanceID, true);
                    }

                    // Impersonation
                    //
                    if (this.info.HostingMode == HostingMode.WebHostOutOfProcess)
                    {
                        impersonateContext = identity.Impersonate();
                    }

                    CorrelationState correlationState;
                    correlationState = new CorrelationState(impersonateContext,
                                                            serverSecurity,
                                                            from,
                                                            action,
                                                            identity.Name,
                                                            instanceID);

                    impersonateContext = null;
                    serverSecurity = null;


                    return correlationState;
                }
                finally
                {
                    if (impersonateContext != null)
                        impersonateContext.Undo();

                    if (serverSecurity != null)
                        ((IDisposable)serverSecurity).Dispose();
                }

            }
            catch (Exception e)
            {
                if (errorTraced == false)
                {
                    if (DiagnosticUtility.ShouldTraceError)
                    {
                        DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                           (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus,
                           (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInvokingMethodFailed,
                           from == null ? string.Empty : from.ToString(),
                           this.info.AppID.ToString("B").ToUpperInvariant(),
                           this.info.Clsid.ToString("B").ToUpperInvariant(),
                           iid.ToString("B").ToUpperInvariant(),
                           action,
                           instanceID.ToString(CultureInfo.InvariantCulture),
                           System.Threading.Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture),
                           SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture),
                           identity.Name,
                           e.ToString());
                    }
                }
                throw;
            }

        }
 public object BeforeInvoke(InstanceContext instanceContext, IClientChannel channel, Message message)
 {
     ComPlusServerSecurity serverSecurity = null;
     WindowsImpersonationContext context = null;
     object obj3;
     bool flag = false;
     WindowsIdentity clientIdentity = null;
     Uri from = null;
     int instanceID = 0;
     string action = null;
     TransactionProxy proxy = null;
     Transaction messageTransaction = null;
     Guid empty = Guid.Empty;
     try
     {
         try
         {
             clientIdentity = MessageUtil.GetMessageIdentity(message);
             if (message.Headers.From != null)
             {
                 from = message.Headers.From.Uri;
             }
             instanceID = instanceContext.GetServiceInstance(message).GetHashCode();
             action = message.Headers.Action;
             ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x50018, "TraceCodeComIntegrationInvokingMethod", this.info, from, action, clientIdentity.Name, this.iid, instanceID, false);
             if (this.info.CheckRoles && !this.comAuth.IsAuthorizedForOperation(clientIdentity))
             {
                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.CallAccessDenied());
             }
             if (this.info.HostingMode != HostingMode.WebHostOutOfProcess)
             {
                 serverSecurity = new ComPlusServerSecurity(clientIdentity, this.info.CheckRoles);
             }
             proxy = instanceContext.Extensions.Find<TransactionProxy>();
             if (proxy != null)
             {
                 messageTransaction = MessageUtil.GetMessageTransaction(message);
                 if (messageTransaction != null)
                 {
                     empty = messageTransaction.TransactionInformation.DistributedIdentifier;
                 }
                 try
                 {
                     if (messageTransaction == null)
                     {
                         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.TransactionMismatch());
                     }
                     proxy.SetTransaction(messageTransaction);
                     ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x5001a, "TraceCodeComIntegrationInvokingMethodNewTransaction", this.info, from, action, clientIdentity.Name, this.iid, instanceID, empty);
                     goto Label_02DC;
                 }
                 catch (FaultException exception)
                 {
                     Transaction currentTransaction = proxy.CurrentTransaction;
                     Guid distributedIdentifier = Guid.Empty;
                     if (currentTransaction != null)
                     {
                         distributedIdentifier = currentTransaction.TransactionInformation.DistributedIdentifier;
                     }
                     string name = string.Empty;
                     if (clientIdentity != null)
                     {
                         name = clientIdentity.Name;
                     }
                     DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId) (-1073610725), new string[] { empty.ToString("B").ToUpperInvariant(), distributedIdentifier.ToString("B").ToUpperInvariant(), from.ToString(), this.info.AppID.ToString("B").ToUpperInvariant(), this.info.Clsid.ToString("B").ToUpperInvariant(), this.iid.ToString(), action, instanceID.ToString(CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture), System.ServiceModel.ComIntegration.SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture), name, exception.ToString() });
                     flag = true;
                     throw;
                 }
             }
             ComPlusMethodCallTrace.Trace(TraceEventType.Verbose, 0x5001b, "TraceCodeComIntegrationInvokingMethodContextTransaction", this.info, from, action, clientIdentity.Name, this.iid, instanceID, true);
         Label_02DC:
             if (this.info.HostingMode == HostingMode.WebHostOutOfProcess)
             {
                 context = clientIdentity.Impersonate();
             }
             CorrelationState state = new CorrelationState(context, serverSecurity, from, action, clientIdentity.Name, instanceID);
             context = null;
             serverSecurity = null;
             obj3 = state;
         }
         finally
         {
             if (context != null)
             {
                 context.Undo();
             }
             if (serverSecurity != null)
             {
                 ((IDisposable) serverSecurity).Dispose();
             }
         }
     }
     catch (Exception exception2)
     {
         if (!flag && DiagnosticUtility.ShouldTraceError)
         {
             DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error, EventLogCategory.ComPlus, (EventLogEventId) (-1073610727), new string[] { (from == null) ? string.Empty : from.ToString(), this.info.AppID.ToString("B").ToUpperInvariant(), this.info.Clsid.ToString("B").ToUpperInvariant(), this.iid.ToString("B").ToUpperInvariant(), action, instanceID.ToString(CultureInfo.InvariantCulture), Thread.CurrentThread.ManagedThreadId.ToString(CultureInfo.InvariantCulture), System.ServiceModel.ComIntegration.SafeNativeMethods.GetCurrentThreadId().ToString(CultureInfo.InvariantCulture), clientIdentity.Name, exception2.ToString() });
         }
         throw;
     }
     return obj3;
 }