コード例 #1
0
        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            object result = null;
            Guid   incomingTransactionID = Guid.Empty;

            if (ContextUtil.IsInTransaction)
            {
                incomingTransactionID = ContextUtil.TransactionId;
            }
            ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInstanceCreationRequest,
                                               SR.TraceCodeComIntegrationInstanceCreationRequest, this.info, message, incomingTransactionID);

            WindowsIdentity callerIdentity = null;

            callerIdentity = MessageUtil.GetMessageIdentity(message);
            WindowsImpersonationContext impersonateContext = null;

            try
            {
                try
                {
                    if (this.info.HostingMode ==
                        HostingMode.WebHostOutOfProcess)
                    {
                        if (SecurityUtils.IsAtleastImpersonationToken(new SafeCloseHandle(callerIdentity.Token, false)))
                        {
                            impersonateContext = callerIdentity.Impersonate();
                        }
                        else
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new COMException(SR.GetString(SR.BadImpersonationLevelForOutOfProcWas), HR.ERROR_BAD_IMPERSONATION_LEVEL));
                        }
                    }

                    CLSCTX clsctx = CLSCTX.SERVER;
                    if (PlatformSupportsBitness && (this.info.HostingMode ==
                                                    HostingMode.WebHostOutOfProcess))
                    {
                        if (this.info.Bitness == Bitness.Bitness32)
                        {
                            clsctx |= CLSCTX.ACTIVATE_32_BIT_SERVER;
                        }
                        else
                        {
                            clsctx |= CLSCTX.ACTIVATE_64_BIT_SERVER;
                        }
                    }

                    result = SafeNativeMethods.CoCreateInstance(
                        info.Clsid,
                        null,
                        clsctx,
                        IID_IUnknown);
                }
                finally
                {
                    if (impersonateContext != null)
                    {
                        impersonateContext.Undo();
                    }
                }
            }
            catch (Exception e)
            {
                if (Fx.IsFatal(e))
                {
                    throw;
                }

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

                DiagnosticUtility.EventLog.LogEvent(TraceEventType.Error,
                                                    (ushort)System.Runtime.Diagnostics.EventLogCategory.ComPlus,
                                                    (uint)System.Runtime.Diagnostics.EventLogEventId.ComPlusInstanceCreationError,
                                                    from == null ? string.Empty : from.ToString(),
                                                    this.info.AppID.ToString(),
                                                    this.info.Clsid.ToString(),
                                                    incomingTransactionID.ToString(),
                                                    callerIdentity.Name,
                                                    e.ToString());

                throw TraceUtility.ThrowHelperError(e, message);
            }


            TransactionProxy proxy = instanceContext.Extensions.Find <TransactionProxy>();

            if (proxy != null)
            {
                proxy.InstanceID = result.GetHashCode();
            }

            ComPlusInstanceCreationTrace.Trace(TraceEventType.Verbose, TraceCode.ComIntegrationInstanceCreationSuccess,
                                               SR.TraceCodeComIntegrationInstanceCreationSuccess, this.info, message, result.GetHashCode(), incomingTransactionID);
            return(result);
        }