ChatterBox.Common.Communication.Helpers.ChannelInvoker.ProcessRequest C# (CSharp) Method

ProcessRequest() public method

Handles the request by deserializing it and invoking the requested method on the handler object
public ProcessRequest ( string request ) : InvocationResult
request string The serialized request in the format
return InvocationResult
        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()
                };
            }
        }
    }

Usage Example

 public void WaitForRegistration()
 {
     Task.Run(async () =>
     {
         var reader = new StreamReader(TcpClient.GetStream());
         var clientChannelProxy = new ChannelInvoker(this);
         var message = await reader.ReadLineAsync();
         if (!clientChannelProxy.ProcessRequest(message).Invoked)
         {
             OnInvalidRequest(InvalidMessage.For(message));
         }
     });
 }
All Usage Examples Of ChatterBox.Common.Communication.Helpers.ChannelInvoker::ProcessRequest