WebErrorHandler.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / NetFx35 / System.ServiceModel.Web / System / ServiceModel / Dispatcher / WebErrorHandler.cs / 1 / WebErrorHandler.cs

                            //------------------------------------------------------------ 
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------
#pragma warning disable 1634, 1691
namespace System.ServiceModel.Dispatcher 
{
    using System; 
    using System.Collections.Generic; 
    using System.Globalization;
    using System.ServiceModel; 
    using System.Text;
    using System.Xml;
    using System.Net;
    using System.ServiceModel.Channels; 
    using System.ServiceModel.Description;
    using System.ServiceModel.Web; 
    using System.IO; 

    class WebErrorHandler : IErrorHandler 
    {
        bool includeExceptionDetailInFaults;

        public WebErrorHandler(bool includeExceptionDetailInFaults) 
        {
            this.includeExceptionDetailInFaults = includeExceptionDetailInFaults; 
        } 

        public bool HandleError(Exception error) 
        {
            return false;
        }
 
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        { 
            if (version != MessageVersion.None || error == null) 
            {
                return; 
            }
            MemoryStream stream = new MemoryStream();
            WriteErrorPage(stream, error);
            stream.Seek(0, SeekOrigin.Begin); 
            fault = new HttpStreamMessage(stream);
            fault.Properties.Add(WebBodyFormatMessageProperty.Name, WebBodyFormatMessageProperty.RawProperty); 
            HttpResponseMessageProperty responseProperty = new HttpResponseMessageProperty(); 
            responseProperty.StatusCode = HttpStatusCode.BadRequest;
            responseProperty.Headers[HttpResponseHeader.ContentType] = "text/html"; 
            fault.Properties.Add(HttpResponseMessageProperty.Name, responseProperty);
        }

        public void WriteErrorPage(Stream stream, Exception error) 
        {
            string errorText; 
            string stackTrace; 
            if (this.includeExceptionDetailInFaults)
            { 
                errorText = SR2.GetString(SR2.ServerErrorProcessingRequestWithDetails, error.Message);
                stackTrace = error.StackTrace;
            }
            else 
            {
                errorText = SR2.GetString(SR2.ServerErrorProcessingRequest); 
                stackTrace = null; 
            }
            using (XmlWriter writer = XmlWriter.Create(stream)) 
            {
                writer.WriteStartElement("HTML");
                writer.WriteStartElement("HEAD");
                writer.WriteRaw(String.Format(CultureInfo.InvariantCulture, 
                    @"{0}
{1}", 
                    SR2.GetString(SR2.HelpPageLayout), 
                    SR2.GetString(SR2.WebErrorPageTitleText)));
                writer.WriteEndElement(); //HEAD 

                writer.WriteRaw(String.Format(CultureInfo.InvariantCulture,
                    @"

{0}


{1}

{2}

", SR2.GetString(SR2.WebErrorPageTitleText), errorText, stackTrace ?? string.Empty)); writer.WriteEndElement(); //HTML } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK