private static bool TryGetCommandName(
            IDictionary <string, object> valueSet,
            out ServiceCommandName commandName,
            out IServiceCommandResponse errorResponse)
        {
            if (!valueSet.TryGetValue(ParamName.CommandName.ToString(), out object rawValue))
            {
                errorResponse = ServiceCommandResponse.CreateError(
                    ServiceCommandName.Unknown,
                    ServiceErrorInfo.MissingRequiredMessageValue(ParamName.CommandName));
                commandName = ServiceCommandName.Unknown;
                return(false);
            }

            if (!Enum.TryParse(rawValue.ToString(), out commandName))
            {
                errorResponse = ServiceCommandResponse.CreateError(
                    ServiceCommandName.Unknown,
                    ServiceErrorInfo.WrongMessageValueType(
                        ParamName.CommandName,
                        rawValue.GetType(),
                        typeof(ServiceCommandName)));
                commandName = ServiceCommandName.Unknown;
                return(false);
            }

            errorResponse = null;
            return(true);
        }
        private bool GetRequiredValue(ParamName paramName, out object value)
        {
            if (ValueSet.TryGetValue(paramName.ToString(), out value))
            {
                return(true);
            }

            LastError = ServiceCommandResponse.CreateError(
                CommandName,
                ServiceErrorInfo.MissingRequiredMessageValue(paramName));
            return(false);
        }
 private bool TryConvertEnumValue <T>(ParamName paramName, object rawValue, out T value) where T : struct
 {
     try
     {
         value = (T)Enum.Parse(typeof(T), rawValue.ToString());
         return(true);
     }
     catch (Exception e) when(e is ArgumentException || e is OverflowException)
     {
         LastError = ServiceCommandResponse.CreateError(
             CommandName,
             ServiceErrorInfo.WrongMessageValueType(paramName, rawValue.GetType(), typeof(T)));
         value = default(T);
         return(false);
     }
 }
        public string GetStringValue(ParamName paramName)
        {
            if (!GetRequiredValue(paramName, out object rawValue))
            {
                return(null);
            }

            if (rawValue is string value)
            {
                return(value);
            }

            LastError = ServiceCommandResponse.CreateError(
                CommandName,
                ServiceErrorInfo.WrongMessageValueType(paramName, rawValue.GetType(), typeof(string)));
            return(null);
        }
        public int GetIntValue(ParamName paramName)
        {
            if (!GetRequiredValue(paramName, out object rawValue))
            {
                return(0);
            }

            if (rawValue is int value)
            {
                return(value);
            }

            LastError = ServiceCommandResponse.CreateError(
                CommandName,
                ServiceErrorInfo.WrongMessageValueType(paramName, rawValue.GetType(), typeof(int)));
            return(0);
        }
 public static ServiceCommandResponse CreateError(ServiceCommandName commandName, Exception exception)
 {
     return(CreateError(commandName, ServiceErrorInfo.InternalError(exception.Message)));
 }
 public static ServiceCommandResponse CreateError(
     ServiceCommandName commandName,
     ServiceErrorInfo errorInfo)
 {
     return(new ServiceCommandResponse(commandName, null, errorInfo.ErrorCode, errorInfo.ErrorMessage));
 }