예제 #1
0
        public string SerializeToString(IRequestContext requestContext, object response)
        {
            var contentType = requestContext.ResponseContentType;

            StreamSerializerDelegate responseStreamWriter;

            if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
                this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
            {
                using (var ms = new MemoryStream())
                {
                    responseStreamWriter(requestContext, response, ms);

                    ms.Position = 0;
                    var result = new StreamReader(ms, UTF8EncodingWithoutBom).ReadToEnd();
                    return(result);
                }
            }

            ResponseSerializerDelegate responseWriter;

            if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
                this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
            {
                using (var ms = new MemoryStream())
                {
                    var httpRes = new HttpResponseStreamWrapper(ms)
                    {
                        KeepOpen = true, //Don't let view engines close the OutputStream
                    };
                    responseWriter(requestContext, response, httpRes);

                    var bytes  = ms.ToArray();
                    var result = bytes.FromUtf8Bytes();

                    httpRes.ForceClose(); //Manually close the OutputStream

                    return(result);
                }
            }


            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);

            switch (contentTypeAttr)
            {
            case EndpointAttributes.Xml:
                return(WrappedXmlSerializer.SerializeToString(response, false));

            case EndpointAttributes.Json:
                return(WrappedJsonSerializer.Instance.SerializeToString(response));

            case EndpointAttributes.Jsv:
                return(TypeSerializer.SerializeToString(response));
            }

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }
예제 #2
0
 private string XmlSerializeToString(object obj)
 {
     try
     {
         return(WrappedXmlSerializer.SerializeToString(obj, true));
     }
     catch (Exception)
     {
         return("");
     }
 }
예제 #3
0
        public object ExecuteText(string operationName, string requestXml, Type requestType, IRequestContext requestContext)
        {
            var          request = WrappedXmlSerializer.DeserializeFromString(requestXml, requestType);
            IHttpRequest httpReq = requestContext.Get <IHttpRequest>();

            if (httpReq != null)
            {
                httpReq.RequestObject = request;
            }
            var response    = Execute(operationName, request, requestContext);
            var responseXml = WrappedXmlSerializer.SerializeToString(response, false);

            return(responseXml);
        }
예제 #4
0
        public byte[] SerializeToBytes(IRequestContext requestContext, object response)
        {
            var contentType = requestContext.ResponseContentType;

            StreamSerializerDelegate responseStreamWriter;

            if (this.ContentTypeSerializers.TryGetValue(contentType, out responseStreamWriter) ||
                this.ContentTypeSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseStreamWriter))
            {
                using (var ms = new MemoryStream())
                {
                    responseStreamWriter(requestContext, response, ms);
                    ms.Position = 0;
                    return(ms.ToArray());
                }
            }

            ResponseSerializerDelegate responseWriter;

            if (this.ContentTypeResponseSerializers.TryGetValue(contentType, out responseWriter) ||
                this.ContentTypeResponseSerializers.TryGetValue(ContentType.GetRealContentType(contentType), out responseWriter))
            {
                using (var ms = new MemoryStream())
                {
                    var httpRes = new HttpResponseStreamWrapper(ms);
                    responseWriter(requestContext, response, httpRes);
                    ms.Position = 0;
                    return(ms.ToArray());
                }
            }

            var contentTypeAttr = ContentType.GetEndpointAttributes(contentType);

            switch (contentTypeAttr)
            {
            case EndpointAttributes.Xml:
                return(WrappedXmlSerializer.SerializeToString(response, false).ToUtf8Bytes());

            case EndpointAttributes.Json:
                return(WrappedJsonSerializer.Instance.SerializeToString(response).ToUtf8Bytes());

            case EndpointAttributes.Jsv:
                return(TypeSerializer.SerializeToString(response).ToUtf8Bytes());
            }

            throw new NotSupportedException("ContentType not supported: " + contentType);
        }
예제 #5
0
        protected override string CreateMessage(Type dtoType)
        {
            var requestObj = ReflectionUtils.PopulateObject(dtoType.CreateInstance());

            return(WrappedXmlSerializer.SerializeToString(requestObj, true));
        }