简体   繁体   English

在WCF REST服务中禁用XML示例?

[英]Disable XML examples in WCF REST service?

I have a REST service that has several clients consuming it that is set up to, as far as I can tell, only accept JSON when performing a POST/PUT (and only return JSON on all calls). 我有一个REST服务,有几个客户端使用它,据我所知,只有在执行POST / PUT时才接受JSON(并且只在所有调用上返回JSON)。 The issue is that on the service /help page, it shows examples of JSON and XML both. 问题是在服务/帮助页面上,它显示了JSON和XML的示例。 Is there a way to remove all the extra XML garbage so as not to confuse users (since, again, the service only accepts JSON) and only display JSON examples on the /help page? 有没有办法删除所有额外的XML垃圾,以免混淆用户(因为,再次,服务只接受JSON),只在/帮助页面上显示JSON示例? Here's my Web.config: 这是我的Web.config:

<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" />
  </webHttpEndpoint>
</standardEndpoints>

And each of my GetGets/WebInvokes are defined w/ JSON as the formats, for example: 我的每个GetGets / WebInvokes都以JSON作为格式定义,例如:

[WebInvoke(UriTemplate = "/sample", BodyStyle = WebMessageBodyStyle.Bare, Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

So, is there anything else I can do to let the service know that it's JSON only and remove the auto-gen XML junk cluttering up my /help pages? 那么,有没有什么我可以做让服务知道它是JSON, 只有及时排除自动根XML垃圾塞满了我的/帮助页?

Take your Microsoft.ApplicationServer.Http.HttpConfiguration config and remove its XmlFormatter (it has a JSON formatter and an XML formatter by default). 使用您的Microsoft.ApplicationServer.Http.HttpConfiguration config并删除其XmlFormatter (默认情况下它具有JSON格式化程序和XML格式化程序)。

var config = new Microsoft.ApplicationServer.Http.HttpConfiguration();
config.Formatters.Remove(config.Formatters.XmlFormatter);

Now you can create an HttpServiceHostFactory with this configuration, and use that to register routes. 现在,您可以使用此配置创建HttpServiceHostFactory ,并使用它来注册路由。

//RouteTable is of type System.Web.Routing.RouteCollection
RouteTable.Add(new WebApiRoute(
    "MyService",
    new HttpServiceHostFactory { Configuration = config },
    typeof(MyService)));

I don't have any credible source for you other than "it works for me." 除了“它适合我”之外,我没有任何可靠的消息来源。

You can disable the help page by adding this to your web.config: 您可以通过将此添加到web.config来禁用帮助页面:

<webServices>
 <protocols>
   <remove name="Documentation" />
 </protocols>
</webServices>

source 资源

I was very unhappy with how MS implemented this feature. 我对MS如何实现此功能非常不满意。 They don't provide any control on the output and all implementing details are private. 它们不对输出提供任何控制,并且所有实现细节都是私有的。 Interfaces, classes, etc... 接口,类等......

I ended up building my own sample generator for other reasons but after reading all the code related to this I can tell you for sure you can't control the behavior. 由于其他原因,我最终构建了自己的样本生成器,但在阅读了与此相关的所有代码后,我可以确定您无法控制行为。

Optionally, and if you feel strongly about this, you can add an IOperationBehavior and strip the xml example output. (可选)如果您对此有强烈的感觉,可以添加IOperationBehavior并去除xml示例输出。 The mark up is well formatted and you should be able to easily parse and find the sections to remove from the final mark up. 标记格式正确,您应该能够轻松解析并找到要从最终标记中删除的部分。

If you feel like you'd like to pursue this further let me know and I can give you more details. 如果你想进一步追求这个让我知道,我可以给你更多的细节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM