public void ProcessRequest(HttpContext context) { App app = CodeTorch.Core.Configuration.GetInstance().App; StringBuilder builder = new StringBuilder(); StringWriter writer = new StringWriter(builder); try { if (Me == null) { if (context.Request.Path.ToLower().EndsWith("xml")) { Format = "xml"; } throw new ApplicationException("No service has been configured for this path"); } DataCommand command = null; RestServiceMethodReturnTypeEnum returnType = RestServiceMethodReturnTypeEnum.None; //collect parameters DataTable dt = null; XmlDocument doc = null; context.Response.TrySkipIisCustomErrors = true; if (Format.ToLower() == "json") { context.Response.ContentType = "application/json"; } else { context.Response.ContentType = "text/xml"; } BaseRestServiceMethod method = Me.Methods.Where(i => (i.Action.ToString() == HttpContext.Current.Request.HttpMethod)).SingleOrDefault(); if (method != null) { //pass parameters - minus dbcommand parameters to datacommand DataCommandService dataCommandDB = DataCommandService.GetInstance(); if (String.IsNullOrEmpty(method.RequestDataCommand)) { throw new ApplicationException(String.Format("Request Data Command has not been configured for this service - {0}", Me.Name)); } command = DataCommand.GetDataCommand(method.RequestDataCommand); if (command == null) { throw new ApplicationException(String.Format("Request Data Command - {0} - could not be found in configuration", method.RequestDataCommand)); } List <ScreenDataCommandParameter> parameters = GetPopulatedCommandParameters(method.RequestDataCommand, method.DataCommands, null); returnType = method.ReturnType; //execute request datacommand and return data if applicable switch (command.ReturnType) { case DataCommandReturnType.Integer: object newID = dataCommandDB.ExecuteDataCommand(method.RequestDataCommand, parameters); if (method is PostRestServiceMethod) { DataCommand postCommand = null; PostRestServiceMethod postMethod = (PostRestServiceMethod)method; returnType = postMethod.ReturnType; if (!String.IsNullOrEmpty(postMethod.ResponseDataCommand)) { postCommand = DataCommand.GetDataCommand(postMethod.ResponseDataCommand); if (postCommand == null) { throw new ApplicationException(String.Format("Response Data Command - {0} - could not be found in configuration", postMethod.ResponseDataCommand)); } parameters = GetPopulatedCommandParameters(postMethod.ResponseDataCommand, method.DataCommands, newID); switch (returnType) { case RestServiceMethodReturnTypeEnum.DataRow: case RestServiceMethodReturnTypeEnum.DataTable: dt = dataCommandDB.GetDataForDataCommand(postMethod.ResponseDataCommand, parameters); break; case RestServiceMethodReturnTypeEnum.Xml: doc = dataCommandDB.GetXmlDataForDataCommand(postMethod.ResponseDataCommand, parameters); break; } } } if (method is PutRestServiceMethod) { PutRestServiceMethod putMethod = (PutRestServiceMethod)method; returnType = putMethod.ReturnType; if (!String.IsNullOrEmpty(putMethod.ResponseDataCommand)) { parameters = GetPopulatedCommandParameters(putMethod.ResponseDataCommand, method.DataCommands, newID); dt = dataCommandDB.GetDataForDataCommand(putMethod.ResponseDataCommand, parameters); } } break; case DataCommandReturnType.Xml: doc = dataCommandDB.GetXmlDataForDataCommand(method.RequestDataCommand, parameters); break; default: dt = dataCommandDB.GetDataForDataCommand(method.RequestDataCommand, parameters); break; } //in certain cases execute response datacommand } else { throw new NotSupportedException(); } //get data if any if ( ((dt != null) && (returnType == RestServiceMethodReturnTypeEnum.DataTable)) || ((dt != null) && (returnType == RestServiceMethodReturnTypeEnum.DataRow)) || ((doc != null) && (returnType == RestServiceMethodReturnTypeEnum.Xml)) ) { if (Format.ToLower() == "json") { using (JsonWriter json = new JsonTextWriter(writer)) { DataColumnCollection columns = null; switch (returnType) { case RestServiceMethodReturnTypeEnum.DataTable: columns = dt.Columns; BuildJsonArrayResponse(app, context, method, command, dt, json, columns); break; case RestServiceMethodReturnTypeEnum.DataRow: columns = dt.Columns; BuildJsonObjectResponse(app, context, method, command, dt, json, columns); break; case RestServiceMethodReturnTypeEnum.Xml: BuildJsonXmlObjectResponse(app, method, builder, doc); break; } } } else { //xml using (XmlWriter xml = new XmlTextWriter(writer)) { DataColumnCollection columns = null; switch (method.ReturnType) { case RestServiceMethodReturnTypeEnum.DataTable: columns = dt.Columns; BuildXmlListResponse(app, context, method, command, dt, xml, columns); break; case RestServiceMethodReturnTypeEnum.DataRow: columns = dt.Columns; BuildXmlItemResponse(app, context, method, command, dt, xml, columns); break; case RestServiceMethodReturnTypeEnum.Xml: BuildXmlObjectResponse(app, method, doc, xml); break; } } } } //perform any special post processing //string url = ((System.Web.Routing.Route)(RouteData.Route)).Url; //context.Response.Write("From the handler at " + DateTime.Now + " - " + Me.Name + " - " + url + " - " + HttpContext.Current.Request.HttpMethod); //context.Response.ContentType = "text/xml"; //context.Response.Write(dt.DataSet.GetXml()); context.Response.StatusCode = (int)HttpStatusCode.OK; context.Response.Write(builder.ToString()); } catch (CodeTorchException cex) { builder = new StringBuilder(); RestServiceException exception = new RestServiceException(); exception.Status = (int)HttpStatusCode.InternalServerError; exception.Message = cex.Message; exception.MoreInfo = cex.MoreInfo; exception.ErrorCode = cex.ErrorCode; context.Response.TrySkipIisCustomErrors = true; context.Response.StatusCode = exception.Status; SerializeRestException(app, context, builder, writer, exception); } catch (Exception ex) { builder = new StringBuilder(); RestServiceException exception = new RestServiceException(); exception.Status = (int)HttpStatusCode.InternalServerError; exception.Message = ex.Message; context.Response.TrySkipIisCustomErrors = true; context.Response.StatusCode = exception.Status; SerializeRestException(app, context, builder, writer, exception); } }