/// <summary>
        /// Generic method that handles processing a Callback request by routing to
        /// a method in a provided target object.
        ///
        /// </summary>
        /// <param name="target">The target object that is to be called. If null this is used</param>
        public void ProcessCallbackMethodCall(object target, string methodToCall)
        {
            if (target == null)
            {
                target = this;
            }

            HttpRequest  Request  = HttpContext.Current.Request;
            HttpResponse Response = HttpContext.Current.Response;

            Response.Charset = null;

            // check for Route Data method name
            if (target is CallbackHandler)
            {
                var routeData = ((CallbackHandler)target).RouteData;
                if (routeData != null)
                {
                    methodToCall = ((CallbackHandlerRouteHandler)routeData.RouteHandler).MethodName;
                }
            }

            CallbackMethodProcessorHelper helper = new CallbackMethodProcessorHelper(this);

            List <string> parameterList = null;

            string contentType = Request.ContentType.ToLower();

            // Allow for a single JSON object to be POSTed rather than POST variables
            if (contentType.StartsWith(WebResources.STR_JavaScriptContentType) ||
                contentType.StartsWith(WebResources.STR_JsonContentType))
            {
                if (string.IsNullOrEmpty(methodToCall))
                {
                    methodToCall = Request.Params["Method"];
                }

                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.", null);
                    return;
                }

                // Pass a Parameter List with our JSON encoded parameters
                parameterList = new List <string>();

                if (Request.ContentLength > 0L)
                {
                    // Pick up single unencoded JSON parameter
                    StreamReader sr         = new StreamReader(Request.InputStream);
                    string       singleParm = sr.ReadToEnd();
                    sr.Close();
                    sr.Dispose();

                    if (!string.IsNullOrEmpty(singleParm))
                    {
                        parameterList.Add(singleParm);
                    }
                }
            }
            // Post AjaxMethodCallback style interface
            else if (contentType.StartsWith(WebResources.STR_UrlEncodedContentType) && Request.Params["CallbackMethod"] != null)
            {
                // Only pick up the method name - Parameters are parsed out of POST buffer during method calling
                methodToCall = Request.Params["CallbackMethod"];
            }
            else
            {
                JsonPMethod = Request.QueryString["jsonp"] ?? Request.QueryString["callback"];

                // Check for querystring method parameterList
                if (string.IsNullOrEmpty(methodToCall))
                {
                    methodToCall = Request.QueryString["Method"];
                }

                // No method - no can do
                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.", null);
                    return;
                }
            }

            // Explicitly set the content type here - set here so the method
            // can override it if it so desires
            Response.ContentType = WebResources.STR_JsonContentType;

            object result                = null;
            string stringResult          = null;
            CallbackMethodAttribute attr = new CallbackMethodAttribute();

            try
            {
                if (parameterList != null)
                {
                    // use the supplied parameter list
                    result = helper.ExecuteMethod(methodToCall, target, parameterList.ToArray(),
                                                  CallbackMethodParameterType.Json, ref attr);
                }
                else
                {
                    // grab the info out of QueryString Values or POST buffer during parameter parsing
                    // for optimization
                    result = helper.ExecuteMethod(methodToCall, target, null,
                                                  CallbackMethodParameterType.Json, ref attr);
                }
            }
            catch (Exception ex)
            {
                Exception activeException = DebugUtils.GetInnerMostException(ex);
                WriteErrorResponse(activeException.Message,
                                   (HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null));
                return;
            }

            // Special return type handling: Stream, Bitmap, byte[] and raw string results
            // are converted and returned directly
            HandleSpecialReturnTypes(result, attr, Request, Response);

            // Standard json formatting
            try
            {
                JSONSerializer Serializer = new JSONSerializer();
                Serializer.DateSerializationMode = JsonDateEncoding;

                // In debug mode show nicely formatted JSON
                // In release normal packed JSON is used
                if (HttpContext.Current.IsDebuggingEnabled)
                {
                    Serializer.FormatJsonOutput = true;
                }

                stringResult = Serializer.Serialize(result);
            }
            catch (Exception ex)
            {
                WriteErrorResponse(ex.Message, HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null);
                return;
            }


            if (!string.IsNullOrEmpty(JsonPMethod))
            {
                stringResult = JsonPMethod + "( " + stringResult + " );";
            }

            Response.Write(stringResult);
            Response.End();
        }
        /// <summary>
        /// Generic method that handles processing a Callback request by routing to
        /// a method in a provided target object.
        /// 
        /// </summary>
        /// <param name="target">The target object that is to be called. If null this is used</param>
        public void ProcessCallbackMethodCall(object target, string methodToCall)
        {
            if (target == null)
                target = this;

            HttpRequest Request = HttpContext.Current.Request;
            HttpResponse Response = HttpContext.Current.Response;
            Response.Charset = null;

            CallbackMethodProcessorHelper helper = new CallbackMethodProcessorHelper(this);

            List<string> ParameterList = null;

            string contentType = Request.ContentType.ToLower();

            // check for Route Data method name
            if (string.IsNullOrEmpty(methodToCall) && target is CallbackHandler)
            {
                CallbackHandler chandler = target as CallbackHandler;
                if (chandler.RouteData != null)
                    methodToCall = ((CallbackHandlerRouteHandler)chandler.RouteData.RouteHandler).MethodName;
            }

            // Allow for a single XML object to be POSTed rather than POST variables
            if ( contentType.StartsWith(WebResources.STR_XmlContentType) )
            {
                if (string.IsNullOrEmpty(methodToCall))
                    methodToCall = Request.Params["Method"];

                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.",null);
                    return;
                }

                // Pass a Parameter List with our JSON encoded parameters
                ParameterList = new List<string>();

                if (Request.ContentLength > 0L)
                {
                    // Pick up single unencoded JSON parameter
                    string singleParm = WebUtils.FormBufferToString();

                    if (!string.IsNullOrEmpty(singleParm))
                        ParameterList.Add(singleParm);
                }
            }
            // Post AjaxMethodCallback style interface
            else if (contentType.StartsWith(WebResources.STR_UrlEncodedContentType) && Request.Params["CallbackMethod"] != null)
                // Only pick up the method name - Parameters are parsed out of POST buffer during method calling
                methodToCall = Request.Params["CallbackMethod"];
            else
            {
                if (string.IsNullOrEmpty(methodToCall))
                    methodToCall = Request.QueryString["Method"];

                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.",null);
                    return;
                }
            }

            object Result = null;
            string StringResult = null;
            CallbackMethodAttribute attr = new CallbackMethodAttribute();
            try
            {
                if (ParameterList != null)
                    // use the supplied parameter list
                    Result = helper.ExecuteMethod(methodToCall,target, ParameterList.ToArray(),
                                                  CallbackMethodParameterType.Xml, ref attr);
                else
                    // grab the info out of QueryString Values or POST buffer during parameter parsing
                    // for optimization
                    Result = helper.ExecuteMethod(methodToCall, target, null,
                                                  CallbackMethodParameterType.Xml,ref attr);
            }
            catch (Exception ex)
            {
                Exception ActiveException = null;
                if (ex.InnerException != null)
                    ActiveException = ex.InnerException;
                else
                    ActiveException = ex;

                WriteErrorResponse(ActiveException.Message,
                                  ( HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null ) );
                return;
            }

            // Special return type handling: Stream, Bitmap, byte[] and raw string results
            // are converted and returned directly
            HandleSpecialReturnTypes(Result, attr, Request, Response);

            // Standard json formatting
            try
            {
                SerializationUtils.SerializeObject(Result, out StringResult);
            }
            catch (Exception ex)
            {
                WriteErrorResponse(ex.Message, HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null);
                return;
            }

            // Explicitly set the content type here
            Response.ContentType = WebResources.STR_XmlContentType;

            Response.Write(StringResult);
            Response.End();
        }
        /// <summary>
        /// Generic method that handles processing a Callback request by routing to
        /// a method in a provided target object.
        ///
        /// </summary>
        /// <param name="target">The target object that is to be called. If null this is used</param>
        public void ProcessCallbackMethodCall(object target, string methodToCall)
        {
            if (target == null)
            {
                target = this;
            }

            HttpRequest  Request  = HttpContext.Current.Request;
            HttpResponse Response = HttpContext.Current.Response;

            Response.Charset = null;

            CallbackMethodProcessorHelper helper = new CallbackMethodProcessorHelper(this);

            List <string> ParameterList = null;

            string contentType = Request.ContentType.ToLower();


            // check for Route Data method name
            if (string.IsNullOrEmpty(methodToCall) && target is CallbackHandler)
            {
                CallbackHandler chandler = target as CallbackHandler;
                if (chandler.RouteData != null)
                {
                    methodToCall = ((CallbackHandlerRouteHandler)chandler.RouteData.RouteHandler).MethodName;
                }
            }

            // Allow for a single XML object to be POSTed rather than POST variables
            if (contentType.StartsWith(WebResources.STR_XmlContentType))
            {
                if (string.IsNullOrEmpty(methodToCall))
                {
                    methodToCall = Request.Params["Method"];
                }

                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.", null);
                    return;
                }

                // Pass a Parameter List with our JSON encoded parameters
                ParameterList = new List <string>();

                if (Request.ContentLength > 0L)
                {
                    // Pick up single unencoded JSON parameter
                    string singleParm = WebUtils.FormBufferToString();

                    if (!string.IsNullOrEmpty(singleParm))
                    {
                        ParameterList.Add(singleParm);
                    }
                }
            }
            // Post AjaxMethodCallback style interface
            else if (contentType.StartsWith(WebResources.STR_UrlEncodedContentType) && Request.Params["CallbackMethod"] != null)
            {
                // Only pick up the method name - Parameters are parsed out of POST buffer during method calling
                methodToCall = Request.Params["CallbackMethod"];
            }
            else
            {
                if (string.IsNullOrEmpty(methodToCall))
                {
                    methodToCall = Request.QueryString["Method"];
                }

                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.", null);
                    return;
                }
            }

            object Result                = null;
            string StringResult          = null;
            CallbackMethodAttribute attr = new CallbackMethodAttribute();

            try
            {
                if (ParameterList != null)
                {
                    // use the supplied parameter list
                    Result = helper.ExecuteMethod(methodToCall, target, ParameterList.ToArray(),
                                                  CallbackMethodParameterType.Xml, ref attr);
                }
                else
                {
                    // grab the info out of QueryString Values or POST buffer during parameter parsing
                    // for optimization
                    Result = helper.ExecuteMethod(methodToCall, target, null,
                                                  CallbackMethodParameterType.Xml, ref attr);
                }
            }
            catch (Exception ex)
            {
                Exception ActiveException = null;
                if (ex.InnerException != null)
                {
                    ActiveException = ex.InnerException;
                }
                else
                {
                    ActiveException = ex;
                }

                WriteErrorResponse(ActiveException.Message,
                                   (HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null));
                return;
            }

            // Special return type handling: Stream, Bitmap, byte[] and raw string results
            // are converted and returned directly
            HandleSpecialReturnTypes(Result, attr, Request, Response);

            // Standard json formatting
            try
            {
                SerializationUtils.SerializeObject(Result, out StringResult);
            }
            catch (Exception ex)
            {
                WriteErrorResponse(ex.Message, HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null);
                return;
            }


            // Explicitly set the content type here
            Response.ContentType = WebResources.STR_XmlContentType;

            Response.Write(StringResult);
            Response.End();
        }
        /// <summary>
        /// Generic method that handles processing a Callback request by routing to
        /// a method in a provided target object.
        /// 
        /// </summary>
        /// <param name="target">The target object that is to be called. If null this is used</param>
        public void ProcessCallbackMethodCall(object target, string methodToCall)
        {
            if (target == null)
                target = this;

            HttpRequest Request = HttpContext.Current.Request;
            HttpResponse Response = HttpContext.Current.Response;
            Response.Charset = null;

            // check for Route Data method name
            if (target is CallbackHandler)
            {
                var routeData = ((CallbackHandler)target).RouteData;                
                if (routeData != null)
                    methodToCall = ((CallbackHandlerRouteHandler)routeData.RouteHandler).MethodName;
            }

            CallbackMethodProcessorHelper helper = new CallbackMethodProcessorHelper(this);
            
            List<string> parameterList = null;
            
            string contentType = Request.ContentType.ToLower();

            // Allow for a single JSON object to be POSTed rather than POST variables
            if (contentType.StartsWith(WebResources.STR_JavaScriptContentType) ||
                contentType.StartsWith(WebResources.STR_JsonContentType))
            {
                if (string.IsNullOrEmpty(methodToCall))
                    methodToCall = Request.Params["Method"];

                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.",null);
                    return;
                }

                // Pass a Parameter List with our JSON encoded parameters
                parameterList = new List<string>();

                if (Request.ContentLength > 0L)
                {
                    // Pick up single unencoded JSON parameter
                    string singleParm;
                    using (StreamReader sr = new StreamReader(Request.InputStream))
                    {
                        singleParm = sr.ReadToEnd();
                    }

                    if (!string.IsNullOrEmpty(singleParm))
                        parameterList.Add(singleParm);
                }
            }
            // Post AjaxMethodCallback style interface            
            else if (contentType.StartsWith(WebResources.STR_UrlEncodedContentType) && Request.Params["CallbackMethod"] != null)
                // Only pick up the method name - Parameters are parsed out of POST buffer during method calling
                methodToCall = Request.Params["CallbackMethod"];                
            else
            {
                JsonPMethod = Request.QueryString["jsonp"] ?? Request.QueryString["callback"];

                // Check for querystring method parameterList
                if (string.IsNullOrEmpty(methodToCall)) 
                    methodToCall = Request.QueryString["Method"];               

                // No method - no can do
                if (string.IsNullOrEmpty(methodToCall))
                {
                    WriteErrorResponse("No method to call specified.",null);
                    return;
                }
            }

            // Explicitly set the content type here - set here so the method 
            // can override it if it so desires
            Response.ContentType = WebResources.STR_JsonContentType;
            
            object result = null;
            string stringResult = null;
            CallbackMethodAttribute attr = new CallbackMethodAttribute();
            try
            {
                if (parameterList != null)
                    // use the supplied parameter list
                    result = helper.ExecuteMethod(methodToCall, target, parameterList.ToArray(),
                        CallbackMethodParameterType.Json, ref attr);
                else
                // grab the info out of QueryString Values or POST buffer during parameter parsing 
                // for optimization
                    result = helper.ExecuteMethod(methodToCall, target, null,
                        CallbackMethodParameterType.Json, ref attr);
            }
            catch (CallbackException ex)
            {
                WriteErrorResponse(ex.Message,
                                  (HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null),
                                  ex.StatusCode);
            }
            catch (Exception ex)
            {                
                WriteErrorResponse(ex.GetBaseException().Message,
                                  ( HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null ) );
                return;
            }

            // Special return type handling: Stream, Bitmap, byte[] and raw string results
            // are converted and returned directly
            HandleSpecialReturnTypes(result, attr, Request, Response);
            
            // Standard json formatting            
            try
            {
                JSONSerializer Serializer = new JSONSerializer();
                Serializer.DateSerializationMode = JsonDateEncoding;               

                // In debug mode show nicely formatted JSON 
                // In release normal packed JSON is used
                if (HttpContext.Current.IsDebuggingEnabled)
                    Serializer.FormatJsonOutput = true;

                if (result == null)
                    stringResult = "null";
                else
                    stringResult = Serializer.Serialize(result);
            }            
            catch (Exception ex)
            {
                WriteErrorResponse(ex.Message, HttpContext.Current.IsDebuggingEnabled ? ex.StackTrace : null);
                return;
            }
            
           
            if (!string.IsNullOrEmpty(JsonPMethod))            
                stringResult = JsonPMethod + "( " + stringResult + " );";
            
            Response.Write(stringResult);
            Response.End();
        }