示例#1
0
        public TResponse Execute <TResponse>(IApiRequest <TResponse> request, HttpContext context = null)
            where TResponse : class, IApiResponse, new()
        {
            if (this != Instance)
            {
                return(Instance.Execute(request, context));
            }
            IApiHandler handler = null;

            try
            {
                var type = typeof(ApiHandler <,>).MakeGenericType(request.GetType(), typeof(TResponse));
                handler = this.Resolve <IApiHandler>(type) ?? throw new Exception();
            }
            catch (Exception e)
            {
                throw new ResolveException("Resolve Error: " + e.Message);
            }
            if (handler == null)
            {
                throw new ResolveException("Handler Resolve Failed");
            }
            if (context != null)
            {
                handler.HttpContext = context;
            }
            try
            {
                return(handler.Execute(request) as TResponse);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
示例#2
0
        public IResponse Execute(IRequest request, HttpContext context = null)
        {
            if (this != Instance)
            {
                return(Instance.Execute(request, context));
            }
            IApiHandler handler = null;

            try
            {
                var name = request.GetType().FullName.ToLower();
                if (name.EndsWith("request"))
                {
                    name = name.Substring(0, name.Length - 7);
                }
                var type = ApiCache.Instance[name].BaseType;
                handler = this.Resolve <IApiHandler>(type);
            }
            catch (Exception e)
            {
                throw new ResolveException("Resolve Error: " + e.Message);
            }
            if (handler == null)
            {
                throw new ResolveException("Handler Resolve Failed");
            }
            if (context != null)
            {
                handler.HttpContext = context;
            }
            try
            {
                return(handler.Execute(request));
            }
            catch (Exception e)
            {
                throw e;
            }
        }