public InvocationResult ProcessRequest(string request)
{
try
{
//Get the method name from the request
var methodName = !request.Contains(" ")
? request
: request.Substring(0, request.IndexOf(" ", StringComparison.CurrentCultureIgnoreCase));
//Find the method on the handler
var methods = Handler.GetType().GetRuntimeMethods();
var method = methods.Single(s => s.Name == methodName);
//Get the method parameters
var parameters = method.GetParameters();
object argument = null;
//If the method requires parameters, deserialize the parameter based on the required type
if (parameters.Any())
{
var serializedParameter =
request.Substring(request.IndexOf(" ", StringComparison.CurrentCultureIgnoreCase) + 1);
argument = JsonConvert.Deserialize(serializedParameter, parameters.Single().ParameterType);
}
//Invoke the method on Handler and return the result
var result = method.Invoke(Handler, argument == null ? null : new[] {argument});
return new InvocationResult
{
Invoked = true,
Result = result
};
}
catch(Exception exception)
{
//Return a failed invocation result with the Exception message
return new InvocationResult
{
Invoked = false,
ErrorMessage = exception.ToString()
};
}
}
}