protected void HandleServiceException(IHttpRequest httpReq, IHttpResponse httpRes, string operationName, Exception ex, bool throwException = true)
        {
            try
            {
                Operation operation = EndpointHost.MetadataMap[httpReq.ServicePath].OperationNameMap[httpReq.OperationName.ToLower()];

                httpRes.ResponseObject = ErrorUtils.CreateServiceErrorResponse(httpReq, ex, operation.ResponseType);
                httpRes.WriteToResponse(httpReq, httpRes.ResponseObject);
            }
            catch
            {
                var errorMessage = string.Format("Error occured while Processing Request: {0}", ex.Message);
                Log.Error(errorMessage, ex, new Dictionary <string, string>()
                {
                    { "Version", ServiceUtils.SOA2VersionCatName },
                    { "ErrorCode", "FXD300000" },
                    { "Service", EndpointHost.MetadataMap[httpReq.ServicePath].FullServiceName },
                    { "Operation", operationName },
                    { "ErrorClassification", "FrameworkError" }
                });

                if (throwException)
                {
                    throw ex;
                }
            }
            finally
            {
                if (!httpRes.IsClosed)
                {
                    httpRes.AddHeader(ServiceUtils.ResponseStatusHttpHeaderKey, AckCodeType.Failure.ToString());
                }
                httpRes.LogRequest(httpReq);
                httpRes.EndRequest(true);
            }
        }
        public virtual Task ProcessRequestAsync(IHttpRequest httpReq, IHttpResponse httpRes, string operationName)
        {
            TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();

            try
            {
                object requestObject = null;
                if (PreProcessRequestAsync(httpReq, httpRes, operationName, out requestObject))
                {
                    tcs.TrySetResult(null);
                    return(tcs.Task);
                }

                Task task = GetResponse(httpReq, httpRes, requestObject) as Task;

                string identity = EndpointHost.Config.MetadataMap[httpReq.ServicePath].GetOperationByOpName(httpReq.OperationName).Key;
                return(task.ContinueWith(t =>
                {
                    try
                    {
                        object response = null;
                        if (t.Exception != null)
                        {
                            var ex = t.Exception.InnerException ?? t.Exception;
                            if (httpRes != null && httpRes.ExecutionResult != null)
                            {
                                httpRes.ExecutionResult.ExceptionCaught = ex;
                            }

                            Type responseType = EndpointHost.MetadataMap[ServicePath].GetResponseTypeByOpName(operationName);
                            response = ErrorUtils.CreateServiceErrorResponse(httpReq, ex, responseType);
                            httpRes.WriteToResponse(httpReq, response);
                            return;
                        }

                        response = ((Task <object>)t).Result;
                        if (PostProcessRequestAsync(httpReq, httpRes, operationName, response))
                        {
                            return;
                        }

                        httpRes.WriteToResponse(httpReq, response);
                    }
                    catch (Exception ex)
                    {
                        if (httpRes != null && httpRes.ExecutionResult != null)
                        {
                            httpRes.ExecutionResult.ExceptionCaught = ex;
                        }

                        throw;
                    }
                    finally
                    {
                        if (EndpointHost.PostResponseFilters.Count > 0)
                        {
                            EndpointHost.ApplyPostResponseFilters(new PostResponseFilterArgs()
                            {
                                ExecutionResult = httpRes.ExecutionResult,
                                ServicePath = httpReq.ServicePath,
                                OperationName = httpReq.OperationName,
                                RequestDeserializeTimeInMilliseconds = httpReq.DeserializationTimeInMillis,
                                ResponseSerializeTimeInMilliseconds = httpRes.SerializationTimeInMillis
                            });
                        }
                    }
                }));
            }
            catch (Exception ex)
            {
                try
                {
                    Type   responseType = EndpointHost.MetadataMap[ServicePath].GetResponseTypeByOpName(operationName);
                    object response     = ErrorUtils.CreateFrameworkErrorResponse(httpReq, ex, responseType);
                    httpRes.WriteToResponse(httpReq, response);
                    tcs.TrySetResult(null);
                }
                catch (Exception ex1)
                {
                    tcs.TrySetException(ex1);
                }

                return(tcs.Task);
            }
            finally
            {
                HostContext.Instance.EndRequest();
            }
        }