internal override bool WriteException(Exception e, Stream outputStream) { SoapException exception; if (this.message == null) { return(false); } this.message.Headers.Clear(); if ((this.serverMethod != null) && (this.Target != null)) { SoapHeaderHandling.GetHeaderMembers(this.message.Headers, this.Target, this.serverMethod.outHeaderMappings, SoapHeaderDirection.Fault, false); } if (e is SoapException) { exception = (SoapException)e; } else if (((this.serverMethod != null) && this.serverMethod.rpc) && ((this.helper.Version == SoapProtocolVersion.Soap12) && (e is ArgumentException))) { exception = SoapException.Create(this.Version, System.Web.Services.Res.GetString("WebRequestUnableToProcess"), new XmlQualifiedName("Client", "http://schemas.xmlsoap.org/soap/envelope/"), null, null, null, new SoapFaultSubCode(Soap12FaultCodes.RpcBadArgumentsFaultCode), e); } else { exception = SoapException.Create(this.Version, System.Web.Services.Res.GetString("WebRequestUnableToProcess"), new XmlQualifiedName("Server", "http://schemas.xmlsoap.org/soap/envelope/"), e); } if (SoapException.IsVersionMismatchFaultCode(exception.Code) && this.IsSupported(WebServiceProtocols.HttpSoap12)) { SoapUnknownHeader header = this.CreateUpgradeHeader(); if (header != null) { this.Message.Headers.Add(header); } } base.Response.ClearHeaders(); base.Response.Clear(); HttpStatusCode statusCode = this.helper.SetResponseErrorCode(base.Response, exception); bool flag = false; SoapExtensionStream extensionStream = new SoapExtensionStream(); if (this.message.allExtensions != null) { this.message.SetExtensionStream(extensionStream); } try { this.message.InitExtensionStreamChain(this.message.allExtensions); } catch (Exception exception2) { if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException)) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "WriteException", exception2); } flag = true; } this.message.SetStage(SoapMessageStage.BeforeSerialize); this.message.ContentType = ContentType.Compose(this.helper.HttpContentType, Encoding.UTF8); this.message.Exception = exception; if (!flag) { try { this.message.RunExtensions(this.message.allExtensions, false); } catch (Exception exception3) { if (((exception3 is ThreadAbortException) || (exception3 is StackOverflowException)) || (exception3 is OutOfMemoryException)) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "WriteException", exception3); } flag = true; } } this.message.SetStream(outputStream); base.Response.ContentType = this.message.ContentType; if ((this.message.ContentEncoding != null) && (this.message.ContentEncoding.Length > 0)) { base.Response.AppendHeader("Content-Encoding", this.message.ContentEncoding); } XmlWriter writerForMessage = this.GetWriterForMessage(this.message, 0x200); if (writerForMessage == null) { throw new InvalidOperationException(System.Web.Services.Res.GetString("WebNullWriterForMessage")); } this.helper.WriteFault(writerForMessage, this.message.Exception, statusCode); if (!flag) { SoapException exception4 = null; try { this.message.SetStage(SoapMessageStage.AfterSerialize); this.message.RunExtensions(this.message.allExtensions, false); } catch (Exception exception5) { if (((exception5 is ThreadAbortException) || (exception5 is StackOverflowException)) || (exception5 is OutOfMemoryException)) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "WriteException", exception5); } if (!extensionStream.HasWritten) { exception4 = SoapException.Create(this.Version, System.Web.Services.Res.GetString("WebExtensionError"), new XmlQualifiedName("Server", "http://schemas.xmlsoap.org/soap/envelope/"), exception5); } } if (exception4 != null) { base.Response.ContentType = ContentType.Compose("text/plain", Encoding.UTF8); StreamWriter writer2 = new StreamWriter(outputStream, new UTF8Encoding(false)); writer2.WriteLine(base.GenerateFaultString(this.message.Exception)); writer2.Flush(); } } return(true); }
internal override bool WriteException(Exception e, Stream outputStream) { if (message == null) { return(false); } message.Headers.Clear(); if (serverMethod != null) { SoapHeaderHandling.GetHeaderMembers(message.Headers, this.Target, serverMethod.outHeaderMappings, SoapHeaderDirection.Fault, false); } SoapException soapException; if (e is SoapException) { soapException = (SoapException)e; } else if (serverMethod != null && serverMethod.rpc && helper.Version == SoapProtocolVersion.Soap12 && e is ArgumentException) { // special case to handle soap 1.2 rpc "BadArguments" fault soapException = new SoapException(Res.GetString(Res.WebRequestUnableToProcess), new XmlQualifiedName(Soap.ClientCode, Soap.Namespace), null, null, null, new SoapFaultSubcode(Soap12FaultCodes.RpcBadArgumentsFaultCode), e); } else { soapException = new SoapException(Res.GetString(Res.WebRequestUnableToProcess), new XmlQualifiedName(Soap.ServerCode, Soap.Namespace), e); } if (SoapException.IsVersionMismatchFaultCode(soapException.Code)) { if (IsSupported(ProtocolsEnum.HttpSoap12)) { SoapUnknownHeader unknownHeader = CreateUpgradeHeader(); if (unknownHeader != null) { Message.Headers.Add(unknownHeader); } } } Response.ClearHeaders(); Response.Clear(); helper.SetResponseErrorCode(Response, soapException); bool disableExtensions = false; if (message.allExtensions != null) { message.SetExtensionStream(new SoapExtensionStream()); } try { message.InitExtensionStreamChain(message.allExtensions); } catch (Exception) { disableExtensions = true; } message.SetStage(SoapMessageStage.BeforeSerialize); message.ContentType = ContentType.Compose(helper.HttpContentType, Encoding.UTF8); message.SetException(soapException); if (!disableExtensions) { try { message.RunExtensions(message.allExtensions); } catch (Exception) { disableExtensions = true; } } message.SetStream(outputStream); Response.ContentType = message.ContentType; if (message.ContentEncoding != null && message.ContentEncoding.Length > 0) { Response.AppendHeader(ContentType.ContentEncoding, message.ContentEncoding); } bool isEncoded = serverMethod != null && serverMethod.use == SoapBindingUse.Encoded; StreamWriter sw = new StreamWriter(message.Stream, new UTF8Encoding(false), 128); XmlTextWriter writer = isEncoded && message.Headers.Count > 0 ? new XmlSpecialTextWriter(sw, helper.Version) : new XmlTextWriter(sw); writer.Formatting = Formatting.Indented; // CONSIDER, don't format to save space writer.Indentation = 2; // CONSIDER, don't indent to save space helper.WriteFault(writer, soapException); if (!disableExtensions) { try { message.SetStage(SoapMessageStage.AfterSerialize); message.RunExtensions(message.allExtensions); } catch (Exception) { // it's too late to do anything about this -- we've already written to the stream } } return(true); }