1

I'm building a WCF REST Web service which should output JSON, but I'm getting XML.

I'm using ResponseFormat = WebMessageFormat.Json

Please help I search everywhere and I couldn't find the solution.

Note: I even tried the suggestion from here

config file

<services> 
  <service name="TestService"> 
     <endpoint address="" 
               behaviorConfiguration="TestServiceAspNetAjaxBehavior" 
               binding="webHttpBinding" 
               bindingConfiguration="webBinding" 
               contract="TestService" /> 
   </service> 
 </services> 
 <bindings> 
     <webHttpBinding> 
         <binding name="webBinding"> 
              <security mode="Transport"> 
              </security> 
          </binding> 
      </webHttpBinding> 
 </bindings>
8
  • I did try <webHttp defaultOutgoingResponseFormat="Json"/> in the web.config file and didn't work! Commented Jul 12, 2011 at 15:10
  • Can I confirm that you are using WebServiceHost or WebHttpBinding? Commented Jul 12, 2011 at 15:11
  • I'm using WebHttpBinding. <services> <service name="TestService"> <endpoint address="" behaviorConfiguration="TestServiceAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="TestService" /> </service> </services> <bindings> <webHttpBinding> <binding name="webBinding"> <security mode="Transport"> </security> </binding> </webHttpBinding> </bindings> Commented Jul 12, 2011 at 15:14
  • Can you add the endpointBehavior block to the config above too Commented Jul 12, 2011 at 17:25
  • Well I have it on <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="TimeServiceAspNetAjaxBehavior"> <webHttp defaultOutgoingResponseFormat="Json"/> <enableWebScript /> </behavior> </endpointBehaviors> </behaviors> Commented Jul 12, 2011 at 19:24

2 Answers 2

1

Some things you need to check:

  • The "name" attribute of your <service> element in web.config needs to match the fully-qualified name of the service class - if TestService is on namespace MyNamespace, then the service must be declared as <service name="MyNamespace.TestService"> - in other words, it must match the name you have in the '.svc' file for your service
  • Your endpoint declaration doesn't specify a behaviorConfiguration attribute; for WCF web endpoints, you need both to have the webHttpBinding and the a reference to a behavior which declares (in your case) <webHttp />
  • Another option is to use the WebServiceHostFactory in the .svc file: <% @ServiceHost Service="MyNamespace.TestService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" Language="C#" debug="true" %>. With this you don't need to have the service defined in config.
Sign up to request clarification or add additional context in comments.

1 Comment

I'm not using .svc file. I using the WCF Rest template which don't need a .svc file. Beside that I did change the service name in the web.config and didn't solve the issue.
0

I had a similar issue because I had a tag like this;

<behavior name="jsonBehavior">
          <enableWebScript/>
          <webHttp helpEnabled="true"/>
        </behavior>

after I remove

<webHttp helpEnabled="true"/>

it is fixed. Maybe it is a bug. I am not sure.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.