Example #1
0
 private bool ApplyResponseFilters <TResponse>(object response)
 {
     if (EndpointHost.ApplyResponseFilters(httpReq, httpRes, response))
     {
         ThrowIfError <TResponse>(httpRes);
         return(true);
     }
     return(false);
 }
Example #2
0
        protected Message ExecuteMessage(Message requestMsg, EndpointAttributes endpointAttributes)
        {
            if ((EndpointAttributes.Soap11 & this.HandlerAttributes) == EndpointAttributes.Soap11)
            {
                EndpointHost.Config.AssertFeatures(Feature.Soap11);
            }
            else if ((EndpointAttributes.Soap12 & this.HandlerAttributes) == EndpointAttributes.Soap12)
            {
                EndpointHost.Config.AssertFeatures(Feature.Soap12);
            }

            string requestXml;

            using (var reader = requestMsg.GetReaderAtBodyContents())
            {
                requestXml = reader.ReadOuterXml();
            }

            var requestType = GetRequestType(requestMsg, requestXml);

            try
            {
                var request = DataContractDeserializer.Instance.Parse(requestXml, requestType);

                IHttpRequest  httpReq            = null;
                IHttpResponse httpRes            = null;
                var           hasRequestFilters  = EndpointHost.RequestFilters.Count > 0;
                var           hasResponseFilters = EndpointHost.ResponseFilters.Count > 0;
                if (hasRequestFilters || hasResponseFilters)
                {
                    httpReq = HttpContext.Current != null
                                                ? new HttpRequestWrapper(requestType.Name, HttpContext.Current.Request)
                                                : null;
                    httpRes = HttpContext.Current != null
                                                ? new HttpResponseWrapper(HttpContext.Current.Response)
                                                : null;
                }

                EndpointHost.ApplyRequestFilters(httpReq, httpRes, request);

                var response = ExecuteService(request, endpointAttributes, null);

                EndpointHost.ApplyResponseFilters(httpReq, httpRes, response);

                return(requestMsg.Headers.Action == null
                                        ? Message.CreateMessage(requestMsg.Version, null, response)
                                        : Message.CreateMessage(requestMsg.Version, requestType.Name + "Response", response));
            }
            catch (Exception ex)
            {
                throw new SerializationException("3) Error trying to deserialize requestType: "
                                                 + requestType
                                                 + ", xml body: " + requestXml, ex);
            }
        }
 private bool ApplyResponseFilters(object response)
 {
     if (EndpointHost.ApplyResponseFilters(httpReq, httpRes, response))
     {
         if (httpRes.StatusCode >= 400)
         {
             throw new WebServiceException("WebServiceException, StatusCode: " + httpRes.StatusCode)
                   {
                       ResponseDto = response,
                       StatusCode  = httpRes.StatusCode,
                   };
         }
         return(true);
     }
     return(false);
 }
