WCF Rest Service未返回JSON数据。但它适用于XML

时间:2015-06-29 11:23:30

标签: json wcf wcf-rest

服务合同信息如下 服务合同:

 [OperationContract]
    [FaultContract(typeof(DcCustomFaultMessage))]        
    [WebInvoke(Method="GET", ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "/GetCustomData")]
    List<DcCustomData> GetCustomData();

和配置详细信息 配置:

<system.serviceModel>
<services>
  <service name="MyService" behaviorConfiguration="MyServiceBehavior">
    <endpoint address="" binding="webHttpBinding"  bindingConfiguration="MyServiceBinding" behaviorConfiguration="web" contract="IMyService"/>
  </service>     
</services>    
<behaviors>
  <serviceBehaviors>
    <behavior name="MyServiceBehavior">
      <serviceDebug includeExceptionDetailInFaults="True"/>
      <serviceTimeouts transactionTimeout="05:00:00"/>
      <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
      <serviceMetadata httpGetEnabled="True"/>      
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="web" >
      <webHttp helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>
</behaviors>
<standardEndpoints>
  <webHttpEndpoint>
    <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
  </webHttpEndpoint>
</standardEndpoints>  
<bindings>
  <webHttpBinding>
    <binding name="MyServiceBinding">
      <security mode="None"/>
    </binding>
  </webHttpBinding>
</bindings>    
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />    
<diagnostics performanceCounters="All"/>    

当我调试时,GetCustomData()返回列表。但是当我在浏览器上检查它什么都没有返回..没有错误信息。请帮帮我

1 个答案:

答案 0 :(得分:0)

请配置WCF消息记录并使用svctraceviewer工具查看可能在幕后发生的实际错误:

<configuration>
<system.diagnostics>
    <trace autoflush="true" />
    <sources>
      <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true">
        <listeners>
          <add name="wcfTraceListener" type="System.Diagnostics.XmlWriterTraceListener" initializeData="E:\Errors\WcfTrace.svclog" traceOutputOptions="DateTime" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>
 </configuration>

<强> https://msdn.microsoft.com/en-us/library/ms731859%28v=vs.110%29.aspx