public bool Initialize(RuntimeSetup runtimeSetup, ILogger logger)
            {
                hook = HostUtils.CreateInstance <ElevationHook>(host);

                if (!hook.HasElevatedPrivileges())
                {
                    logger.Log(LogSeverity.Warning, "Failed to create an elevation context out of process.  The process was created successfully but it did not acquire elevated privileges for an unknown reason.");
                    return(false);
                }

                hook.Initialize(runtimeSetup, new RemoteLogger(logger));
                return(true);
            }
示例#2
0
        /// <summary>
        /// Runs an isolated task in a host.
        /// </summary>
        /// <typeparam name="TIsolatedTask">The isolated task type.</typeparam>
        /// <param name="hostSetup">The host setup which includes the test isolation option properties copied down, not null.</param>
        /// <param name="statusReporter">The status reporter, not null.</param>
        /// <param name="args">The task arguments.</param>
        /// <returns>The task result.</returns>
        protected virtual object RunIsolatedTaskInHost <TIsolatedTask>(HostSetup hostSetup, StatusReporter statusReporter, object[] args)
            where TIsolatedTask : IsolatedTask
        {
            IHost host = null;

            try
            {
                statusReporter("Creating test host.");
                host = hostFactory.CreateHost(hostSetup, logger);

                RemoteLogger remoteLogger = new RemoteLogger(logger);
                RuntimeSetup runtimeSetup = RuntimeAccessor.Instance.GetRuntimeSetup().Copy();

                Shim shim = HostUtils.CreateInstance <Shim>(host);
                try
                {
                    statusReporter("Initializing the runtime.");
                    shim.Initialize(runtimeSetup, remoteLogger);
                    statusReporter("");

                    var isolatedTask = HostUtils.CreateInstance <TIsolatedTask>(host);

                    ManualResetEvent disconnectedEvent        = new ManualResetEvent(false);
                    EventHandler     disconnectedEventHandler = (sender, e) => disconnectedEvent.Set();
                    try
                    {
                        host.Disconnected += disconnectedEventHandler;

                        RunIsolatedTaskDelegate isolatedTaskDelegate = isolatedTask.Run;
                        IAsyncResult            asyncResult          = isolatedTaskDelegate.BeginInvoke(args, null, null);

                        WaitHandle.WaitAny(new[] { disconnectedEvent, asyncResult.AsyncWaitHandle });

                        if (asyncResult.IsCompleted)
                        {
                            return(isolatedTaskDelegate.EndInvoke(asyncResult));
                        }

                        throw new TestIsolationException("The host disconnected or was terminated prematurely while the task was running.");
                    }
                    finally
                    {
                        host.Disconnected -= disconnectedEventHandler;
                    }
                }
                finally
                {
                    statusReporter("Shutting down the runtime.");
                    shim.Shutdown();

                    GC.KeepAlive(remoteLogger);
                }
            }
            finally
            {
                statusReporter("Disposing test host.");

                try
                {
                    if (host != null)
                    {
                        host.Dispose();
                    }
                }
                catch (Exception ex)
                {
                    UnhandledExceptionPolicy.Report("An exception occurred while disposing the test host.", ex);
                }

                statusReporter("");
            }
        }