好的解决了问题.....当前代码只处理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);
}
}