/* * internal bool IsInitialized { * get { return serverMethod != null; } * } */ internal override void CreateServerInstance() { base.CreateServerInstance(); message.SetStage(SoapMessageStage.AfterDeserialize); message.RunExtensions(message.allExtensions); SoapHeaderHandling.SetHeaderMembers(message.Headers, this.Target, serverMethod.inHeaderMappings, SoapHeaderDirection.In, false); }
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; 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; } }
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); //} }
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 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; } }
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); }