在WCF REST服务中,我根据Accept HTTP标头中指定的值在JSON和XML之间切换响应序列化。我正在使用IDispatchMessageFormatter,如此处所述 - http://damianblog.com/2008/10/31/wcf-rest-dynamic-response/。
我正在使用Content-Type进行PUT和POST,但是在PUT和POST的情况下,它自己的IDispatchMessageFormatter.SerializeReply不会被执行。
唯一的问题是这只适用于GET请求,而不适用于PUT,POST,DELETE等。有人知道为什么吗?或者我遗漏了一些非常基本的东西:)
感谢。
答案 0 :(得分:1)
Ok解决了问题.....当前代码只处理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);
}
}
答案 1 :(得分:0)
对于过帐值,您应使用标题Content-Type。此标头通常用于此类操作。
作为参考,您可以查看http://en.wikipedia.org/wiki/List_of_HTTP_header_fields