Ejemplo n.º 1
0
        private async Task<Connection> ConnectStartElevatedService()
        {
            var callingPid = ProcessHelper.GetCallerPid();

            Logger.Instance.Log($"Caller PID: {callingPid}", LogLevel.Debug);

            if (InputArguments.IntegrityLevel.HasValue && InputArguments.IntegrityLevel.Value == IntegrityLevel.System && !InputArguments.RunAsSystem)
            {
                Logger.Instance.Log($"Elevating as System because of IntegrityLevel=System parameter.", LogLevel.Warning);
                InputArguments.RunAsSystem = true;
            }

            IRpcClient rpcClient = GetClient();

            Connection connection = null;
            try
            {
                connection = await rpcClient.Connect(null, true).ConfigureAwait(false);
            }
            catch (System.IO.IOException) { }
            catch (TimeoutException) { }
            catch (Exception ex)
            {
                Logger.Instance.Log(ex.ToString(), LogLevel.Warning);
            }

            if (connection == null) // service is not running or listening.
            {
                if (!StartElevatedService(callingPid, cacheDuration: null))
                    return null;

                connection = await rpcClient.Connect(callingPid, false).ConfigureAwait(false);
            }
            return connection;
        }