示例#1
0
        private CommandResult Process(Command command)
        {
            var serviceDescriptor = ServiceRegistry.ResolveServiceDescriptor(command.CallContract);
            var methodDescriptor  = ServiceRegistry.ResolveMethodDescriptor(command.CallContract, command.Method2Invoke);

            List <object> parameters = new List <object>();

            foreach (var pd in methodDescriptor.ParameterDescriptors)
            {
                parameters.Add(ParameterParser.GetValue(command, pd));
            }

            object serviceInstance = Activator.CreateInstance(serviceDescriptor.ServiceType);

            bool      successful    = false;
            Exception exc           = null;
            bool      noReturnValue = false;
            object    resultValue   = null;

            if (methodDescriptor.MethodInfo.ReturnType.Equals(typeof(void)))
            {
                noReturnValue = true;
            }

            try
            {
                if (noReturnValue)
                {
                    methodDescriptor.MethodInfo.Invoke(serviceInstance, parameters.ToArray());
                }
                else
                {
                    resultValue = methodDescriptor.MethodInfo.Invoke(serviceInstance, parameters.ToArray());
                }
                successful = true;
            }
            catch (Exception ex)
            {
                exc        = ex.InnerException;
                successful = false;
            }

            CommandResult result = new CommandResult();

            result.Sucessful        = successful;
            result.ConnectionWorker = command.ConnectionWorker;

            if (successful)
            {
                if (!noReturnValue)
                {
                    result.Result = SerializerUtility.Instance().BinSerialize(resultValue);
                }
            }
            else
            {
                result.Exception = SerializerUtility.Instance().BinSerialize(exc);
            }

            result.ConnectionWorker.LastActiveTime = DateTime.Now; //reset timeout

            return(result);
        }
 public void GetValue_null_input_returns_null()
 {
     Assert.AreEqual(ParameterParser.GetValue(null), null);
 }
 public void GetValue_string_input_returns_null()
 {
     Assert.AreEqual(ParameterParser.GetValue(new[] { "0fw" }), null);
 }
 public void GetValue_valid_input_returns_value()
 {
     Assert.AreEqual(ParameterParser.GetValue(new [] { "3" }), 3);
 }
 public void GetValue_empty_input_returns_null()
 {
     Assert.AreEqual(ParameterParser.GetValue(new string[] {}), null);
 }