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;
			}
		}