internal override object[] ReadParameters() {
            message.InitExtensionStreamChain(message.otherExtensions);
            message.RunExtensions(message.otherExtensions, true);
 
            // do a sanity check on the content-type before we check the version since otherwise the error might be really nasty
            if (!ContentType.IsSoap(message.ContentType))
                throw new SoapException(Res.GetString(Res.WebRequestContent, message.ContentType, helper.HttpContentType), 
                    new XmlQualifiedName(Soap.Code.Client, Soap.Namespace), new SoapFaultSubCode(Soap12FaultCodes.UnsupportedMediaTypeFaultCode));

            // now that all the extensions have run, establish the real version of the request
            XmlReader reader = null;
            try {
                reader = GetXmlReader();
                reader.MoveToContent();
                SetHelper(SoapServerProtocolHelper.GetHelper(this, reader.NamespaceURI));
            }
            catch (XmlException e) {
                throw new SoapException(Res.GetString(Res.WebRequestUnableToRead), new XmlQualifiedName(Soap.Code.Client, Soap.Namespace), e);
            }
            CheckHelperVersion();

            // now do a more specific content-type check for soap 1.1 only (soap 1.2 allows various xml content types)
            if (version == SoapProtocolVersion.Soap11 && !ContentType.MatchesBase(message.ContentType, helper.HttpContentType))
                throw new SoapException(Res.GetString(Res.WebRequestContent, message.ContentType, helper.HttpContentType), 
                    new XmlQualifiedName(Soap.Code.Client, Soap.Namespace), new SoapFaultSubCode(Soap12FaultCodes.UnsupportedMediaTypeFaultCode));

            if (message.Exception != null) {
                throw message.Exception;
            }
            try {
                if (!reader.IsStartElement(Soap.Element.Envelope, helper.EnvelopeNs))
                    throw new InvalidOperationException(Res.GetString(Res.WebMissingEnvelopeElement));

                if (reader.IsEmptyElement)
                    throw new InvalidOperationException(Res.GetString(Res.WebMissingBodyElement));

                int depth = reader.Depth;

                reader.ReadStartElement(Soap.Element.Envelope, helper.EnvelopeNs);
                reader.MoveToContent();

                // run time check for R2738 A MESSAGE MUST include all soapbind:headers specified on a wsdl:input or wsdl:output of a wsdl:operationwsdl:binding that describes it. 
                bool checkRequiredHeaders = (this.serverMethod.wsiClaims & WsiProfiles.BasicProfile1_1) != 0 && version != SoapProtocolVersion.Soap12;
                string missingHeader = new SoapHeaderHandling().ReadHeaders(reader, serverMethod.inHeaderSerializer, message.Headers, serverMethod.inHeaderMappings, SoapHeaderDirection.In, helper.EnvelopeNs, serverMethod.use == SoapBindingUse.Encoded ? helper.EncodingNs : null, checkRequiredHeaders);
                        
                if (missingHeader != null) {
                    throw new SoapHeaderException(Res.GetString(Res.WebMissingHeader, missingHeader), 
                        new XmlQualifiedName(Soap.Code.MustUnderstand, Soap.Namespace));
                }

                if (!reader.IsStartElement(Soap.Element.Body, helper.EnvelopeNs))
                    throw new InvalidOperationException(Res.GetString(Res.WebMissingBodyElement));

                reader.ReadStartElement(Soap.Element.Body, helper.EnvelopeNs);
                reader.MoveToContent();

                object[] values;
                bool isEncodedSoap = serverMethod.use == SoapBindingUse.Encoded;
                TraceMethod caller = Tracing.On ? new TraceMethod(this, "ReadParameters") : null;
                if (Tracing.On) Tracing.Enter(Tracing.TraceId(Res.TraceReadRequest), caller, new TraceMethod(serverMethod.parameterSerializer, "Deserialize", reader, serverMethod.use == SoapBindingUse.Encoded ? helper.EncodingNs : null));

                bool useDeserializationEvents = !isEncodedSoap && (WebServicesSection.Current.SoapEnvelopeProcessing.IsStrict || Tracing.On);
                if (useDeserializationEvents) {
                    XmlDeserializationEvents events = Tracing.On ? Tracing.GetDeserializationEvents() : RuntimeUtils.GetDeserializationEvents();
                    values = (object[])serverMethod.parameterSerializer.Deserialize(reader, null, events);
                }
                else {
                    values = (object[])serverMethod.parameterSerializer.Deserialize(reader, isEncodedSoap ? helper.EncodingNs : null);
                }
                if (Tracing.On) Tracing.Exit(Tracing.TraceId(Res.TraceReadRequest), caller);

                // Consume soap:Body and soap:Envelope closing tags
                while (depth < reader.Depth && reader.Read()) {
                    // Nothing, just read on
                }
                // consume end tag
                if (reader.NodeType == XmlNodeType.EndElement) {
                    reader.Read();
                }

                message.SetParameterValues(values);

                return values;
            }
            catch (SoapException) {
                throw;
            }
            catch (Exception e) {
                if (e is ThreadAbortException || e is StackOverflowException || e is OutOfMemoryException) {
                    throw;
                }
                throw new SoapException(Res.GetString(Res.WebRequestUnableToRead), new XmlQualifiedName(Soap.Code.Client, Soap.Namespace), e);
            }
        }
        object[] ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, bool asyncCall) {
            SoapClientMethod method = message.Method;

            // 


            HttpWebResponse httpResponse = response as HttpWebResponse;
            int statusCode = httpResponse != null ? (int)httpResponse.StatusCode : -1;
            if (statusCode >= 300 && statusCode != 500 && statusCode != 400)
                throw new WebException(RequestResponseUtils.CreateResponseExceptionString(httpResponse, responseStream), null, 
                    WebExceptionStatus.ProtocolError, httpResponse);

            message.Headers.Clear();
            message.SetStream(responseStream);
            message.InitExtensionStreamChain(message.initializedExtensions);

            message.SetStage(SoapMessageStage.BeforeDeserialize);
            message.ContentType = response.ContentType;
            message.ContentEncoding = response.Headers[ContentType.ContentEncoding];
            message.RunExtensions(message.initializedExtensions, false);

            if (method.oneWay && (httpResponse == null || (int)httpResponse.StatusCode != 500)) {
                return new object[0];
            }

            // this statusCode check is just so we don't repeat the contentType check we did above
            bool isSoap = ContentType.IsSoap(message.ContentType);
            if (!isSoap || (isSoap && (httpResponse != null) && (httpResponse.ContentLength == 0))) {
                // special-case 400 since we exempted it above on the off-chance it might be a soap 1.2 sender fault. 
                // based on the content-type, it looks like it's probably just a regular old 400
                if (statusCode == 400) 
                    throw new WebException(RequestResponseUtils.CreateResponseExceptionString(httpResponse, responseStream), null, 
                        WebExceptionStatus.ProtocolError, httpResponse);
                else
                    throw new InvalidOperationException(Res.GetString(Res.WebResponseContent, message.ContentType, HttpContentType) +
                                    Environment.NewLine +
                                    RequestResponseUtils.CreateResponseExceptionString(response, responseStream));
            }
            if (message.Exception != null) {
                throw message.Exception;
            }

            // perf fix: changed buffer size passed to StreamReader
            int bufferSize;
            if (asyncCall || httpResponse == null)
                bufferSize = 512;
            else {
                bufferSize = RequestResponseUtils.GetBufferSize((int)httpResponse.ContentLength);
            }
            XmlReader reader = GetReaderForMessage(message, bufferSize);
            if (reader == null)
                throw new InvalidOperationException(Res.GetString(Res.WebNullReaderForMessage));

            reader.MoveToContent();
            int depth = reader.Depth;

            // should be able to handle no ns, soap 1.1 ns, or soap 1.2 ns
            string encodingNs = EncodingNs;
            string envelopeNs = reader.NamespaceURI;

            if (envelopeNs == null || envelopeNs.Length == 0)
                // ok to omit namespace -- assume correct version
                reader.ReadStartElement(Soap.Element.Envelope);
            else if (reader.NamespaceURI == Soap.Namespace)
                reader.ReadStartElement(Soap.Element.Envelope, Soap.Namespace);
            else if (reader.NamespaceURI == Soap12.Namespace)
                reader.ReadStartElement(Soap.Element.Envelope, Soap12.Namespace);
            else
                throw new SoapException(Res.GetString(Res.WebInvalidEnvelopeNamespace, envelopeNs, EnvelopeNs), SoapException.VersionMismatchFaultCode);

            reader.MoveToContent();
            SoapHeaderHandling headerHandler = new SoapHeaderHandling();
            headerHandler.ReadHeaders(reader, method.outHeaderSerializer, message.Headers, method.outHeaderMappings, SoapHeaderDirection.Out | SoapHeaderDirection.Fault, envelopeNs, method.use == SoapBindingUse.Encoded ? encodingNs : null, false);
            reader.MoveToContent();
            reader.ReadStartElement(Soap.Element.Body, envelopeNs);
            reader.MoveToContent();
            if (reader.IsStartElement(Soap.Element.Fault, envelopeNs)) {
                message.Exception = ReadSoapException(reader);
            } 
            else {
                if (method.oneWay) {
                    reader.Skip();
                    message.SetParameterValues(new object[0]);
                }
                else {
                    TraceMethod caller = Tracing.On ? new TraceMethod(this, "ReadResponse") : null;
                    bool isEncodedSoap = method.use == SoapBindingUse.Encoded;
                    if (Tracing.On) Tracing.Enter(Tracing.TraceId(Res.TraceReadResponse), caller, new TraceMethod(method.returnSerializer, "Deserialize", reader, isEncodedSoap ? encodingNs : null));

                    bool useDeserializationEvents = !isEncodedSoap && (WebServicesSection.Current.SoapEnvelopeProcessing.IsStrict || Tracing.On);
                    if (useDeserializationEvents) {
                        XmlDeserializationEvents events = Tracing.On ? Tracing.GetDeserializationEvents() : RuntimeUtils.GetDeserializationEvents();
                        message.SetParameterValues((object[])method.returnSerializer.Deserialize(reader, null, events));
                    }
                    else {
                        message.SetParameterValues((object[])method.returnSerializer.Deserialize(reader, isEncodedSoap ? encodingNs : null));
                    }

                    if (Tracing.On) Tracing.Exit(Tracing.TraceId(Res.TraceReadResponse), caller);
                }
            }

            // Consume soap:Body and soap:Envelope closing tags
            while (depth < reader.Depth && reader.Read()) {
                // Nothing, just read on
            }
            // consume end tag
            if (reader.NodeType == XmlNodeType.EndElement) {
                reader.Read();
            }

            message.SetStage(SoapMessageStage.AfterDeserialize);
            message.RunExtensions(message.initializedExtensions, false);
            SoapHeaderHandling.SetHeaderMembers(message.Headers, this, method.outHeaderMappings, SoapHeaderDirection.Out | SoapHeaderDirection.Fault, true);

            if (message.Exception != null) throw message.Exception;
            return message.GetParameterValues();        
        }
        SoapClientMessage BeforeSerialize(WebRequest request, string methodName, object[] parameters)
        {
            if (parameters == null)
            {
                throw new ArgumentNullException("parameters");
            }
            SoapClientMethod method = clientType.GetMethod(methodName);

            if (method == null)
            {
                throw new ArgumentException(Res.GetString(Res.WebInvalidMethodName, methodName));
            }

            // Run BeforeSerialize extension pass. Extensions are not allowed
            // to write into the stream during this pass.
            SoapReflectedExtension[] allExtensions     = (SoapReflectedExtension[])CombineExtensionsHelper(clientType.HighPriExtensions, method.extensions, clientType.LowPriExtensions, typeof(SoapReflectedExtension));
            object[]          allExtensionInitializers = (object[])CombineExtensionsHelper(clientType.HighPriExtensionInitializers, method.extensionInitializers, clientType.LowPriExtensionInitializers, typeof(object));
            SoapExtension[]   initializedExtensions    = SoapMessage.InitializeExtensions(allExtensions, allExtensionInitializers);
            SoapClientMessage message = new SoapClientMessage(this, method, Url);

            message.initializedExtensions = initializedExtensions;
            if (initializedExtensions != null)
            {
                message.SetExtensionStream(new SoapExtensionStream());
            }
            message.InitExtensionStreamChain(message.initializedExtensions);

            string soapAction = UrlEncoder.EscapeString(method.action, Encoding.UTF8);

            message.SetStage(SoapMessageStage.BeforeSerialize);
            if (this.version == SoapProtocolVersion.Soap12)
            {
                message.ContentType = ContentType.Compose(ContentType.ApplicationSoap, RequestEncoding != null ? RequestEncoding : Encoding.UTF8, soapAction);
            }
            else
            {
                message.ContentType = ContentType.Compose(ContentType.TextXml, RequestEncoding != null ? RequestEncoding : Encoding.UTF8);
            }
            message.SetParameterValues(parameters);
            SoapHeaderHandling.GetHeaderMembers(message.Headers, this, method.inHeaderMappings, SoapHeaderDirection.In, true);
            message.RunExtensions(message.initializedExtensions, true);

            // Last chance to set request headers
            request.ContentType = message.ContentType;
            if (message.ContentEncoding != null && message.ContentEncoding.Length > 0)
            {
                request.Headers[ContentType.ContentEncoding] = message.ContentEncoding;
            }

            request.Method = "POST";
            if (this.version != SoapProtocolVersion.Soap12 && request.Headers[Soap.Action] == null)
            {
                StringBuilder actionStringBuilder = new StringBuilder(soapAction.Length + 2);
                actionStringBuilder.Append('"');
                actionStringBuilder.Append(soapAction);
                actionStringBuilder.Append('"');
                request.Headers.Add(Soap.Action, actionStringBuilder.ToString());
            }

            return(message);
        }
