예제 #1
0
        /// <summary>
        /// Create a new instance of the WCF Message class that has the correct content type (xml/json) and has a serialized
        /// instance of the SyncError class which is a representation of the error message.
        /// </summary>
        /// <param name="httpStatusCode">Status code to be used for the outgoing response.</param>
        /// <param name="errorDescription">A description of the error.</param>
        /// <returns>An instance of the WCF Message class that is sent as response.</returns>
        private Message CreateExceptionMessage(HttpStatusCode httpStatusCode, string errorDescription)
        {
            var error = new ServiceError {
                ErrorDescription = errorDescription
            };
            //ServiceError error = null;

            Message message;

            if (_syncConfiguration.SerializationFormat == SyncSerializationFormat.ODataJson)
            {
                message = Message.CreateMessage(MessageVersion.None, String.Empty, error, new DataContractJsonSerializer(typeof(ServiceError)));
                message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));
            }
            else
            {
                message = Message.CreateMessage(MessageVersion.None, String.Empty, error, new DataContractSerializer(typeof(ServiceError)));
                message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Xml));
            }

            var property = new HttpResponseMessageProperty {
                StatusCode = httpStatusCode
            };

            property.Headers.Add(HttpResponseHeader.ContentType, WebUtil.GetContentType(_serviceHost.GetOutputSerializationFormat(_syncConfiguration.SerializationFormat)));

            message.Properties.Add(HttpResponseMessageProperty.Name, property);

            return(message);
        }
        protected Message CreateResponseMessage(SyncSerializationFormat serializationFormat, SyncWriter oDataWriter)
        {
            var bodyWriter = new DelegateBodyWriter(WriteResponse, oDataWriter);

            Message message = Message.CreateMessage(MessageVersion.None, String.Empty, bodyWriter);



            switch (serializationFormat)
            {
            case SyncSerializationFormat.ODataAtom:
                message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Xml));
                break;

            case SyncSerializationFormat.ODataJson:
                message.Properties.Add(WebBodyFormatMessageProperty.Name, new WebBodyFormatMessageProperty(WebContentFormat.Json));
                break;
            }



            var property = new HttpResponseMessageProperty {
                StatusCode = HttpStatusCode.OK
            };

            property.Headers[HttpResponseHeader.ContentType] = WebUtil.GetContentType(serializationFormat);

            // Copy the SyncOperationContext's ResponseHeaders if present
            if (this._operationContext != null)
            {
                property.Headers.Add(this._operationContext.ResponseHeaders);
            }

            message.Properties.Add(HttpResponseMessageProperty.Name, property);

            return(message);
        }