Beispiel #1
0
        private void StringRequesthandler(AppSession session, StringRequestInfo requestinfo)
        {
            var args = requestinfo.Body.Split('/');
            var info = new RequestedServiceInfo
            {
                Name       = args[0],
                Action     = args[1],
                Paramaters = string.Join("/", args.Skip(2).Take(args.Length - 2))
            };
            var result = ServiceExecuter.Execute(info);

            Console.WriteLine();
            session.Send($"{JsonConvert.SerializeObject(result.Result)}\r\n");
        }
Beispiel #2
0
        public static ActionResult Execute(RequestedServiceInfo info)
        {
            var result = new ActionResult();

            if (ServiceContainer.Services.Any(a => a.Name == info.Name))
            {
                var service    = ServiceContainer.Services.Single(a => a.Name == info.Name).GetInstance();
                var methodInfo = service.GetType().GetMethod(info.Action);
                if (methodInfo != null)
                {
                    var jsonArray      = JsonConvert.DeserializeObject <string[]>(info.Paramaters);
                    var parameterTypes = methodInfo.GetParameters().Select(a => a.ParameterType).ToArray();
                    var parameters     = Enumerable.Range(0, parameterTypes.Length)
                                         .Select(i => JsonConvert.DeserializeObject(jsonArray[i], parameterTypes[i])).ToArray();

                    result.Result    = methodInfo.Invoke(service, parameters);
                    result.IsSuccess = true;
                    return(result);
                }
            }
            throw new Exception("No action matched.");
        }