public RhspCommandResponse GetCommandResponse(RhspCommandCarrier carrier)
        {
            try
            {
                return(getCommandResponse(carrier));

                throw new Exception("test");
            }
            catch (Exception ex)
            {
                // Save full error info to log for further diagnosis.
                writeErrorToEventLog(ex);

                // Re-throw error so end-user sees message.
                throw new Exception(ex.Message);
            }
        }
        private RhspCommandResponse getCommandResponse(RhspCommandCarrier carrier)
        {
            RhspCommandResponse response = new RhspCommandResponse();

            string[] ctParts = carrier.Command.CommandText.Split('.');
            if (ctParts.Length != 2)
            {
                throw new RhspException(
                          "Unable to process command because it is not in " +
                          "the correct format (ModuleName.MethodName).");
            }

            string moduleName = ctParts[0];
            string methodName = ctParts[1];

            if (!moduleManagerTable.ContainsKey(moduleName))
            {
                throw new RhspException(
                          "Unable to process command because the module " +
                          "name '" + moduleName + "' was not recognised.");
            }

            Type managerType = moduleManagerTable[moduleName];

            RhspManager manager;

            if (managerType == typeof(HostingConfigManager))
            {
                // Ensure that only one hosting config is used.
                manager = service.Context.HostingConfig;
            }
            else if (managerType == typeof(ServerConfigManager))
            {
                // Ensure that only one server config is used.
                manager = service.Context.ServerConfig;
            }
            else
            {
                manager = RhspManager.CreateManager(
                    service.Context,
                    managerType);
            }

            MethodInfo methodInfo = getModuleMethod(
                manager,
                methodName,
                carrier.Command.Parameters);

            ParameterInfo[] systemParamArray = methodInfo.GetParameters();
            object[]        methodValueArray = new object[systemParamArray.Length];

            foreach (ParameterInfo systemParam in systemParamArray)
            {
                RhspParameter rhspParam = carrier.Command.Parameters[systemParam.Name];
                if (!systemParam.ParameterType.IsAssignableFrom(rhspParam.Value.GetType()))
                {
                    throw new RhspException(
                              "The data type '" + systemParam.ParameterType.Name + "' " +
                              "of system parameter with name '" + systemParam.Name + "' " +
                              "is not compatible with the equivilent command parameter " +
                              "of type '" + rhspParam.Value.GetType().Name + "'.");
                }

                // Assign the value to the correct position so it matches the signature.
                methodValueArray[systemParam.Position] = rhspParam.Value;
            }

            // Call method using manager so that apropriate events are fired.
            response.SetData(manager.InvokeMethod(methodInfo, methodValueArray));

            // Save both configs regardless of if changes have been made.
            // TODO: Only save if changes actually have been made.
            service.Context.HostingConfig.Save();
            service.Context.ServerConfig.Save();

            return(response);
        }
 public RhspCommandResponse GetCommandResponse(RhspCommandCarrier carrier)
 {
     return(commandHandler.GetCommandResponse(carrier));
 }