Пример #1
0
        /// <summary>Handles REST XML formatting behavior</summary>
        /// <param name="operationDescription"></param>
        /// <param name="endpoint"></param>
        /// <returns></returns>
        protected override IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
        {
            var webInvoke = GetBehavior <WebInvokeAttribute>(operationDescription);

            if (webInvoke == null)
            {
                webInvoke = new WebInvokeAttribute();
                operationDescription.Behaviors.Add(webInvoke);
            }
            webInvoke.RequestFormat  = WebMessageFormat.Xml;
            webInvoke.ResponseFormat = WebMessageFormat.Xml;
            webInvoke.Method         = RestHelper.GetHttpMethodFromOperationDescription(operationDescription);

            var formatter = base.GetReplyDispatchFormatter(operationDescription, endpoint);

            return(formatter);
        }
Пример #2
0
        /// <summary>Handles REST JSON formatting behavior</summary>
        /// <param name="operationDescription"></param>
        /// <param name="endpoint"></param>
        /// <returns></returns>
        protected override IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, ServiceEndpoint endpoint)
        {
            var webInvoke = operationDescription.Behaviors.OfType <WebInvokeAttribute>().FirstOrDefault();

            if (webInvoke == null)
            {
                webInvoke = new WebInvokeAttribute();
                operationDescription.Behaviors.Add(webInvoke);
            }
            webInvoke.RequestFormat  = WebMessageFormat.Json;
            webInvoke.ResponseFormat = WebMessageFormat.Json;
            webInvoke.Method         = RestHelper.GetHttpMethodFromOperationDescription(operationDescription);

            if (operationDescription.Messages.Count == 1 || operationDescription.Messages[1].Body.ReturnValue.Type == typeof(void))
            {
                return(base.GetReplyDispatchFormatter(operationDescription, endpoint));
            }
            return(new NewtonsoftJsonDispatchFormatter(operationDescription, false));
        }