internal override MethodStubInfo GetRequestMethod (HttpContext context) { try { requestMessage = DeserializeRequest (context.Request); return methodInfo; } catch (Exception ex) { SerializeFault (context, requestMessage, ex); return null; } }
public override void ProcessRequest (HttpContext context) { Context = context; SoapServerMessage responseMessage = null; try { if (requestMessage == null) requestMessage = DeserializeRequest (context.Request); responseMessage = Invoke (context, requestMessage); SerializeResponse (context.Response, responseMessage); } catch (Exception ex) { SerializeFault (context, requestMessage, ex); return; } }
public override void ProcessRequest (HttpContext context) { Context = context; SoapServerMessage responseMessage = null; try { if (requestMessage == null) { requestMessage = DeserializeRequest (context); } if (methodInfo != null && methodInfo.OneWay) { context.Response.BufferOutput = false; context.Response.StatusCode = 202; context.Response.Flush (); context.Response.Close (); Invoke (context, requestMessage); } else { responseMessage = Invoke (context, requestMessage); SerializeResponse (context.Response, responseMessage); } } catch (Exception ex) { if (methodInfo != null && methodInfo.OneWay) { context.Response.StatusCode = 500; context.Response.Flush (); context.Response.Close (); } else { SerializeFault (context, requestMessage, ex); } } finally { IDisposable disp = requestMessage.Server as IDisposable; requestMessage = null; if (disp != null) disp.Dispose(); } }
// tries to get to the first child element of body, ignoring details // such as the namespace of Envelope and Body (a version mismatch check will come later) protected XmlQualifiedName GetRequestElement() { SoapServerMessage message = ServerProtocol.Message; long savedPosition = message.Stream.Position; XmlTextReader reader = GetXmlTextReader(message.ContentType, message.Stream); reader.MoveToContent(); requestNamespace = reader.NamespaceURI; reader.ReadStartElement(Soap.Envelope, requestNamespace); reader.MoveToContent(); while (!reader.EOF && !reader.IsStartElement(Soap.Body, requestNamespace)) { reader.Skip(); } if (reader.EOF) { throw new InvalidOperationException(Res.GetString(Res.WebMissingBodyElement)); } XmlQualifiedName element; if (reader.IsEmptyElement) { element = new XmlQualifiedName("", ""); } else { reader.ReadStartElement(Soap.Body, requestNamespace); reader.MoveToContent(); element = new XmlQualifiedName(reader.LocalName, reader.NamespaceURI); } message.Stream.Position = savedPosition; return(element); }
protected virtual SoapServerMethod RouteRequest(SoapServerMessage message) { throw new NotImplementedException(); }
private SoapServerMessage Invoke (HttpContext ctx, SoapServerMessage requestMessage) { SoapMethodStubInfo methodInfo = requestMessage.MethodStubInfo; // Assign header values to web service members requestMessage.UpdateHeaderValues (requestMessage.Server, methodInfo.Headers); // Fill an array with the input parameters at the right position object[] parameters = new object[methodInfo.MethodInfo.Parameters.Length]; ParameterInfo[] inParams = methodInfo.MethodInfo.InParameters; for (int n=0; n<inParams.Length; n++) parameters [inParams[n].Position] = requestMessage.InParameters [n]; // Invoke the method try { object[] results = methodInfo.MethodInfo.Invoke (requestMessage.Server, parameters); requestMessage.OutParameters = results; } catch (TargetInvocationException ex) { throw ex.InnerException; } // Check that headers with MustUnderstand flag have been understood foreach (SoapHeader header in requestMessage.Headers) { if (header.MustUnderstand && !header.DidUnderstand) throw new SoapHeaderException ("Header not understood: " + header.GetType(), SoapException.MustUnderstandFaultCode); } // Collect headers that must be sent to the client requestMessage.CollectHeaders (requestMessage.Server, methodInfo.Headers, SoapHeaderDirection.Out); return requestMessage; }
void SerializeFault (HttpContext context, SoapServerMessage requestMessage, Exception ex) { SoapException soex = ex as SoapException; if (soex == null) soex = new SoapException (ex.Message, SoapException.ServerFaultCode, ex); SoapServerMessage faultMessage; if (requestMessage != null) faultMessage = new SoapServerMessage (context.Request, soex, requestMessage.MethodStubInfo, requestMessage.Server, requestMessage.Stream); else faultMessage = new SoapServerMessage (context.Request, soex, null, null, null); SerializeResponse (context.Response, faultMessage); context.Response.End (); return; }
protected virtual SoapServerMethod RouteRequest (SoapServerMessage message) { throw new NotImplementedException (); }
/// <devdoc> /// Determines which SoapMethod should be invoked for a particular /// message. /// </devdoc> protected virtual SoapServerMethod RouteRequest(SoapServerMessage message) { return helper.RouteRequest(); }
protected virtual XmlReader GetReaderForMessage(SoapServerMessage message, int bufferSize) { Encoding enc = RequestResponseUtils.GetEncoding2(message.ContentType); if (bufferSize < 512) bufferSize = 512; int readTimeout = WebServicesSection.Current.SoapEnvelopeProcessing.ReadTimeout; Int64 timeout = readTimeout < 0 ? 0L : (Int64)readTimeout * 10000000; Int64 nowTicks = DateTime.UtcNow.Ticks; Int64 timeoutTicks = Int64.MaxValue - timeout <= nowTicks ? Int64.MaxValue : nowTicks + timeout; XmlTextReader reader; if (enc != null) { if (timeoutTicks == Int64.MaxValue) { reader = new XmlTextReader(new StreamReader(message.Stream, enc, true, bufferSize)); } else { reader = new SoapEnvelopeReader(new StreamReader(message.Stream, enc, true, bufferSize), timeoutTicks); } } else { if (timeoutTicks == Int64.MaxValue) { reader = new XmlTextReader(message.Stream); } else { reader = new SoapEnvelopeReader(message.Stream, timeoutTicks); } } reader.DtdProcessing = DtdProcessing.Prohibit; reader.Normalization = true; reader.XmlResolver = null; return reader; }
void SerializeResponse(HttpResponse response, SoapServerMessage message) { SoapMethodStubInfo methodInfo = message.MethodStubInfo; if ((message.ContentEncoding != null) && (message.ContentEncoding.Length > 0)) { response.AppendHeader("Content-Encoding", message.ContentEncoding); } response.ContentType = message.IsSoap12 ? "application/soap+xml; charset=utf-8" : "text/xml; charset=utf-8"; if (message.Exception != null) { response.StatusCode = 500; } Stream responseStream = response.OutputStream; Stream outStream = responseStream; bool bufferResponse = (methodInfo == null || methodInfo.MethodAttribute.BufferResponse); response.BufferOutput = bufferResponse; try { // While serializing, process extensions in reverse order if (bufferResponse) { outStream = SoapExtension.ExecuteChainStream(_extensionChainHighPrio, outStream); outStream = SoapExtension.ExecuteChainStream(_extensionChainMedPrio, outStream); outStream = SoapExtension.ExecuteChainStream(_extensionChainLowPrio, outStream); message.SetStage(SoapMessageStage.BeforeSerialize); SoapExtension.ExecuteProcessMessage(_extensionChainLowPrio, message, outStream, true); SoapExtension.ExecuteProcessMessage(_extensionChainMedPrio, message, outStream, true); SoapExtension.ExecuteProcessMessage(_extensionChainHighPrio, message, outStream, true); } XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter(outStream); if (message.Exception == null) { WebServiceHelper.WriteSoapMessage(xtw, methodInfo, SoapHeaderDirection.Out, message.OutParameters, message.Headers, message.IsSoap12); } else if (methodInfo != null) { if (message.IsSoap12) { WebServiceHelper.WriteSoapMessage(xtw, methodInfo, SoapHeaderDirection.Fault, new Soap12Fault(message.Exception), message.Headers, message.IsSoap12); } else { WebServiceHelper.WriteSoapMessage(xtw, methodInfo, SoapHeaderDirection.Fault, new Fault(message.Exception), message.Headers, message.IsSoap12); } } else { if (message.IsSoap12) { WebServiceHelper.WriteSoapMessage(xtw, SoapBindingUse.Literal, Soap12Fault.Serializer, null, new Soap12Fault(message.Exception), null, message.IsSoap12); } else { WebServiceHelper.WriteSoapMessage(xtw, SoapBindingUse.Literal, Fault.Serializer, null, new Fault(message.Exception), null, message.IsSoap12); } } if (bufferResponse) { message.SetStage(SoapMessageStage.AfterSerialize); SoapExtension.ExecuteProcessMessage(_extensionChainLowPrio, message, outStream, true); SoapExtension.ExecuteProcessMessage(_extensionChainMedPrio, message, outStream, true); SoapExtension.ExecuteProcessMessage(_extensionChainHighPrio, message, outStream, true); } xtw.Flush(); } catch (Exception ex) { // If the response is buffered, we can discard the response and // serialize a new Fault message as response. if (bufferResponse) { throw ex; } // If it is not buffered, we can't rollback what has been sent, // so we can only close the connection and return. responseStream.Close(); return; } }
internal override void WriteFault(XmlWriter writer, SoapException soapException, HttpStatusCode statusCode) { if (statusCode != HttpStatusCode.InternalServerError) { return; } if (soapException == null) { return; } SoapServerMessage message = ServerProtocol.Message; writer.WriteStartDocument(); writer.WriteStartElement(Soap.Prefix, Soap.Element.Envelope, Soap.Namespace); writer.WriteAttributeString("xmlns", Soap.Prefix, null, Soap.Namespace); writer.WriteAttributeString("xmlns", "xsi", null, XmlSchema.InstanceNamespace); writer.WriteAttributeString("xmlns", "xsd", null, XmlSchema.Namespace); if (ServerProtocol.ServerMethod != null) { SoapHeaderHandling.WriteHeaders(writer, ServerProtocol.ServerMethod.outHeaderSerializer, message.Headers, ServerProtocol.ServerMethod.outHeaderMappings, SoapHeaderDirection.Fault, ServerProtocol.ServerMethod.use == SoapBindingUse.Encoded, ServerType.serviceNamespace, ServerType.serviceDefaultIsEncoded, Soap.Namespace); } else { SoapHeaderHandling.WriteUnknownHeaders(writer, message.Headers, Soap.Namespace); } writer.WriteStartElement(Soap.Element.Body, Soap.Namespace); writer.WriteStartElement(Soap.Element.Fault, Soap.Namespace); writer.WriteStartElement(Soap.Element.FaultCode, ""); XmlQualifiedName code = TranslateFaultCode(soapException.Code); if (code.Namespace != null && code.Namespace.Length > 0 && writer.LookupPrefix(code.Namespace) == null) { writer.WriteAttributeString("xmlns", "q0", null, code.Namespace); } writer.WriteQualifiedName(code.Name, code.Namespace); writer.WriteEndElement(); // write faultString element with possible "lang" attribute writer.WriteStartElement(Soap.Element.FaultString, ""); if (soapException.Lang != null && soapException.Lang.Length != 0) { writer.WriteAttributeString("xml", Soap.Attribute.Lang, Soap.XmlNamespace, soapException.Lang); } writer.WriteString(ServerProtocol.GenerateFaultString(soapException)); writer.WriteEndElement(); // Only write an actor element if the actor was specified (it's optional for end-points) string actor = soapException.Actor; if (actor.Length > 0) { writer.WriteElementString(Soap.Element.FaultActor, "", actor); } // Only write a FaultDetail element if exception is related to the body (not a header) if (!(soapException is SoapHeaderException)) { if (soapException.Detail == null) { // writer.WriteStartElement(Soap.Element.FaultDetail, ""); writer.WriteEndElement(); } else { soapException.Detail.WriteTo(writer); } } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndElement(); writer.Flush(); }
internal override bool Initialize() { // try to guess the request version so we can handle any exceptions that might come up GuessVersion(); message = new SoapServerMessage(this); onewayInitException = null; serverType = (SoapServerType)GetFromCache(typeof(SoapServerProtocol), Type); if (serverType == null) { lock (Type){ serverType = (SoapServerType)GetFromCache(typeof(SoapServerProtocol), Type); if (serverType == null) { serverType = new SoapServerType(Type, versionsSupported); AddToCache(typeof(SoapServerProtocol), Type, serverType); } } } // We delay throwing any exceptions out of the extension until we determine if the method is one-way or not. Exception extensionException = null; try { message.highPriConfigExtensions = SoapMessage.InitializeExtensions(serverType.HighPriExtensions, serverType.HighPriExtensionInitializers); // For one-way methods we rely on Request.InputStream guaranteeing that the entire request body has arrived message.SetStream(Request.InputStream); Debug.Assert(message.Stream.CanSeek, "Web services SOAP handler assumes a seekable stream."); message.InitExtensionStreamChain(message.highPriConfigExtensions); message.SetStage(SoapMessageStage.BeforeDeserialize); message.ContentType = Request.ContentType; message.ContentEncoding = Request.Headers[ContentType.ContentEncoding]; message.RunExtensions(message.highPriConfigExtensions); } catch (Exception e) { extensionException = e; } // set this here since we might throw before we init the other extensions message.allExtensions = message.highPriConfigExtensions; // maybe the extensions that just ran changed some of the request data so we can make a better version guess GuessVersion(); try { this.serverMethod = helper.RouteRequest(); // the RouteRequest impl should throw an exception if it can't route the request but just in case... if (this.serverMethod == null) { throw new SoapException(Res.GetString(Res.UnableToHandleRequest0), new XmlQualifiedName(Soap.ServerCode, Soap.Namespace)); } } catch (Exception) { if (helper.RequestNamespace != null) { SetHelper(SoapServerProtocolHelper.GetHelper(this, helper.RequestNamespace)); } // version mismatches override other errors CheckHelperVersion(); throw; } this.isOneWay = serverMethod.oneWay; if (extensionException == null) { try { SoapReflectedExtension[] otherReflectedExtensions = (SoapReflectedExtension[])CombineExtensionsHelper(serverMethod.extensions, serverType.LowPriExtensions, typeof(SoapReflectedExtension)); object[] otherInitializers = (object[])CombineExtensionsHelper(serverMethod.extensionInitializers, serverType.LowPriExtensionInitializers, typeof(object)); message.otherExtensions = SoapMessage.InitializeExtensions(otherReflectedExtensions, otherInitializers); message.allExtensions = (SoapExtension[])CombineExtensionsHelper(message.highPriConfigExtensions, message.otherExtensions, typeof(SoapExtension)); } catch (Exception e) { extensionException = e; } } if (extensionException != null) { if (isOneWay) { onewayInitException = extensionException; } else { throw new SoapException(Res.GetString(Res.WebConfigExtensionError), new XmlQualifiedName(Soap.ServerCode, Soap.Namespace), extensionException); } } return(true); }
protected virtual XmlWriter GetWriterForMessage( SoapServerMessage message, int bufferSize) { throw new NotImplementedException(); }
private bool WriteException_TryWriteFault(SoapServerMessage message, Stream outputStream, HttpStatusCode statusCode, bool disableExtensions) { return(true); }
protected virtual SoapServerMethod RouteRequest(SoapServerMessage message) { return(this.helper.RouteRequest()); }
internal override bool Initialize() { this.GuessVersion(); this.message = new SoapServerMessage(this); this.onewayInitException = null; this.serverType = (SoapServerType)base.GetFromCache(typeof(SoapServerProtocol), base.Type); if (this.serverType == null) { lock (ServerProtocol.InternalSyncObject) { this.serverType = (SoapServerType)base.GetFromCache(typeof(SoapServerProtocol), base.Type); if (this.serverType == null) { this.serverType = new SoapServerType(base.Type, this.protocolsSupported); base.AddToCache(typeof(SoapServerProtocol), base.Type, this.serverType); } } } Exception innerException = null; try { this.message.highPriConfigExtensions = SoapMessage.InitializeExtensions(this.serverType.HighPriExtensions, this.serverType.HighPriExtensionInitializers); this.message.highPriConfigExtensions = this.ModifyInitializedExtensions(PriorityGroup.High, this.message.highPriConfigExtensions); this.message.SetStream(base.Request.InputStream); this.message.InitExtensionStreamChain(this.message.highPriConfigExtensions); this.message.SetStage(SoapMessageStage.BeforeDeserialize); this.message.ContentType = base.Request.ContentType; this.message.ContentEncoding = base.Request.Headers["Content-Encoding"]; this.message.RunExtensions(this.message.highPriConfigExtensions, false); innerException = this.message.Exception; } catch (Exception exception2) { if (((exception2 is ThreadAbortException) || (exception2 is StackOverflowException)) || (exception2 is OutOfMemoryException)) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "Initialize", exception2); } innerException = exception2; } this.message.allExtensions = this.message.highPriConfigExtensions; this.GuessVersion(); try { this.serverMethod = this.RouteRequest(this.message); if (this.serverMethod == null) { throw new SoapException(System.Web.Services.Res.GetString("UnableToHandleRequest0"), new XmlQualifiedName("Server", "http://schemas.xmlsoap.org/soap/envelope/")); } } catch (Exception exception3) { if (((exception3 is ThreadAbortException) || (exception3 is StackOverflowException)) || (exception3 is OutOfMemoryException)) { throw; } if (this.helper.RequestNamespace != null) { this.SetHelper(SoapServerProtocolHelper.GetHelper(this, this.helper.RequestNamespace)); } this.CheckHelperVersion(); throw; } this.isOneWay = this.serverMethod.oneWay; if (innerException == null) { try { SoapReflectedExtension[] reflectedExtensions = (SoapReflectedExtension[])CombineExtensionsHelper(this.serverMethod.extensions, this.serverType.LowPriExtensions, typeof(SoapReflectedExtension)); object[] extensionInitializers = (object[])CombineExtensionsHelper(this.serverMethod.extensionInitializers, this.serverType.LowPriExtensionInitializers, typeof(object)); this.message.otherExtensions = SoapMessage.InitializeExtensions(reflectedExtensions, extensionInitializers); this.message.otherExtensions = this.ModifyInitializedExtensions(PriorityGroup.Low, this.message.otherExtensions); this.message.allExtensions = (SoapExtension[])CombineExtensionsHelper(this.message.highPriConfigExtensions, this.message.otherExtensions, typeof(SoapExtension)); } catch (Exception exception4) { if (((exception4 is ThreadAbortException) || (exception4 is StackOverflowException)) || (exception4 is OutOfMemoryException)) { throw; } if (Tracing.On) { Tracing.ExceptionCatch(TraceEventType.Warning, this, "Initialize", exception4); } innerException = exception4; } } if (innerException != null) { if (!this.isOneWay) { if (innerException is SoapException) { throw innerException; } throw SoapException.Create(this.Version, System.Web.Services.Res.GetString("WebConfigExtensionError"), new XmlQualifiedName("Server", "http://schemas.xmlsoap.org/soap/envelope/"), innerException); } this.onewayInitException = innerException; } return(true); }
SoapServerMessage DeserializeRequest (HttpRequest request) { Stream stream = request.InputStream; using (stream) { string soapAction = null; string ctype; Encoding encoding = WebServiceHelper.GetContentEncoding (request.ContentType, out ctype); if (ctype != "text/xml") throw new WebException ("Content is not XML: " + ctype); server = CreateServerInstance (); SoapServerMessage message = new SoapServerMessage (request, server, stream); message.SetStage (SoapMessageStage.BeforeDeserialize); message.ContentType = ctype; message.ContentEncoding = encoding.WebName; // If the routing style is SoapAction, then we can get the method information now // and set it to the SoapMessage if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.SoapAction) { soapAction = request.Headers ["SOAPAction"]; if (soapAction == null) throw new SoapException ("Missing SOAPAction header", SoapException.ClientFaultCode); methodInfo = _typeStubInfo.GetMethodForSoapAction (soapAction); if (methodInfo == null) throw new SoapException ("Server did not recognize the value of HTTP header SOAPAction: " + soapAction, SoapException.ClientFaultCode); message.MethodStubInfo = methodInfo; } // Execute the high priority global extensions. Do not try to execute the medium and // low priority extensions because if the routing style is RequestElement we still // don't have method information _extensionChainHighPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[0]); stream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, stream); SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, false); // If the routing style is RequestElement, try to get the method name from the // stream processed by the high priority extensions if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.RequestElement) { MemoryStream mstream; byte[] buffer = null; if (stream.CanSeek) { buffer = new byte [stream.Length]; for (int n=0; n<stream.Length;) n += stream.Read (buffer, n, (int)stream.Length-n); mstream = new MemoryStream (buffer); } else { buffer = new byte [500]; mstream = new MemoryStream (); int len; while ((len = stream.Read (buffer, 0, 500)) > 0) mstream.Write (buffer, 0, len); mstream.Position = 0; buffer = mstream.ToArray (); } soapAction = ReadActionFromRequestElement (new MemoryStream (buffer), encoding); stream = mstream; methodInfo = (SoapMethodStubInfo) _typeStubInfo.GetMethod (soapAction); message.MethodStubInfo = methodInfo; } // Whatever routing style we used, we should now have the method information. // We can now notify the remaining extensions if (methodInfo == null) throw new SoapException ("Method '" + soapAction + "' not defined in the web service '" + _typeStubInfo.LogicalType.WebServiceName + "'", SoapException.ClientFaultCode); _extensionChainMedPrio = SoapExtension.CreateExtensionChain (methodInfo.SoapExtensions); _extensionChainLowPrio = SoapExtension.CreateExtensionChain (_typeStubInfo.SoapExtensions[1]); stream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, stream); stream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, stream); SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, false); SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, false); // Deserialize the request StreamReader reader = new StreamReader (stream, encoding, false); XmlTextReader xmlReader = new XmlTextReader (reader); try { object content; SoapHeaderCollection headers; WebServiceHelper.ReadSoapMessage (xmlReader, _typeStubInfo, methodInfo.Use, methodInfo.RequestSerializer, out content, out headers); message.InParameters = (object []) content; message.SetHeaders (headers); } catch (Exception ex) { throw new SoapException ("Could not deserialize Soap message", SoapException.ClientFaultCode, ex); } // Notify the extensions after deserialization message.SetStage (SoapMessageStage.AfterDeserialize); SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, false); SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, false); SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, false); xmlReader.Close (); return message; } }
void SerializeFault (HttpContext context, SoapServerMessage requestMessage, Exception ex) { SoapException soex = ex as SoapException; if (soex == null) soex = new SoapException (ex.ToString (), WebServiceHelper.ServerFaultCode (requestMessage != null && requestMessage.IsSoap12), ex); SoapServerMessage faultMessage; if (requestMessage != null) faultMessage = new SoapServerMessage (context.Request, soex, requestMessage.MethodStubInfo, requestMessage.Server, requestMessage.Stream); else faultMessage = new SoapServerMessage (context.Request, soex, null, null, null); #if NET_2_0 object soapVer = context.Items ["WebServiceSoapVersion"]; if (soapVer != null) faultMessage.SetSoapVersion ((SoapProtocolVersion) soapVer); #endif SerializeResponse (context.Response, faultMessage); context.Response.End (); return; }
SoapServerMessage DeserializeRequest(HttpContext context) { HttpRequest request = context.Request; Stream stream = request.InputStream; //using (stream) //{ string soapAction = null; string ctype; Encoding encoding = WebServiceHelper.GetContentEncoding(request.ContentType, out ctype); if (ctype != "text/xml" && ctype != "application/soap+xml") { throw new WebException("Content is not XML: " + ctype); } object server = CreateServerInstance(); SoapServerMessage message = new SoapServerMessage(request, server, stream); message.SetStage(SoapMessageStage.BeforeDeserialize); message.ContentType = ctype; object soapVer = context.Items ["WebServiceSoapVersion"]; if (soapVer != null) { message.SetSoapVersion((SoapProtocolVersion)soapVer); } // If the routing style is SoapAction, then we can get the method information now // and set it to the SoapMessage if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.SoapAction) { string headerName = message.IsSoap12 ? "action" : "SOAPAction"; soapAction = message.IsSoap12 ? WebServiceHelper.GetContextAction(request.ContentType) : request.Headers [headerName]; if (soapAction == null) { if (!message.IsSoap12) { throw new SoapException("Missing SOAPAction header", WebServiceHelper.ClientFaultCode(message.IsSoap12)); } } else { methodInfo = _typeStubInfo.GetMethodForSoapAction(soapAction); if (methodInfo == null) { throw new SoapException("Server did not recognize the value of HTTP header " + headerName + ": " + soapAction, WebServiceHelper.ClientFaultCode(message.IsSoap12)); } message.MethodStubInfo = methodInfo; } } // Execute the high priority global extensions. Do not try to execute the medium and // low priority extensions because if the routing style is RequestElement we still // don't have method information _extensionChainHighPrio = SoapExtension.CreateExtensionChain(_typeStubInfo.SoapExtensions[0]); stream = SoapExtension.ExecuteChainStream(_extensionChainHighPrio, stream); SoapExtension.ExecuteProcessMessage(_extensionChainHighPrio, message, stream, false); // If the routing style is RequestElement, try to get the method name from the // stream processed by the high priority extensions if (_typeStubInfo.RoutingStyle == SoapServiceRoutingStyle.RequestElement || (message.IsSoap12 && soapAction == null)) { MemoryStream mstream; byte[] buffer = null; if (stream.CanSeek) { buffer = new byte [stream.Length]; for (int n = 0; n < stream.Length;) { n += stream.Read(buffer, n, (int)stream.Length - n); } mstream = new MemoryStream(buffer); } else { buffer = new byte [500]; mstream = new MemoryStream(); int len; while ((len = stream.Read(buffer, 0, 500)) > 0) { mstream.Write(buffer, 0, len); } mstream.Position = 0; buffer = mstream.ToArray(); } soapAction = ReadActionFromRequestElement(new MemoryStream(buffer), encoding, message.IsSoap12); stream = mstream; methodInfo = (SoapMethodStubInfo)_typeStubInfo.GetMethod(soapAction); message.MethodStubInfo = methodInfo; } // Whatever routing style we used, we should now have the method information. // We can now notify the remaining extensions if (methodInfo == null) { throw new SoapException("Method '" + soapAction + "' not defined in the web service '" + _typeStubInfo.LogicalType.WebServiceName + "'", WebServiceHelper.ClientFaultCode(message.IsSoap12)); } _extensionChainMedPrio = SoapExtension.CreateExtensionChain(methodInfo.SoapExtensions); _extensionChainLowPrio = SoapExtension.CreateExtensionChain(_typeStubInfo.SoapExtensions[1]); stream = SoapExtension.ExecuteChainStream(_extensionChainMedPrio, stream); stream = SoapExtension.ExecuteChainStream(_extensionChainLowPrio, stream); SoapExtension.ExecuteProcessMessage(_extensionChainMedPrio, message, stream, false); SoapExtension.ExecuteProcessMessage(_extensionChainLowPrio, message, stream, false); // Deserialize the request StreamReader reader = new StreamReader(stream, encoding, false); XmlTextReader xmlReader = new XmlTextReader(reader); try { object content; SoapHeaderCollection headers; WebServiceHelper.ReadSoapMessage(xmlReader, methodInfo, SoapHeaderDirection.In, message.IsSoap12, out content, out headers); message.InParameters = (object [])content; message.SetHeaders(headers); } catch (Exception ex) { throw new SoapException("Could not deserialize Soap message", WebServiceHelper.ClientFaultCode(message.IsSoap12), ex); } // Notify the extensions after deserialization message.SetStage(SoapMessageStage.AfterDeserialize); SoapExtension.ExecuteProcessMessage(_extensionChainHighPrio, message, stream, false); SoapExtension.ExecuteProcessMessage(_extensionChainMedPrio, message, stream, false); SoapExtension.ExecuteProcessMessage(_extensionChainLowPrio, message, stream, false); return(message); //} }
// Write the contents of the incoming SOAP message to the log file. public void WriteInput(SoapServerMessage message) { // Utility method to copy the contents of one stream to another. Copy(oldStream, newStream); FileStream myFileStream = new FileStream(filename, FileMode.Append, FileAccess.Write); StreamWriter myStreamWriter = new StreamWriter(myFileStream); myStreamWriter.WriteLine("================================== Request at " + DateTime.Now); myStreamWriter.WriteLine("The method that has been invoked is : "); myStreamWriter.WriteLine("\t" + message.MethodInfo.Name); myStreamWriter.WriteLine("The contents of the SOAP envelope are : "); myStreamWriter.Flush(); newStream.Position = 0; Copy(newStream, myFileStream); myFileStream.Close(); newStream.Position = 0; }
protected virtual XmlWriter GetWriterForMessage(SoapServerMessage message, int bufferSize) { if (bufferSize < 512) bufferSize = 512; return new XmlTextWriter(new StreamWriter(message.Stream, new UTF8Encoding(false), bufferSize)); /* XmlWriterSettings ws = new XmlWriterSettings(); ws.Encoding = new UTF8Encoding(false); ws.Indent = false; ws.NewLineHandling = NewLineHandling.None; return XmlWriter.Create(message.Stream, ws); */ }
// Write the contents of the outgoing SOAP message to the log file. public void WriteOutput(SoapServerMessage message) { newStream.Position = 0; FileStream myFileStream = new FileStream(filename, FileMode.Append, FileAccess.Write); StreamWriter myStreamWriter = new StreamWriter(myFileStream); myStreamWriter.WriteLine("---------------------------------- Response at " + DateTime.Now); myStreamWriter.Flush(); // Utility method to copy the contents of one stream to another. Copy(newStream, myFileStream); myFileStream.Close(); newStream.Position = 0; Copy(newStream, oldStream); }
internal override bool Initialize() { // try to guess the request version so we can handle any exceptions that might come up GuessVersion(); message = new SoapServerMessage(this); onewayInitException = null; if (null == (serverType = (SoapServerType)GetFromCache(typeof(SoapServerProtocol), Type)) && null == (serverType = (SoapServerType)GetFromCache(typeof(SoapServerProtocol), Type, true))) { lock (InternalSyncObject) { if (null == (serverType = (SoapServerType)GetFromCache(typeof(SoapServerProtocol), Type)) && null == (serverType = (SoapServerType)GetFromCache(typeof(SoapServerProtocol), Type, true))) { bool excludeSchemeHostPortFromCachingKey = this.IsCacheUnderPressure(typeof(SoapServerProtocol), Type); serverType = new SoapServerType(Type, protocolsSupported); AddToCache(typeof(SoapServerProtocol), Type, serverType, excludeSchemeHostPortFromCachingKey); } } } // We delay throwing any exceptions out of the extension until we determine if the method is one-way or not. Exception extensionException = null; try { message.highPriConfigExtensions = SoapMessage.InitializeExtensions(serverType.HighPriExtensions, serverType.HighPriExtensionInitializers); // // Allow derived classes to modify the high priority extensions list. // message.highPriConfigExtensions = ModifyInitializedExtensions(PriorityGroup.High, message.highPriConfigExtensions); // For one-way methods we rely on Request.InputStream guaranteeing that the entire request body has arrived message.SetStream(Request.InputStream); #if DEBUG //Debug.Assert(message.Stream.CanSeek, "Web services SOAP handler assumes a seekable stream."); // use exception in the place of Debug.Assert to avoid throwing asserts from a server process such as aspnet_ewp.exe if (!message.Stream.CanSeek) throw new InvalidOperationException("Non-Seekable stream " + message.Stream.GetType().FullName + " Web services SOAP handler assumes a seekable stream."); #endif message.InitExtensionStreamChain(message.highPriConfigExtensions); message.SetStage(SoapMessageStage.BeforeDeserialize); message.ContentType = Request.ContentType; message.ContentEncoding = Request.Headers[ContentType.ContentEncoding]; message.RunExtensions(message.highPriConfigExtensions, false); extensionException = message.Exception; } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } if (Tracing.On) Tracing.ExceptionCatch(TraceEventType.Warning, this, "Initialize", e); extensionException = e; } // set this here since we might throw before we init the other extensions message.allExtensions = message.highPriConfigExtensions; // maybe the extensions that just ran changed some of the request data so we can make a better version guess GuessVersion(); try { this.serverMethod = RouteRequest(message); // the RouteRequest impl should throw an exception if it can't route the request but just in case... if (this.serverMethod == null) throw new SoapException(Res.GetString(Res.UnableToHandleRequest0), new XmlQualifiedName(Soap.Code.Server, Soap.Namespace)); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } if (helper.RequestNamespace != null) SetHelper(SoapServerProtocolHelper.GetHelper(this, helper.RequestNamespace)); // version mismatches override other errors CheckHelperVersion(); throw; } this.isOneWay = serverMethod.oneWay; if (extensionException == null) { try { SoapReflectedExtension[] otherReflectedExtensions = (SoapReflectedExtension[]) CombineExtensionsHelper(serverMethod.extensions, serverType.LowPriExtensions, typeof(SoapReflectedExtension)); object[] otherInitializers = (object[]) CombineExtensionsHelper(serverMethod.extensionInitializers, serverType.LowPriExtensionInitializers, typeof(object)); message.otherExtensions = SoapMessage.InitializeExtensions(otherReflectedExtensions, otherInitializers); // // Allow derived classes to modify the other extensions list. // message.otherExtensions = ModifyInitializedExtensions(PriorityGroup.Low, message.otherExtensions); message.allExtensions = (SoapExtension[]) CombineExtensionsHelper(message.highPriConfigExtensions, message.otherExtensions, typeof(SoapExtension)); } catch (Exception e) { if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) { throw; } if (Tracing.On) Tracing.ExceptionCatch(TraceEventType.Warning, this, "Initialize", e); extensionException = e; } } if (extensionException != null) { if (isOneWay) onewayInitException = extensionException; else if (extensionException is SoapException) throw extensionException; else throw SoapException.Create(Version, Res.GetString(Res.WebConfigExtensionError), new XmlQualifiedName(Soap.Code.Server, Soap.Namespace), extensionException); } return true; }
void SerializeResponse (HttpResponse response, SoapServerMessage message) { SoapMethodStubInfo methodInfo = message.MethodStubInfo; response.ContentType = "text/xml; charset=utf-8"; if (message.Exception != null) response.StatusCode = 500; Stream responseStream = response.OutputStream; Stream outStream = responseStream; bool bufferResponse = (methodInfo == null || methodInfo.MethodAttribute.BufferResponse); response.BufferOutput = bufferResponse; try { // While serializing, process extensions in reverse order if (bufferResponse) { outStream = SoapExtension.ExecuteChainStream (_extensionChainHighPrio, outStream); outStream = SoapExtension.ExecuteChainStream (_extensionChainMedPrio, outStream); outStream = SoapExtension.ExecuteChainStream (_extensionChainLowPrio, outStream); message.SetStage (SoapMessageStage.BeforeSerialize); SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, true); SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, true); SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, true); } XmlTextWriter xtw = WebServiceHelper.CreateXmlWriter (outStream); if (message.Exception == null) WebServiceHelper.WriteSoapMessage (xtw, _typeStubInfo, methodInfo.Use, methodInfo.ResponseSerializer, message.OutParameters, message.Headers); else WebServiceHelper.WriteSoapMessage (xtw, _typeStubInfo, SoapBindingUse.Literal, Fault.Serializer, new Fault (message.Exception), null); if (bufferResponse) { message.SetStage (SoapMessageStage.AfterSerialize); SoapExtension.ExecuteProcessMessage (_extensionChainLowPrio, message, true); SoapExtension.ExecuteProcessMessage (_extensionChainMedPrio, message, true); SoapExtension.ExecuteProcessMessage (_extensionChainHighPrio, message, true); } xtw.Flush (); } catch (Exception ex) { // If the response is buffered, we can discard the response and // serialize a new Fault message as response. if (bufferResponse) throw ex; // If it is not buffered, we can't rollback what has been sent, // so we can only close the connection and return. responseStream.Close (); return; } }
bool WriteException_TryWriteFault(SoapServerMessage message, Stream outputStream, HttpStatusCode statusCode, bool disableExtensions) { return true; }
protected virtual XmlWriter GetWriterForMessage ( SoapServerMessage message, int bufferSize) { throw new NotImplementedException (); }