/// <summary> /// Processes incoming requests and routes them to the appropriate JSON handler method. /// </summary> /// <param name="context"></param> void IHttpHandler.ProcessRequest(HttpContext context) { IsExecuting = true; try { // Perform the requested operation switch (context.Request.PathInfo) { case "/GetType": // Enable response caching context.Response.Cache.SetCacheability(HttpCacheability.Public); context.Response.Cache.SetExpires(DateTime.Now.AddDays(7)); context.Response.Cache.SetMaxAge(TimeSpan.FromDays(7)); // Output the type metadata context.Response.ContentType = "application/json"; JsonUtility.Serialize(context.Response.OutputStream, new ServiceRequest(context.Request.QueryString["type"].Replace("\"", "")).Invoke(null)); break; case "/LogError": // Raise the error event context.Response.ContentType = "application/json"; ExoWeb.OnError(JsonUtility.Deserialize<ServiceError>(context.Request.InputStream)); break; default: // Deserialize the request context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetNoStore(); ServiceRequest request = JsonUtility.Deserialize<ServiceRequest>(context.Request.InputStream); // Invoke the request and output the response context.Response.ContentType = "application/json"; using (var test = new StringWriter()) { JsonUtility.Serialize(test, request.Invoke(null)); context.Response.Write(test.ToString()); } break; } } catch (Exception e) { // look for an ExoModel.ModelException that may display a more // abstracted or informative message in the UI. Otherwise, use // the inner most exception Exception reported = e; for (Exception x = e; x != null; x = x.InnerException) { reported = x; if (x is ModelException) break; } // Create an error to log var error = new ServiceError(); error.Type = reported.GetType().FullName; error.StackTrace = GetFullStackTrace(e); error.Message = reported.Message; error.Url = context.Request.RawUrl; if (error.AdditionalInfo == null) error.AdditionalInfo = new Dictionary<string, object>(); //error.AdditionalInfo.Add("Client.RequestJson", json); // Raise the error event ExoWeb.OnError(error); // Also send the error information to the client context.Response.Clear(); context.Response.ContentType = "application/json"; context.Response.StatusCode = 500; // internal server error // Enable error information on client if (ExoWeb.EnableExceptionInformation) { context.Response.AddHeader("jsonerror", "true"); // Ensure IIS 7 doesn't intercept the error context.Response.TrySkipIisCustomErrors = true; } context.Response.Write(ExoWeb.ToJson(error)); } }
internal ServiceErrorEventArgs(ServiceError error) { this.Error = error; }