Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable XML examples in WCF REST service?

Tags:

json

rest

c#

.net

wcf

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). The issue is that on the service /help page, it shows examples of JSON and XML both. 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? Here's my 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:

[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?

like image 769
Stephen Fischer Avatar asked Oct 21 '22 21:10

Stephen Fischer


1 Answers

Take your Microsoft.ApplicationServer.Http.HttpConfiguration config and remove its XmlFormatter (it has a JSON formatter and an XML formatter by default).

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.

//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."

like image 198
Timothy Shields Avatar answered Oct 27 '22 08:10

Timothy Shields