Exemple #1
0
        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);
        }
        object SetupServiceConfig(InstanceContext instanceContext, Message message)
        {
            object serviceConfig = new CServiceConfig();

            // Threading
            //
            IServiceThreadPoolConfig threadPoolConfig;

            threadPoolConfig = (IServiceThreadPoolConfig)(serviceConfig);
            switch (this.info.ThreadingModel)
            {
            case ThreadingModel.MTA:
                threadPoolConfig.SelectThreadPool(ThreadPoolOption.MTA);
                break;

            case ThreadingModel.STA:
                threadPoolConfig.SelectThreadPool(ThreadPoolOption.STA);
                break;

            default:
                Fx.Assert("Unexpected threading model");

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(Error.UnexpectedThreadingModel());
            }
            threadPoolConfig.SetBindingInfo(BindingOption.BindingToPoolThread);

            // SxS activation context properties
            //


            // Manifest for VARIANT UDT types
            //

            // this only gets executed if we actually have UDTs
            if (this.info.HasUdts())
            {
                IServiceSxsConfig sxsConfig = serviceConfig as IServiceSxsConfig;
                if (sxsConfig == null)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.QFENotPresent());
                }

                lock (manifestLock)
                {
                    string tempPath = String.Empty;

                    try
                    {
                        tempPath = Path.GetTempPath();
                    }
                    catch (Exception e)
                    {
                        if (Fx.IsFatal(e))
                        {
                            throw;
                        }

                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.CannotAccessDirectory(tempPath));
                    }

                    string manifestDirectory = tempPath + this.info.AppID.ToString() + @"\";


                    if (!Directory.Exists(manifestDirectory))
                    {
                        try
                        {
                            Directory.CreateDirectory(manifestDirectory);
                        }
                        catch (Exception e)
                        {
                            if (Fx.IsFatal(e))
                            {
                                throw;
                            }

                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(System.ServiceModel.ComIntegration.Error.CannotAccessDirectory(manifestDirectory));
                        }

                        Guid[] assemblyGuids = this.info.Assemblies;
                        ComIntegrationManifestGenerator.GenerateManifestCollectionFile(assemblyGuids, manifestDirectory + manifestFileName + @".manifest", manifestFileName);

                        foreach (Guid g in assemblyGuids)
                        {
                            Type[] types = this.info.GetTypes(g);
                            if (types.Length > 0)
                            {
                                String guidStr = g.ToString();
                                ComIntegrationManifestGenerator.GenerateWin32ManifestFile(types, manifestDirectory + guidStr + @".manifest", guidStr);
                            }
                        }
                    }


                    sxsConfig.SxsConfig(CSC_SxsConfig.CSC_NewSxs);
                    sxsConfig.SxsName(manifestFileName + @".manifest");
                    sxsConfig.SxsDirectory(manifestDirectory);
                }
            }


            // Partitions
            //
            if (this.info.PartitionId != DefaultPartitionId)
            {
                IServicePartitionConfig partitionConfig;
                partitionConfig = (IServicePartitionConfig)(serviceConfig);
                partitionConfig.PartitionConfig(PartitionOption.New);
                partitionConfig.PartitionID(this.info.PartitionId);
            }

            // Transactions
            //
            IServiceTransactionConfig transactionConfig;

            transactionConfig = (IServiceTransactionConfig)(serviceConfig);
            transactionConfig.ConfigureTransaction(
                TransactionConfig.NoTransaction);

            if ((this.info.TransactionOption == TransactionOption.Required) ||
                (this.info.TransactionOption == TransactionOption.Supported))
            {
                Transaction messageTransaction = null;
                messageTransaction = MessageUtil.GetMessageTransaction(message);
                if (messageTransaction != null)
                {
                    TransactionProxy proxy = new TransactionProxy(info.AppID, info.Clsid);
                    proxy.SetTransaction(messageTransaction);

                    instanceContext.Extensions.Add(proxy);
                    IServiceSysTxnConfig sysTxnconfing = (IServiceSysTxnConfig)transactionConfig;
                    IntPtr pUnk = TransactionProxyBuilder.CreateTransactionProxyTearOff(proxy);
                    sysTxnconfing.ConfigureBYOTSysTxn(pUnk);
                    Marshal.Release(pUnk);
                }
            }
            return(serviceConfig);
        }
        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;
            }
        }