Beispiel #1
0
        private void OnRegister(ControllerAttribute attribute, Type type, object controller)
        {
            foreach (Type itype in attribute.Types)
            {
                if (!itype.IsInterface)
                {
                    continue;
                }
                if (type.GetInterface(itype.Name) == null)
                {
                    continue;
                }
                string url = "/" + (attribute.Name ?? itype.Name) + "/";
                foreach (MethodInfo method in type.GetMethods(BindingFlags.Public | BindingFlags.Instance))
                {
                    try
                    {
                        if (string.Compare("Equals", method.Name, true) == 0 ||
                            string.Compare("GetHashCode", method.Name, true) == 0 ||
                            string.Compare("GetType", method.Name, true) == 0 ||
                            string.Compare("ToString", method.Name, true) == 0 || method.Name.IndexOf("set_") >= 0 ||
                            method.Name.IndexOf("get_") >= 0)
                        {
                            continue;
                        }
                        ActionAttribute aa        = method.GetCustomAttribute <ActionAttribute>(false);
                        var             actionUrl = url + (aa == null ? method.Name : aa.Name);
                        if (mActionHadlers.TryGetValue(actionUrl, out ActionHandler handler))
                        {
                            Server.Log(EventArgs.LogType.Warring, $"{itype.Name}->{type.Name}.{method.Name} action already exists, can add ActionAttribute on the method");
                        }
                        else
                        {
                            handler = new ActionHandler(type, method, controller);
                            handler.SingleInstance    = attribute.SingleInstance;
                            handler.Interface         = itype;
                            mActionHadlers[actionUrl] = handler;

                            Server.Log(EventArgs.LogType.Info, $"Register {itype.Name}->{type.Name}@{method.Name} to {actionUrl}");
                        }
                    }
                    catch (Exception e_)
                    {
                        Server.Log(EventArgs.LogType.Error, $"Register {itype.Name}->{type.Name}@{method.Name} action error {e_.Message}@{e_.StackTrace}");
                    }
                }
            }
        }
Beispiel #2
0
        public async Task <Response> Execute(Request request)
        {
            long     runTime  = TimeWatch.GetElapsedMilliseconds();
            Response response = new Response();

            try
            {
                response.Status = (short)ResponseCode.SUCCESS;
                ActionHandler handler = GetActionHandler(request.Url);
                if (handler == null)
                {
                    response.Status = (int)ResponseCode.ACTION_NOT_FOUND;
                    response.Data   = new object[] { $"{request.Sesion?.RemoteEndPoint} execute {request.Url} not found!" };
                }
                else
                {
                    var controller = handler.Controller;
                    if (!handler.SingleInstance)
                    {
                        controller = CreateController(handler.ControllerType);
                    }
                    ActionContext context = new ActionContext(this.Server, request, handler, controller);
                    var           result  = await context.Execute();

                    if (result != null)
                    {
                        response.Data = new object[] { result }
                    }
                    ;
                }
            }
            catch (Exception e_)
            {
                response.Status = (int)ResponseCode.INNER_ERROR;
                response.Data   = new object[] { $"{request.Sesion?.RemoteEndPoint} execute {request.Url} error {e_.Message}" };
                if (Server.EnableLog(EventArgs.LogType.Error))
                {
                    Server.Log(EventArgs.LogType.Error, $"{request.Sesion?.RemoteEndPoint} execute {request.Url} error {e_.Message}@{e_.StackTrace}");
                }
            }
            response.ExecuteTime = TimeWatch.GetElapsedMilliseconds() - runTime;
            return(response);
        }