【问题标题】:Switching serialization b/w json and xml in WCF response for non-"GET" methods在 WCF 响应中为非“GET”方法切换序列化 b/w json 和 xml
【发布时间】:2013-08-28 03:51:48
【问题描述】:

在 WCF REST 服务中,我根据 Accept HTTP 标头中指定的值在 JSON 和 XML 之间切换响应序列化。我正在使用此处描述的 IDispatchMessageFormatter - http://damianblog.com/2008/10/31/wcf-rest-dynamic-response/

我将 Content-Type 用于 PUT 和 POST,但 IDispatchMessageFormatter.SerializeReply 在 PUT 和 POST 的情况下不会执行。

唯一的问题是这只适用于 GET 请求,而不适用于 PUT、POST、DELETE 等。有人知道为什么吗?或者我在这里遗漏了一些非常基本的东西:)

谢谢。

【问题讨论】:

    标签: c# xml json wcf


    【解决方案1】:

    好的解决了问题.....当前代码只处理WebGetAttribute:

    class WebHttpBehavior2Ex : WebHttpBehavior
    {
        protected override IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription, 
                                                                               ServiceEndpoint endpoint)
        {
            WebGetAttribute webGetAttribute = operationDescription.Behaviors.Find<WebGetAttribute>();
            DynamicResponseTypeAttribute mapAcceptedContentTypeToResponseEncodingAttribute = 
                operationDescription.Behaviors.Find<DynamicResponseTypeAttribute>();
    
            if (webGetAttribute != null && mapAcceptedContentTypeToResponseEncodingAttribute != null) {
                // We need two formatters, since we don't know what type we will need until runtime
                webGetAttribute.ResponseFormat = WebMessageFormat.Json;
                IDispatchMessageFormatter jsonDispatchMessageFormatter = 
                    base.GetReplyDispatchFormatter(operationDescription, endpoint);
                webGetAttribute.ResponseFormat = WebMessageFormat.Xml;
                IDispatchMessageFormatter xmlDispatchMessageFormatter = 
                    base.GetReplyDispatchFormatter(operationDescription, endpoint);
                return new DynamicFormatter() { 
                    jsonDispatchMessageFormatter = jsonDispatchMessageFormatter, 
                    xmlDispatchMessageFormatter = xmlDispatchMessageFormatter };
            }
            return base.GetReplyDispatchFormatter(operationDescription, endpoint);
        }
    }
    

    相反,我们需要同时处理 WebGet 和 WebInvoke 属性,如下所示:

    public class WebHttpBehaviorEx : WebHttpBehavior
    {
        protected override IDispatchMessageFormatter GetReplyDispatchFormatter(OperationDescription operationDescription,
                                                                               ServiceEndpoint endpoint)
        {
            WebGetAttribute webGetAttribute = operationDescription.Behaviors.Find<WebGetAttribute>();
            WebInvokeAttribute webInvokeAttr = operationDescription.Behaviors.Find<WebInvokeAttribute>();
            DynamicResponseTypeAttribute mapAcceptedContentTypeToResponseEncodingAttribute =
                operationDescription.Behaviors.Find<DynamicResponseTypeAttribute>();
    
            if (webGetAttribute != null && mapAcceptedContentTypeToResponseEncodingAttribute != null)
            {
                // We need two formatters, since we don't know what type we will need until runtime
                webGetAttribute.ResponseFormat = WebMessageFormat.Json;
                IDispatchMessageFormatter jsonDispatchMessageFormatter =
                    base.GetReplyDispatchFormatter(operationDescription, endpoint);
                webGetAttribute.ResponseFormat = WebMessageFormat.Xml;
                IDispatchMessageFormatter xmlDispatchMessageFormatter =
                    base.GetReplyDispatchFormatter(operationDescription, endpoint);
                return new DynamicFormatter()
                {
                    jsonDispatchMessageFormatter = jsonDispatchMessageFormatter,
                    xmlDispatchMessageFormatter = xmlDispatchMessageFormatter
                };
            }
            else if (webInvokeAttr != null && mapAcceptedContentTypeToResponseEncodingAttribute != null)
            {
                // We need two formatters, since we don't know what type we will need until runtime
                webInvokeAttr.ResponseFormat = WebMessageFormat.Json;
                IDispatchMessageFormatter jsonDispatchMessageFormatter =
                    base.GetReplyDispatchFormatter(operationDescription, endpoint);
                webInvokeAttr.ResponseFormat = WebMessageFormat.Xml;
                IDispatchMessageFormatter xmlDispatchMessageFormatter =
                    base.GetReplyDispatchFormatter(operationDescription, endpoint);
                return new DynamicFormatter()
                {
                    jsonDispatchMessageFormatter = jsonDispatchMessageFormatter,
                    xmlDispatchMessageFormatter = xmlDispatchMessageFormatter
                };
            }
            return base.GetReplyDispatchFormatter(operationDescription, endpoint);
        }
    }
    

    【讨论】:

      【解决方案2】:

      对于发布值,您应该使用标头 Content-Type。此标头通常用于此类操作。

      作为参考,您可以查看http://en.wikipedia.org/wiki/List_of_HTTP_header_fields

      【讨论】:

      • 我将 Content-Type 用于 PUT 和 POST,但 IDispatchMessageFormatter.SerializeReply 在 PUT 和 POST 的情况下不会执行。
      猜你喜欢
      • 2013-10-09
      • 1970-01-01
      • 1970-01-01
      • 2014-08-31
      • 2013-03-08
      • 2017-02-08
      • 2015-12-28
      • 2017-06-10
      • 1970-01-01
      相关资源
      最近更新 更多