Example #4
0
        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);
        }
Example #5
0
        internal override void WriteReturns(object[] returnValues, Stream outputStream)
        {
            if (serverMethod.oneWay)
            {
                return;
            }
            bool isEncoded = serverMethod.use == SoapBindingUse.Encoded;

            SoapHeaderHandling.EnsureHeadersUnderstood(message.Headers);
            message.Headers.Clear();
            SoapHeaderHandling.GetHeaderMembers(message.Headers, this.Target, serverMethod.outHeaderMappings, SoapHeaderDirection.Out, false);

            if (message.allExtensions != null)
            {
                message.SetExtensionStream(new SoapExtensionStream());
            }

            message.InitExtensionStreamChain(message.allExtensions);

            message.SetStage(SoapMessageStage.BeforeSerialize);
            message.ContentType = ContentType.Compose(helper.HttpContentType, Encoding.UTF8);
            message.SetParameterValues(returnValues);
            message.RunExtensions(message.allExtensions);

            message.SetStream(outputStream);
            Response.ContentType = message.ContentType;
            if (message.ContentEncoding != null && message.ContentEncoding.Length > 0)
            {
                Response.AppendHeader(ContentType.ContentEncoding, message.ContentEncoding);
            }

            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.WriteStartDocument();
            writer.WriteStartElement("soap", Soap.Envelope, helper.EnvelopeNs);
            writer.WriteAttributeString("xmlns", "soap", null, helper.EnvelopeNs);
            if (isEncoded)
            {
                writer.WriteAttributeString("xmlns", "soapenc", null, helper.EncodingNs);
                writer.WriteAttributeString("xmlns", "tns", null, serverType.serviceNamespace);
                writer.WriteAttributeString("xmlns", "types", null, SoapReflector.GetEncodedNamespace(serverType.serviceNamespace, serverType.serviceDefaultIsEncoded));
            }
            if (serverMethod.rpc && version == SoapProtocolVersion.Soap12)
            {
                writer.WriteAttributeString("xmlns", "rpc", null, Soap12.RpcNamespace);
            }
            writer.WriteAttributeString("xmlns", "xsi", null, XmlSchema.InstanceNamespace);
            writer.WriteAttributeString("xmlns", "xsd", null, XmlSchema.Namespace);
            SoapHeaderHandling.WriteHeaders(writer, serverMethod.outHeaderSerializer, message.Headers, serverMethod.outHeaderMappings, SoapHeaderDirection.Out, isEncoded, serverType.serviceNamespace, serverType.serviceDefaultIsEncoded, helper.EnvelopeNs);
            writer.WriteStartElement(Soap.Body, helper.EnvelopeNs);
            if (isEncoded)
            {
                writer.WriteAttributeString("soap", Soap.EncodingStyle, null, helper.EncodingNs);
            }
            // SOAP12: not using encodingStyle
            //serverMethod.returnSerializer.Serialize(writer, returnValues, null, isEncoded ? helper.EncodingNs : null);
            serverMethod.returnSerializer.Serialize(writer, returnValues, null);
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.Flush();

            message.SetStage(SoapMessageStage.AfterSerialize);
            message.RunExtensions(message.allExtensions);
        }
