/// <summary>
        /// Writes a value to the Windows registryKey.
        /// </summary>
        /// <typeparam name="T">The type of value that is written (<see cref="RegistryReadValueCommand{T}"/>).</typeparam>
        /// <param name="command">The command to execute.</param>
        /// <param name="logger">The logger to use.</param>
        /// <returns>A response indicating success and the written value, or failure with error details.</returns>
        public IServiceCommandResponse ExecuteWrite <T>(RegistryWriteValueCommand <T> command, ILogger logger)
        {
            Param.VerifyNotNull(command, nameof(command));
            Param.VerifyNotNull(logger, nameof(logger));

            IServiceCommandResponse response;

            try
            {
                var registryHive = (RegistryHive)Enum.Parse(typeof(RegistryHive), command.BaseKey.ToString());

                using (IWin32RegistryKey baseKey = _registry.OpenBaseKey(registryHive, RegistryView.Registry64))
                    using (IWin32RegistryKey subKey = baseKey.CreateSubKey(command.Key, writable: true))
                    {
                        subKey.SetValue(command.ValueName, command.Value, TypeToRegistryValueKind(typeof(T)));
                        response = ServiceCommandResponse.Create(command.CommandName, command.Value);
                    }
            }
            catch (Exception e)
            {
                response = ServiceCommandResponse.CreateError(
                    command.CommandName,
                    ServiceErrorInfo.RegistryWriteError(
                        RegistryPath.HiveToWin32Name(RegistryPath.BaseKeyToHive(command.BaseKey)),
                        command.Key,
                        command.ValueName,
                        e));
                logger.LogError(response.ErrorMessage);
            }

            return(response);
        }
        //// ===========================================================================================================
        //// Methods
        //// ===========================================================================================================

        /// <summary>
        /// Reads a value from the Windows registryKey.
        /// </summary>
        /// <typeparam name="T">The type of value that is read (<see cref="RegistryReadValueCommand{T}"/>).</typeparam>
        /// <param name="command">The command to execute.</param>
        /// <param name="logger">The logger to use.</param>
        /// <returns>A response indicating success and the read value, or failure with error details.</returns>
        public IServiceCommandResponse ExecuteRead <T>(RegistryReadValueCommand <T> command, ILogger logger)
        {
            Param.VerifyNotNull(command, nameof(command));
            Param.VerifyNotNull(logger, nameof(logger));

            ServiceCommandResponse response;

            try
            {
                var registryHive = (RegistryHive)Enum.Parse(typeof(RegistryHive), command.BaseKey.ToString());

                using (IWin32RegistryKey baseKey = _registry.OpenBaseKey(registryHive, RegistryView.Registry64))
                    using (IWin32RegistryKey subKey = baseKey.OpenSubKey(command.Key, writable: false))
                    {
                        object value = subKey?.GetValue(command.ValueName, command.DefaultValue) ?? command.DefaultValue;
                        response = ServiceCommandResponse.Create(command.CommandName, value);
                    }
            }
            catch (Exception e)
            {
                response = ServiceCommandResponse.CreateError(
                    command.CommandName,
                    ServiceErrorInfo.RegistryReadError(
                        RegistryPath.HiveToWin32Name(RegistryPath.BaseKeyToHive(command.BaseKey)),
                        command.Key,
                        command.ValueName,
                        e));
                logger.LogError(response.ErrorMessage);
            }

            return(response);
        }
Exemplo n.º 3
0
        //// ===========================================================================================================
        //// Methods
        //// ===========================================================================================================

        public async Task <IServiceCommandResponse> SendCommandAsync(IServiceCommand command)
        {
            var valueSet = new ValueSet();

            command.SerializeToValueSet(valueSet);
            AppServiceResponse bridgeResponse = await _connection.SendMessageAsync(valueSet);

            AppServiceResponseStatus status = bridgeResponse.Status;

            IServiceCommandResponse response;

            if (status == AppServiceResponseStatus.Success)
            {
                if (!ServiceCommandResponse.TryDeserializeFromValueSet(
                        bridgeResponse.Message,
                        out response,
                        out IServiceCommandResponse errorResponse))
                {
                    response = errorResponse;
                }
            }
            else
            {
                response = ServiceCommandResponse.CreateError(command.CommandName,
                                                              ServiceErrorInfo.InternalError($"AppServiceConnection failed with status '{status}'"));
            }

            return(response);
        }
        public void CreateError_should_store_the_properties()
        {
            var response = ServiceCommandResponse.CreateError(
                ServiceCommandName.RegistryReadIntValue,
                ServiceErrorInfo.InternalError("message"));

            response.CommandName.Should().Be(ServiceCommandName.RegistryReadIntValue);
            response.Result.Should().BeNull();
            response.ErrorCode.Should().Be(ServiceCommandErrorCode.InternalError);
            response.ErrorMessage.Should().NotBeNull();
        }
Exemplo n.º 5
0
        internal HttpErrorInfo(ServiceErrorInfo errorInfo)
        {
            ServiceError = errorInfo;
            StatusCode   = HttpStatusCode.InternalServerError;

            foreach (var parameter in GetHttpParameters(errorInfo))
            {
                if (parameter.Name == "code")
                {
                    StatusCode = TryParseStatusCodeInteger(parameter) ?? HttpStatusCode.InternalServerError;
                }
                else if (parameter.Name != "from")
                {
                    AddInvalidHttpParameterError(parameter);
                }
            }
        }
Exemplo n.º 6
0
        internal HttpErrorInfo(ServiceErrorInfo errorInfo)
        {
            ServiceError = errorInfo;
            StatusCode   = HttpStatusCode.InternalServerError;

            foreach (var parameter in errorInfo.GetHttpParameters())
            {
                if (parameter.Name == "code")
                {
                    StatusCode = HttpAttributeUtility.ParseStatusCodeInteger(parameter);
                }
                else
                {
                    throw parameter.CreateInvalidHttpParameterException();
                }
            }
        }
        public IServiceCommandResponse Execute(IServiceCommand command)
        {
            IServiceCommandResponse response;

            _logger.LogDebug("Executing command: ", command.ToDebugString());
            switch (command)
            {
            case ShutdownServerCommand _:
                response = ServiceCommandResponse.Create(ServiceCommandName.ShutdownServer, true);
                break;

            case EchoCommand echoCommand:
                response = ServiceCommandResponse.Create(ServiceCommandName.Echo, echoCommand.EchoMessage);
                break;

            case RegistryReadIntValueCommand registryCommand:
                response = _registryExecutor.ExecuteRead(registryCommand, _logger);
                break;

            case RegistryReadStringValueCommand registryCommand:
                response = _registryExecutor.ExecuteRead(registryCommand, _logger);
                break;

            case RegistryWriteIntValueCommand registryCommand:
                response = _registryExecutor.ExecuteWrite(registryCommand, _logger);
                break;

            default:
                _logger.LogWarning("Unsupported command: ", command.CommandName);
                response = ServiceCommandResponse.CreateError(
                    command.CommandName,
                    ServiceErrorInfo.InternalError(
                        $"Command '{command.CommandName}' is not supported for execution in the elevated bridge application."));
                break;
            }

            return(response);
        }
Exemplo n.º 8
0
 public static string GetErrorName(ServiceErrorInfo errorInfo) => CodeGenUtility.Capitalize(errorInfo.Name);