private static bool TryDeserialize(
            BridgeMessageDeserializer deserializer,
            out IServiceCommandResponse response,
            out IServiceCommandResponse errorResponse)
        {
            // Check for a non-success ErrorCode, which indicates we're deserializing an error
            if (deserializer.TryGetOptionalEnumValue(ParamName.ErrorCode, out ServiceCommandErrorCode errorCode) &&
                errorCode != ServiceCommandErrorCode.Success)
            {
                string errorMessage = deserializer.GetStringValue(ParamName.ErrorMessage);
                response = deserializer.LastError == null
                    ? new ServiceCommandResponse(deserializer.CommandName, null, errorCode, errorMessage)
                    : null;
            }
            // Deserialize a successful result
            else
            {
                object result = deserializer.GetValue(ParamName.CommandResult);
                response = deserializer.LastError == null
                    ? new ServiceCommandResponse(deserializer.CommandName, result, ServiceCommandErrorCode.Success, null)
                    : null;
            }

            errorResponse = deserializer.LastError;
            return(errorResponse == null);
        }
        //// ===========================================================================================================
        //// Methods
        //// ===========================================================================================================

        public static bool TryCreateFromJsonString(
            string jsonString,
            out BridgeMessageDeserializer deserializer,
            out IServiceCommandResponse errorResponse)
        {
            Param.VerifyString(jsonString, nameof(jsonString));

            var valueSet = new Dictionary <string, object>();

            try
            {
                // Try to parse the JSON.
                var jsonObject = JObject.Parse(jsonString);

                // Populate a value set from the parsed JSON.
                foreach (KeyValuePair <string, JToken> pair in jsonObject)
                {
                    valueSet.Add(pair.Key, ((JValue)pair.Value).Value);
                }
            }
            catch (Exception e)
            {
                deserializer  = null;
                errorResponse = ServiceCommandResponse.CreateError(ServiceCommandName.Unknown, e);
                return(false);
            }

            return(TryCreateFromValueSet(valueSet, out deserializer, out errorResponse));
        }
        public static bool TryDeserializeFromValueSet(
            IDictionary <string, object> valueSet,
            out IServiceCommandResponse response,
            out IServiceCommandResponse errorResponse)
        {
            if (!BridgeMessageDeserializer.TryCreateFromValueSet(
                    valueSet,
                    out BridgeMessageDeserializer deserializer,
                    out errorResponse))
            {
                response = null;
                return(false);
            }

            return(TryDeserialize(deserializer, out response, out errorResponse));
        }
        public static bool TryDeserializeFromJsonString(
            string jsonString,
            out IServiceCommandResponse response,
            out IServiceCommandResponse errorResponse)
        {
            if (!BridgeMessageDeserializer.TryCreateFromJsonString(
                    jsonString,
                    out BridgeMessageDeserializer deserializer,
                    out errorResponse))
            {
                response = null;
                return(false);
            }

            return(TryDeserialize(deserializer, out response, out errorResponse));
        }
        public static bool TryCreateFromValueSet(
            IDictionary <string, object> valueSet,
            out BridgeMessageDeserializer deserializer,
            out IServiceCommandResponse errorResponse)
        {
            Param.VerifyNotNull(valueSet, nameof(valueSet));

            if (!TryGetCommandName(valueSet, out ServiceCommandName commandName, out errorResponse))
            {
                deserializer = null;
                return(false);
            }

            deserializer = new BridgeMessageDeserializer(valueSet, commandName);
            return(true);
        }
Exemple #6
0
        private static bool TryDeserialize(
            BridgeMessageDeserializer deserializer,
            out IServiceCommand command,
            out IServiceCommandResponse errorResponse)
        {
            switch (deserializer.CommandName)
            {
            case ServiceCommandName.ShutdownServer:
                command = new ShutdownServerCommand();
                break;

            case ServiceCommandName.Echo:
                command = new EchoCommand(deserializer);
                break;

            case ServiceCommandName.RegistryReadIntValue:
                command = new RegistryReadIntValueCommand(deserializer);
                break;

            case ServiceCommandName.RegistryReadStringValue:
                command = new RegistryReadStringValueCommand(deserializer);
                break;

            case ServiceCommandName.RegistryWriteIntValue:
                command = new RegistryWriteIntValueCommand(deserializer);
                break;

            case ServiceCommandName.RegistryWriteStringValue:
                command = new RegistryWriteStringValueCommand(deserializer);
                break;

            case ServiceCommandName.Unknown:
            default:
                throw new InvalidOperationException(
                          "This should be unreachable because the deserializer should have detected an invalid command name");
            }

            if (deserializer.HadError)
            {
                command = null;
            }
            errorResponse = deserializer.LastError;
            return(!deserializer.HadError);
        }