Example #6
0
        private object[] ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, bool asyncCall)
        {
            SoapClientMethod method          = message.Method;
            HttpWebResponse  httpWebResponse = response as HttpWebResponse;
            int num1 = httpWebResponse != null ? (int)httpWebResponse.StatusCode : -1;

            if (num1 >= 300 && num1 != 500 && num1 != 400)
            {
                throw new WebException(RequestResponseUtils.CreateResponseExceptionString((WebResponse)httpWebResponse, responseStream), (Exception)null, WebExceptionStatus.ProtocolError, (WebResponse)httpWebResponse);
            }
            message.Headers.Clear();
            message.SetStream(responseStream);
            message.InitExtensionStreamChain(message.initializedExtensions);
            message.SetStage(SoapMessageStage.BeforeDeserialize);
            message.ContentType     = response.ContentType;
            message.ContentEncoding = ((NameValueCollection)response.Headers)["Content-Encoding"];
            message.RunExtensions(message.initializedExtensions, false);
            if (method.oneWay && (httpWebResponse == null || httpWebResponse.StatusCode != HttpStatusCode.InternalServerError))
            {
                return(new object[0]);
            }
            bool flag1 = ContentType.IsSoap(message.ContentType);

            if (!flag1 || flag1 && httpWebResponse != null && httpWebResponse.ContentLength == 0L)
            {
                if (num1 == 400)
                {
                    throw new WebException(RequestResponseUtils.CreateResponseExceptionString((WebResponse)httpWebResponse, responseStream), (Exception)null, WebExceptionStatus.ProtocolError, (WebResponse)httpWebResponse);
                }
                throw new InvalidOperationException(System.Web.Services.Res.GetString("WebResponseContent", (object)message.ContentType, (object)this.HttpContentType) + Environment.NewLine + RequestResponseUtils.CreateResponseExceptionString(response, responseStream));
            }
            else
            {
                if (message.Exception != null)
                {
                    throw message.Exception;
                }
                int       bufferSize       = asyncCall || httpWebResponse == null ? 512 : RequestResponseUtils.GetBufferSize((int)httpWebResponse.ContentLength);
                XmlReader readerForMessage = this.GetReaderForMessage(message, bufferSize);
                if (readerForMessage == null)
                {
                    throw new InvalidOperationException(System.Web.Services.Res.GetString("WebNullReaderForMessage"));
                }
                int    num2         = (int)readerForMessage.MoveToContent();
                int    depth        = readerForMessage.Depth;
                string encodingNs   = this.EncodingNs;
                string namespaceUri = readerForMessage.NamespaceURI;
                if (namespaceUri == null || namespaceUri.Length == 0)
                {
                    readerForMessage.ReadStartElement("Envelope");
                }
                else if (readerForMessage.NamespaceURI == "http://schemas.xmlsoap.org/soap/envelope/")
                {
                    readerForMessage.ReadStartElement("Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
                }
                else if (readerForMessage.NamespaceURI == "http://www.w3.org/2003/05/soap-envelope")
                {
                    readerForMessage.ReadStartElement("Envelope", "http://www.w3.org/2003/05/soap-envelope");
                }
                else
                {
                    throw new SoapException(System.Web.Services.Res.GetString("WebInvalidEnvelopeNamespace", (object)namespaceUri, (object)this.EnvelopeNs), SoapException.VersionMismatchFaultCode);
                }
                int num3 = (int)readerForMessage.MoveToContent();
                new SoapHeaderHandling().ReadHeaders(readerForMessage, method.outHeaderSerializer, message.Headers, method.outHeaderMappings, SoapHeaderDirection.Out | SoapHeaderDirection.Fault, namespaceUri, method.use == SoapBindingUse.Encoded ? encodingNs : (string)null, false);
                int num4 = (int)readerForMessage.MoveToContent();
                readerForMessage.ReadStartElement("Body", namespaceUri);
                int num5 = (int)readerForMessage.MoveToContent();
                if (readerForMessage.IsStartElement("Fault", namespaceUri))
                {
                    message.Exception = this.ReadSoapException(readerForMessage);
                }
                else if (method.oneWay)
                {
                    readerForMessage.Skip();
                    message.SetParameterValues(new object[0]);
                }
                else
                {
                    TraceMethod caller = Tracing.On ? new TraceMethod((object)this, "ReadResponse", new object[0]) : (TraceMethod)null;
                    bool        flag2  = method.use == SoapBindingUse.Encoded;
                    if (Tracing.On)
                    {
                        Tracing.Enter(Tracing.TraceId("TraceReadResponse"), caller, new TraceMethod((object)method.returnSerializer, "Deserialize", new object[2]
                        {
                            (object)readerForMessage,
                            flag2 ? (object)encodingNs : (object)(string)null
                        }));
                    }
                    if (!flag2 && (WebServicesSection.Current.SoapEnvelopeProcessing.IsStrict || Tracing.On))
                    {
                        XmlDeserializationEvents events = Tracing.On ? Tracing.GetDeserializationEvents() : RuntimeUtils.GetDeserializationEvents();
                        message.SetParameterValues((object[])method.returnSerializer.Deserialize(readerForMessage, (string)null, events));
                    }
                    else
                    {
                        message.SetParameterValues((object[])method.returnSerializer.Deserialize(readerForMessage, flag2 ? encodingNs : (string)null));
                    }
                    if (Tracing.On)
                    {
                        Tracing.Exit(Tracing.TraceId("TraceReadResponse"), caller);
                    }
                }
                do
                {
                    ;
                }while (depth < readerForMessage.Depth && readerForMessage.Read());
                if (readerForMessage.NodeType == XmlNodeType.EndElement)
                {
                    readerForMessage.Read();
                }
                message.SetStage(SoapMessageStage.AfterDeserialize);
                message.RunExtensions(message.initializedExtensions, false);
                SoapHeaderHandling.SetHeaderMembers(message.Headers, (object)this, method.outHeaderMappings, SoapHeaderDirection.Out | SoapHeaderDirection.Fault, true);
                if (message.Exception != null)
                {
                    throw message.Exception;
                }
                else
                {
                    return(message.GetParameterValues());
                }
            }
        }
Example #7
0
        private object[] ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, bool asyncCall)
        {
            int bufferSize;
            SoapClientMethod method    = message.Method;
            HttpWebResponse  response2 = response as HttpWebResponse;
            int num = (response2 != null) ? ((int)response2.StatusCode) : -1;

            if (((num >= 300) && (num != 500)) && (num != 400))
            {
                throw new WebException(RequestResponseUtils.CreateResponseExceptionString(response2, responseStream), null, WebExceptionStatus.ProtocolError, response2);
            }
            message.Headers.Clear();
            message.SetStream(responseStream);
            message.InitExtensionStreamChain(message.initializedExtensions);
            message.SetStage(SoapMessageStage.BeforeDeserialize);
            message.ContentType     = response.ContentType;
            message.ContentEncoding = response.Headers["Content-Encoding"];
            message.RunExtensions(message.initializedExtensions, false);
            if (method.oneWay && ((response2 == null) || (response2.StatusCode != HttpStatusCode.InternalServerError)))
            {
                return(new object[0]);
            }
            bool flag = ContentType.IsSoap(message.ContentType);

            if (!flag || ((flag && (response2 != null)) && (response2.ContentLength == 0L)))
            {
                if (num == 400)
                {
                    throw new WebException(RequestResponseUtils.CreateResponseExceptionString(response2, responseStream), null, WebExceptionStatus.ProtocolError, response2);
                }
                throw new InvalidOperationException(System.Web.Services.Res.GetString("WebResponseContent", new object[] { message.ContentType, this.HttpContentType }) + Environment.NewLine + RequestResponseUtils.CreateResponseExceptionString(response, responseStream));
            }
            if (message.Exception != null)
            {
                throw message.Exception;
            }
            if (asyncCall || (response2 == null))
            {
                bufferSize = 0x200;
            }
            else
            {
                bufferSize = RequestResponseUtils.GetBufferSize((int)response2.ContentLength);
            }
            XmlReader readerForMessage = this.GetReaderForMessage(message, bufferSize);

            if (readerForMessage == null)
            {
                throw new InvalidOperationException(System.Web.Services.Res.GetString("WebNullReaderForMessage"));
            }
            readerForMessage.MoveToContent();
            int    depth        = readerForMessage.Depth;
            string encodingNs   = this.EncodingNs;
            string namespaceURI = readerForMessage.NamespaceURI;

            if ((namespaceURI == null) || (namespaceURI.Length == 0))
            {
                readerForMessage.ReadStartElement("Envelope");
            }
            else if (readerForMessage.NamespaceURI == "http://schemas.xmlsoap.org/soap/envelope/")
            {
                readerForMessage.ReadStartElement("Envelope", "http://schemas.xmlsoap.org/soap/envelope/");
            }
            else
            {
                if (readerForMessage.NamespaceURI != "http://www.w3.org/2003/05/soap-envelope")
                {
                    throw new SoapException(System.Web.Services.Res.GetString("WebInvalidEnvelopeNamespace", new object[] { namespaceURI, this.EnvelopeNs }), SoapException.VersionMismatchFaultCode);
                }
                readerForMessage.ReadStartElement("Envelope", "http://www.w3.org/2003/05/soap-envelope");
            }
            readerForMessage.MoveToContent();
            new SoapHeaderHandling().ReadHeaders(readerForMessage, method.outHeaderSerializer, message.Headers, method.outHeaderMappings, SoapHeaderDirection.Fault | SoapHeaderDirection.Out, namespaceURI, (method.use == SoapBindingUse.Encoded) ? encodingNs : null, false);
            readerForMessage.MoveToContent();
            readerForMessage.ReadStartElement("Body", namespaceURI);
            readerForMessage.MoveToContent();
            if (readerForMessage.IsStartElement("Fault", namespaceURI))
            {
                message.Exception = this.ReadSoapException(readerForMessage);
            }
            else if (method.oneWay)
            {
                readerForMessage.Skip();
                message.SetParameterValues(new object[0]);
            }
            else
            {
                TraceMethod caller = Tracing.On ? new TraceMethod(this, "ReadResponse", new object[0]) : null;
                bool        flag2  = method.use == SoapBindingUse.Encoded;
                if (Tracing.On)
                {
                    Tracing.Enter(Tracing.TraceId("TraceReadResponse"), caller, new TraceMethod(method.returnSerializer, "Deserialize", new object[] { readerForMessage, flag2 ? encodingNs : null }));
                }
                if (!flag2 && (WebServicesSection.Current.SoapEnvelopeProcessing.IsStrict || Tracing.On))
                {
                    XmlDeserializationEvents events = Tracing.On ? Tracing.GetDeserializationEvents() : RuntimeUtils.GetDeserializationEvents();
                    message.SetParameterValues((object[])method.returnSerializer.Deserialize(readerForMessage, null, events));
                }
                else
                {
                    message.SetParameterValues((object[])method.returnSerializer.Deserialize(readerForMessage, flag2 ? encodingNs : null));
                }
                if (Tracing.On)
                {
                    Tracing.Exit(Tracing.TraceId("TraceReadResponse"), caller);
                }
            }
            while ((depth < readerForMessage.Depth) && readerForMessage.Read())
            {
            }
            if (readerForMessage.NodeType == XmlNodeType.EndElement)
            {
                readerForMessage.Read();
            }
            message.SetStage(SoapMessageStage.AfterDeserialize);
            message.RunExtensions(message.initializedExtensions, false);
            SoapHeaderHandling.SetHeaderMembers(message.Headers, this, method.outHeaderMappings, SoapHeaderDirection.Fault | SoapHeaderDirection.Out, true);
            if (message.Exception != null)
            {
                throw message.Exception;
            }
            return(message.GetParameterValues());
        }