2011-07-12 144 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"/> 

它是固定的。也許這是一個錯誤。我不確定。