Esempio n. 1
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);
        }
        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;
        }
Esempio n. 3
0
        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);
        }