2011-07-12 142 views
1

我正在构建一个应该输出JSON的WCF REST Web服务,但我正在获取XML。WCF JSON返回XML数据!

我使用ResponseFormat = WebMessageFormat.Json

请帮我搜索无处不在,我无法找到解决方案。

注:我甚至尝试从here

配置文件

<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> 
+0

我在web.config文件中尝试了并且没有工作! – Hardcode

+0

我可以确认您使用的是WebServiceHost还是WebHttpBinding? –

+0

我正在使用WebHttpBinding。 <服务名称= “TestService的”> <端点地址= “” behaviorConfiguration = “TestServiceAspNetAjaxBehavior” 结合= “的WebHttpBinding” bindingConfiguration = “webBinding” 合同= “TestService的”/> <的WebHttpBinding> <绑定名称= “webBinding”> <安全模式= “运输”> Hardcode

回答

1

的建议有些事情你需要检查:

  • “名称”,在网络上对你<service>元素的属性.config需要匹配服务类的完全限定名称 - 如果TestService位于namespac上e MyNamespace,那么服务必须声明为<service name="MyNamespace.TestService"> - 换句话说,它必须与您的服务在'.svc'文件中的名称匹配
  • 您的端点声明未指定behaviorConfiguration属性; WCF的网络端点,您既需要具备的WebHttpBinding 的到宣布(你的情况)行为<webHttp />
  • 另一种选择是使用.svc文件的WebServiceHostFactory一个参考:<% @ServiceHost Service="MyNamespace.TestService" Factory="System.ServiceModel.Activation.WebServiceHostFactory" Language="C#" debug="true" %>。有了这个,你不需要在config中定义服务。
+0

我没有使用.svc文件。我使用不需要.svc文件的WCF Rest模板。除此之外,我确实在web.config中更改了服务名称,但没有解决问题。 – Hardcode

0

我有类似的问题,因为我有这样的标签;

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

后我除去

<webHttp helpEnabled="true"/> 

它是固定的。也许这是一个错误。我不确定。