Example #4
0
        protected Message ExecuteMessage(Message message, EndpointAttributes endpointAttributes, IHttpRequest httpRequest, IHttpResponse httpResponse)
        {
            var soapFeature = endpointAttributes.ToSoapFeature();

            EndpointHost.Config.AssertFeatures(soapFeature);

            var httpReq = HttpContext.Current != null && httpRequest == null
                    ? new HttpRequestWrapper(HttpContext.Current.Request)
                    : httpRequest;
            var httpRes = HttpContext.Current != null && httpResponse == null
                ? new HttpResponseWrapper(HttpContext.Current.Response)
                : httpResponse;

            if (httpReq == null)
            {
                throw new ArgumentNullException("httpRequest");
            }

            if (httpRes == null)
            {
                throw new ArgumentNullException("httpResponse");
            }

            if (EndpointHost.ApplyPreRequestFilters(httpReq, httpRes))
            {
                return(PrepareEmptyResponse(message, httpReq));
            }

            var    requestMsg  = message ?? GetRequestMessageFromStream(httpReq.InputStream);
            string requestXml  = GetRequestXml(requestMsg);
            var    requestType = GetRequestType(requestMsg, requestXml);

            if (!EndpointHost.Metadata.CanAccess(endpointAttributes, soapFeature.ToFormat(), requestType.Name))
            {
                throw EndpointHost.Config.UnauthorizedAccess(endpointAttributes);
            }

            try
            {
                var useXmlSerializerRequest = requestType.HasAttribute <XmlSerializerFormatAttribute>();

                var request = useXmlSerializerRequest
                                  ? XmlSerializableDeserializer.Instance.Parse(requestXml, requestType)
                                  : DataContractDeserializer.Instance.Parse(requestXml, requestType);

                var requiresSoapMessage = request as IRequiresSoapMessage;
                if (requiresSoapMessage != null)
                {
                    requiresSoapMessage.Message = requestMsg;
                }

                httpReq.OperationName = requestType.Name;
                httpReq.SetItem("SoapMessage", requestMsg);

                var hasRequestFilters = EndpointHost.RequestFilters.Count > 0 ||
                                        FilterAttributeCache.GetRequestFilterAttributes(request.GetType()).Any();

                if (hasRequestFilters && EndpointHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(EmptyResponse(requestMsg, requestType));
                }

                var response = ExecuteService(request, endpointAttributes, httpReq, httpRes);

                var hasResponseFilters = EndpointHost.ResponseFilters.Count > 0 ||
                                         FilterAttributeCache.GetResponseFilterAttributes(response.GetType()).Any();

                if (hasResponseFilters && EndpointHost.ApplyResponseFilters(httpReq, httpRes, response))
                {
                    return(EmptyResponse(requestMsg, requestType));
                }

                var httpResult = response as IHttpResult;
                if (httpResult != null)
                {
                    response = httpResult.Response;
                }

                var useXmlSerializerResponse = response.GetType().HasAttribute <XmlSerializerFormatAttribute>();

                if (useXmlSerializerResponse)
                {
                    return(requestMsg.Headers.Action == null
                        ? Message.CreateMessage(requestMsg.Version, null, response, new XmlSerializerWrapper(response.GetType()))
                        : Message.CreateMessage(requestMsg.Version, requestType.Name + "Response", response, new XmlSerializerWrapper(response.GetType())));
                }

                return(requestMsg.Headers.Action == null
                    ? Message.CreateMessage(requestMsg.Version, null, response)
                    : Message.CreateMessage(requestMsg.Version, requestType.Name + "Response", response));
            }
            catch (Exception ex)
            {
                throw new SerializationException("3) Error trying to deserialize requestType: "
                                                 + requestType
                                                 + ", xml body: " + requestXml, ex);
            }
        }
Example #5
0
        protected Message ExecuteMessage(Message requestMsg, EndpointAttributes endpointAttributes, IHttpRequest httpRequest, IHttpResponse httpResponse)
        {
            var soapFeatue = endpointAttributes.ToSoapFeature();

            EndpointHost.Config.AssertFeatures(soapFeatue);

            string requestXml;

            using (var reader = requestMsg.GetReaderAtBodyContents())
            {
                requestXml = reader.ReadOuterXml();
            }

            var requestType = GetRequestType(requestMsg, requestXml);

            if (!EndpointHost.Metadata.CanAccess(endpointAttributes, soapFeatue.ToFormat(), requestType.Name))
            {
                throw EndpointHost.Config.UnauthorizedAccess(endpointAttributes);
            }

            try
            {
                var request             = DataContractDeserializer.Instance.Parse(requestXml, requestType);
                var requiresSoapMessage = request as IRequiresSoapMessage;
                if (requiresSoapMessage != null)
                {
                    requiresSoapMessage.Message = requestMsg;
                }

                var httpReq = HttpContext.Current != null
                    ? new HttpRequestWrapper(requestType.Name, HttpContext.Current.Request)
                    : httpRequest;
                var httpRes = HttpContext.Current != null
                    ? new HttpResponseWrapper(HttpContext.Current.Response)
                    : httpResponse;

                if (EndpointHost.ApplyPreRequestFilters(httpReq, httpRes))
                {
                    return(EmptyResponse(requestMsg, requestType));
                }

                var hasRequestFilters = EndpointHost.RequestFilters.Count > 0 ||
                                        FilterAttributeCache.GetRequestFilterAttributes(request.GetType()).Any();

                if (hasRequestFilters && EndpointHost.ApplyRequestFilters(httpReq, httpRes, request))
                {
                    return(EmptyResponse(requestMsg, requestType));
                }

                var response = ExecuteService(request, endpointAttributes, httpReq, httpRes);

                var hasResponseFilters = EndpointHost.ResponseFilters.Count > 0 ||
                                         FilterAttributeCache.GetResponseFilterAttributes(response.GetType()).Any();

                if (hasResponseFilters && EndpointHost.ApplyResponseFilters(httpReq, httpRes, response))
                {
                    return(EmptyResponse(requestMsg, requestType));
                }

                var httpResult = response as IHttpResult;
                if (httpResult != null)
                {
                    response = httpResult.Response;
                }

                return(requestMsg.Headers.Action == null
                    ? Message.CreateMessage(requestMsg.Version, null, response)
                    : Message.CreateMessage(requestMsg.Version, requestType.Name + "Response", response));
            }
            catch (Exception ex)
            {
                throw new SerializationException("3) Error trying to deserialize requestType: "
                                                 + requestType
                                                 + ", xml body: " + requestXml, ex);
            }
        }