将WCF响应中的序列化b/w json和xml切换为非“;获取“;方法
本文关键字:xml 方法 获取 json 响应 WCF 序列化 | 更新日期: 2023-09-27 18:29:11
在WCF REST服务中,我根据Accept HTTP标头中指定的值在JSON和XML之间切换响应序列化。我正在使用IDispatchMessageFormatter,如下所述-http://damianblog.com/2008/10/31/wcf-rest-dynamic-response/.
我将Content-Type用于PUT和POST,但在PUT和POST的情况下,IDispatchMessageFormatter.SerializePReply本身不会执行。
唯一的问题是,这只适用于GET请求,而不适用于PUT、POST、DELETE等。有人知道为什么吗?或者我在这里错过了一些非常基本的东西:)
谢谢。
好的,解决了问题。。。。。当前代码仅处理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);
}
}
对于过帐值,您应该使用标题Content-Type。此标头通常用于此类操作。
作为参考,你可以看看http://en.wikipedia.org/wiki/List_of_HTTP_header_fields