Exemple #1
0
        private async void OnConnectionRequestReceived(
            AppServiceConnection sender,
            AppServiceRequestReceivedEventArgs args)
        {
            try
            {
                // Execute the command and get the response.
                IServiceCommandResponse response;

                if (!ServiceCommand.TryDeserializeFromValueSet(
                        args.Request.Message,
                        out IServiceCommand command,
                        out IServiceCommandResponse errorResponse))
                {
                    response = errorResponse;
                }
                else
                {
                    _logger.LogDebug($"Received command: {command.ToDebugString()}");

                    var commandExecutor = new CommandExecutor(_logger);
                    response = commandExecutor.Execute(command);
                }

                // Serialize the response to a ValueSet.
                var returnValueSet = new ValueSet();
                response.SerializeToValueSet(returnValueSet);

                _logger.LogDebug($"Sending response: {response.ToDebugString()}");

                // Return the data to the caller.
                await args.Request.SendResponseAsync(returnValueSet);
            }
Exemple #2
0
        public void TryDeserializeValueSet_should_correctly_deserialize_a_RegistryWriteIntValueCommand()
        {
            var valueSet = new Dictionary <string, object>
            {
                [ParamName.CommandName.ToString()]       = ServiceCommandName.RegistryWriteIntValue.ToString(),
                [ParamName.RegistryBaseKey.ToString()]   = RegistryBaseKey.CurrentUser.ToString(),
                [ParamName.RegistryKey.ToString()]       = "Key",
                [ParamName.RegistryValueName.ToString()] = "ValueName",
                [ParamName.RegistryValue.ToString()]     = 123
            };

            ServiceCommand.TryDeserializeFromValueSet(
                valueSet,
                out IServiceCommand command,
                out IServiceCommandResponse errorResponse)
            .Should()
            .BeTrue();

            command.Should().BeOfType <RegistryWriteIntValueCommand>();
            errorResponse.Should().BeNull();

            var typedCommand = (RegistryWriteIntValueCommand)command;

            typedCommand.BaseKey.Should().Be(RegistryBaseKey.CurrentUser);
            typedCommand.Key.Should().Be("Key");
            typedCommand.ValueName.Should().Be("ValueName");
            typedCommand.Value.Should().Be(123);
        }
Exemple #3
0
        public void TryDeserializeFromValueSet_should_return_an_error_if_missing_the_command_name()
        {
            var valueSet = new Dictionary <string, object>
            {
                [ParamName.RegistryBaseKey.ToString()]      = RegistryBaseKey.CurrentUser.ToString(),
                [ParamName.RegistryKey.ToString()]          = "Key",
                [ParamName.RegistryValueName.ToString()]    = "ValueName",
                [ParamName.RegistryDefaultValue.ToString()] = 123
            };

            ServiceCommand.TryDeserializeFromValueSet(
                valueSet,
                out IServiceCommand command,
                out IServiceCommandResponse errorResponse)
            .Should()
            .BeFalse();

            command.Should().BeNull();
            errorResponse.Should().NotBeNull();

            errorResponse.CommandName.Should().Be(ServiceCommandName.Unknown);
        }
Exemple #4
0
        public void TryDeserializeValueSet_should_return_an_error_if_missing_a_required_parameter()
        {
            var valueSet = new Dictionary <string, object>
            {
                [ParamName.CommandName.ToString()]          = ServiceCommandName.RegistryReadIntValue.ToString(),
                [ParamName.RegistryKey.ToString()]          = "Key",
                [ParamName.RegistryValueName.ToString()]    = "ValueName",
                [ParamName.RegistryDefaultValue.ToString()] = 123
            };

            ServiceCommand.TryDeserializeFromValueSet(
                valueSet,
                out IServiceCommand command,
                out IServiceCommandResponse errorResponse)
            .Should()
            .BeFalse();

            command.Should().BeNull();
            errorResponse.Should().NotBeNull();

            errorResponse.ErrorCode.Should().Be(ServiceCommandErrorCode.MissingRequiredMessageValue);
            errorResponse.ErrorMessage.Should().Contain(ParamName.RegistryBaseKey.ToString());
        }
Exemple #5
0
        public void TryDeserializeValueSet_should_throw_on_null_params()
        {
            Action action = () => ServiceCommand.TryDeserializeFromValueSet(null, out _, out _);

            action.Should().ThrowExactly <ArgumentNullException>().And.ParamName.Should().Be("valueSet");
        }