コード例 #1
0
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            WebException webException;

            if (error is WebException)
            {
                webException = (WebException)error;
            }
            else
            {
                var    behavior = GetWebErrorHandlerConfiguration();
                string unhandledErrorMessage;

                if (behavior.ReturnRawException)
                {
                    unhandledErrorMessage = error.ToString();
                }
                else
                {
                    unhandledErrorMessage = (behavior != null && behavior.UnhandledErrorMessage != null) ?
                                            behavior.UnhandledErrorMessage :
                                            "An error has occured processing your request.";
                }

                webException = new WebException(error,
                                                System.Net.HttpStatusCode.InternalServerError,
                                                unhandledErrorMessage);
            }

            webException.UpdateHeaders(WebOperationContext.Current.OutgoingResponse.Headers);
            WebOperationContext.Current.OutgoingResponse.StatusCode        = webException.Status;
            WebOperationContext.Current.OutgoingResponse.StatusDescription = webException.Status.ToString();

            WebDispatchFormatter webDispatchFormatter = null;

            if (OperationContext.Current.OutgoingMessageProperties.ContainsKey(WebDispatchFormatter.WebDispatcherFormatterProperty))
            {
                webDispatchFormatter = OperationContext.Current.OutgoingMessageProperties[WebDispatchFormatter.WebDispatcherFormatterProperty] as WebDispatchFormatter;
            }

            if (webDispatchFormatter != null)
            {
                var behavior = GetWebErrorHandlerConfiguration();
                IWebExceptionDataContract exceptionContract = null;

                if (behavior != null)
                {
                    exceptionContract = behavior.CreateExceptionDataContract();
                }
                if (exceptionContract == null)
                {
                    exceptionContract = new WebExceptionContract();
                }

                exceptionContract.Init(webException);

                fault = webDispatchFormatter.Serialize(
                    exceptionContract,
                    typeof(WebExceptionContract),
                    WebOperationContext.Current.IncomingRequest.GetAcceptTypes());
            }

            else
            {
                WebOperationContext.Current.OutgoingResponse.ContentType = MediaTypeNames.Text.Html;

                fault = Message.CreateMessage(
                    MessageVersion.None,
                    null,
                    new BinaryBodyWriter(GenerateResponseText(webException.Message)));

                fault.SetWebContentFormatProperty(WebContentFormat.Raw);
            }

            fault.UpdateHttpProperty();

            if (OperationContainer.Exists())
            {
                DependencyResolver.Current.OperationError(OperationContainer.GetCurrent(), error);
            }
        }