0

在 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 等。有人知道为什么吗?或者我在这里遗漏了一些非常基本的东西:)

谢谢。

4

2 回答 2

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);
    }
}
于 2013-08-28T04:28:37.260 回答
0

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

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

于 2013-08-28T03:59:33.547 回答