Ejemplo n.º 1
0
        public void Process(HttpContextBase context)
        {
            //Resolve all here
            ApiResponce            = Container.Resolve <IApiStandartResponce>();
            ApiManager             = Container.Resolve <IApiManager>();
            RouteContext           = new RequestContext(context, RouteData);
            ApiContext             = new ApiContext(RouteContext);
            ApiResponce.ApiContext = ApiContext;

            //NOTE: Don't register anything it will be resolved when needed
            //Container.RegisterInstance(ApiContext, new HttpContextLifetimeManager2(context));//Regiter only api context

            Method = ApiManager.GetMethod(((Route)RouteData.Route).Url, context.Request.RequestType);//Set method

            DoProcess(context);
        }
        public void Process(HttpContextBase context)
        {
            using (Container)
            {
                ApiManager   = Container.Resolve <IApiManager>();
                RouteContext = new RequestContext(context, RouteData);
                var config = Container.Resolve <IApiConfiguration>();
                ApiContext  = Container.Resolve <ApiContext>(new NamedParameter("requestContext", RouteContext), new NamedParameter("apiConfiguration", config));
                ApiResponce = Container.Resolve <IApiStandartResponce>();

                //NOTE: Don't register anything it will be resolved when needed
                //Container.RegisterInstance(ApiContext, new HttpContextLifetimeManager2(context));//Regiter only api context

                Method = ApiManager.GetMethod(((Route)RouteData.Route).Url, context.Request.RequestType);  //Set method

                DoProcess(context);
            }
        }
Ejemplo n.º 3
0
        protected override void DoProcess(HttpContextBase context)
        {
            Log.Debug("strating request. context: '{0}'", ApiContext);

            //Neeeded to rollback errors
            context.Response.Buffer       = true;
            context.Response.BufferOutput = true;

            try
            {
                Log.Debug("method invoke");
                ApiResponce.Count      = ApiContext.Count;
                ApiResponce.StartIndex = ApiContext.StartIndex;

                if (Method != null)
                {
                    var responce = ApiManager.InvokeMethod(Method, ApiContext);
                    if (responce is Exception)
                    {
                        SetError(context, (Exception)responce, HttpStatusCode.InternalServerError);
                    }
                    else
                    {
                        // success
                        PostProcessResponse(context, responce);
                    }
                }
                else
                {
                    SetError(context, new MissingMethodException("Method not found"), HttpStatusCode.NotFound);
                }
            }
            catch (TargetInvocationException targetInvocationException)
            {
                if (targetInvocationException.InnerException is ItemNotFoundException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.NotFound, "The record could not be found");
                }
                else if (targetInvocationException.InnerException is ArgumentException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.BadRequest, "Invalid arguments");
                }
                else
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.InternalServerError);
                }
            }
            catch (Exception e)
            {
                SetError(context, e, HttpStatusCode.InternalServerError);
            }

            Exception responseError;

            try
            {
                RespondTo(Method, context);
                return;
            }
            catch (ThreadAbortException e)
            {
                //Do nothing. someone killing response
                Log.Error(e, "thread aborted. response not sent");
                return;
            }
            catch (HttpException exception)
            {
                responseError = exception;
                SetError(context, exception, (HttpStatusCode)exception.GetHttpCode());//Set the code of throwed exception
            }
            catch (Exception exception)
            {
                responseError = exception;
                SetError(context, exception, HttpStatusCode.InternalServerError);
            }
            Log.Error(responseError, "error happened while sending response. can't be here");
            RespondTo(Method, context);//If we got there then something went wrong
        }
Ejemplo n.º 4
0
        protected override void DoProcess(HttpContextBase context)
        {
            Log.DebugFormat("strating request. context: '{0}'", ApiContext);

            //Neeeded to rollback errors
            context.Response.Buffer       = true;
            context.Response.BufferOutput = true;

            IApiEntryPoint instance = null;

            try
            {
                Log.Debug("method invoke");
                ApiResponce.Count      = ApiContext.Count;
                ApiResponce.StartIndex = ApiContext.StartIndex;

                if (Method != null)
                {
                    if (!string.IsNullOrEmpty(Method.Name))
                    {
                        instance = Container.ResolveNamed <IApiEntryPoint>(Method.Name, new TypedParameter(typeof(ApiContext), ApiContext));
                    }
                    else
                    {
                        instance = Container.Resolve <IApiEntryPoint>();
                    }

                    var responce = ApiManager.InvokeMethod(Method, ApiContext, instance);
                    if (responce is Exception)
                    {
                        SetError(context, (Exception)responce, HttpStatusCode.InternalServerError);
                    }
                    else
                    {
                        // success
                        PostProcessResponse(context, responce);
                    }
                }
                else
                {
                    SetError(context, new MissingMethodException("Method not found"), HttpStatusCode.NotFound);
                }
            }
            catch (TargetInvocationException targetInvocationException)
            {
                if (targetInvocationException.InnerException is ItemNotFoundException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.NotFound, "The record could not be found");
                }
                else if (targetInvocationException.InnerException is ArgumentException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.BadRequest, "Invalid arguments");
                }
                else if (targetInvocationException.InnerException is SecurityException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.Forbidden, "Access denied");
                }
                else if (targetInvocationException.InnerException is InvalidOperationException)
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.Forbidden);
                }
                else
                {
                    SetError(context, targetInvocationException.InnerException, HttpStatusCode.InternalServerError);
                }
            }
            catch (ApiArgumentMismatchException e)
            {
                SetError(context, e, HttpStatusCode.BadRequest, "Invalid arguments");
            }
            catch (Exception e)
            {
                SetError(context, e, HttpStatusCode.InternalServerError);
            }

            Exception responseError;

            try
            {
                RespondTo(Method, context);
                return;
            }
            catch (ThreadAbortException e)
            {
                //Do nothing. someone killing response
                Log.Error("thread aborted. response not sent", e);
                return;
            }
            catch (HttpException exception)
            {
                responseError = exception;
                SetError(context, exception, (HttpStatusCode)exception.GetHttpCode());//Set the code of throwed exception
            }
            catch (Exception exception)
            {
                responseError = exception;
                SetError(context, exception, HttpStatusCode.InternalServerError);
            }
            Log.Error("error happened while sending response. can't be here", responseError);
            RespondTo(Method, context);//If we got there then something went wrong
        }