Ejemplo n.º 1
0
        public static object ServiceExceptionHandler(IHttpRequest httpReq, object request, Exception ex)
        {
            if (EndpointHost.Config != null && EndpointHost.Config.ReturnsInnerException && ex.InnerException != null && !(ex is IHttpError))
            {
                ex = ex.InnerException;
            }
            ResponseStatus responseStatus = ex.ToResponseStatus();


            if (EndpointHost.DebugMode)
            {
                responseStatus.StackTrace = DtoUtils.GetRequestErrorBody(request) + "\n" + ex;
            }

            var error = DtoUtils.CreateErrorResponse(request, ex, responseStatus);

            IHttpError httpError = error as IHttpError;

            if (httpError != null)
            {
                if (httpReq.QueryString["errorFormat"] == "json")
                {
                    httpError.ContentType = "application/json";
                }
            }

            return(httpError);
        }
Ejemplo n.º 2
0
        public override void Configure(Funq.Container container)
        {
            JsConfig.ExcludeTypeInfo = true;

            Logger.Info("Reading global configuration");
            //register any dependencies your services use, e.g:
            //container.Register<ICacheClient>(new MemoryCacheClient());
            WebConfig =
                System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);

            Logger.Info("Configure ServiceStack EndpointHostConfig");
            base.SetConfig(new EndpointHostConfig {
                GlobalResponseHeaders =
                {
                    { "Access-Control-Allow-Origin",  "*"                                   },
                    { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"     },
                    { "Cache-Control",                "no-cache, no-store, must-revalidate" },
                    { "Pragma",                       "no-cache"                            },
                    { "Expires",                      "0"                                   }
                },
                EnableAccessRestrictions = true,
                DebugMode                = true, //Enable StackTraces in development
                WebHostUrl               = WebConfig.AppSettings.Settings["baseUrl"].Value,
                WriteErrorsToResponse    = true,
                DefaultContentType       = ServiceStack.Common.Web.ContentType.Json,
                MapExceptionToStatusCode =
                {
                    { typeof(NotFoundException), 404 },
                }
            });

            Logger.Info("Load Plugins");
            LoadPlugins();

            Logger.Info("Configure Service Exception Handler");
            this.ServiceExceptionHandler = (httpReq, request, ex) => {
                if (EndpointHost.Config != null && EndpointHost.Config.ReturnsInnerException && ex.InnerException != null && !(ex is IHttpError))
                {
                    ex = ex.InnerException;
                }
                ResponseStatus responseStatus = ex.ToResponseStatus();
                if (EndpointHost.DebugMode)
                {
                    responseStatus.StackTrace = DtoUtils.GetRequestErrorBody(request) + "\n" + ex;
                }
                return(DtoUtils.CreateErrorResponse(request, ex, responseStatus));
            };

            Logger.Info("Register ContentType Filters");
            this.ContentTypeFilters.Register("application/opensearchdescription+xml", OpenSearchDescriptionService.OpenSearchDescriptionSerializer, OpenSearchDescriptionService.OpenSearchDescriptionDeserializer);

            this.PreRequestFilters.Insert(0, (httpReq, httpRes) => {
                httpReq.UseBufferedStream = true;
            });
        }