public void ProcessRequest(HttpContext context) { object returnObject = null; ReturnData returnData = new ReturnData(); try { if (StoredProcedureName == null || StoredProcedureName == string.Empty) { throw new ArgumentException("StoredProcedureName must be set in handler"); } if (PreExecution != null) { PreExecutionResult result = PreExecution(context); if (result != PreExecutionResult.NoErrors) { throw new PreExecutionFailException(result, "PreExecution Failed"); } } // Get the JSON that was posted string json; using (Stream st = context.Request.InputStream) { byte[] buf = new byte[context.Request.InputStream.Length]; int iRead = st.Read(buf, 0, buf.Length); json = Encoding.UTF8.GetString(buf); } //Deserialize the Javascript JavaScriptSerializer ser = new JavaScriptSerializer(); Dictionary <string, object> ajaxData = (Dictionary <string, object>)ser.Deserialize <Dictionary <string, object> >(json); TID id = (TID)Convert.ChangeType(ajaxData["Id"], typeof(TID)); SqlDataAccessBasic <TID, object> dataAccess = new SqlDataAccessBasic <TID, object>(ConnectionString); dataAccess.IdCall(id, StoredProcedureName); returnData.IsSuccessful = true; if (BusinessLogic != null) { returnObject = BusinessLogic(returnData); } else { returnObject = returnData; } } catch (Exception ex) { //The child class of this class should implement an ErrorHandling funciton. //Error message should be logged and a more user friendly error message should be sent in the string ErrorMessage //CallingMethod should be logged and cleared before being serialized into JSON returnData = new ReturnData { ErrorMessage = ex.ToString(), CallingMethod = this.GetType().Name, IsSuccessful = false }; if (ErrorHandling != null) { returnObject = ErrorHandling(returnData, ex); } else { returnObject = returnData; } } //Convert the results to JSON JavaScriptSerializer jss = new JavaScriptSerializer(); string JSON = jss.Serialize(returnObject); //Send JSON string back to browser context.Response.ContentType = "application/json"; context.Response.Write(JSON); }
public void TestIdCall() { SqlDataAccessBasic <int, Test> dataAccess = new SqlDataAccessBasic <int, Test>(cConnectionString); dataAccess.IdCall(1, "Test.IdCallTest"); }