2017-04-06 57 views
0

我開始使用WCF編寫Web服務,而不是使用舊的asp.net asmx方式。我想要能夠返回JSON,所以我不能以舊的方式去做。如果我返回一個字符串或int Web服務工作正常,但如果我嘗試返回一個DataTable它變得沒有響應,我什麼都沒有。我嘗試過調試以查看它爆炸的位置,甚至不會停在斷點上。WCF WebService不返回數據表

我的類文件看起來像這樣:

public string XMLData(string id) 
    { 
     return "You requested " + id; 
    } 


    public string JSONData(string id) 
    { 
     return "You requested " + id; 
    } 

    public DataTable TestDT() 
    { 
     DataTable Testing = new DataTable("TestDT"); 

     return Testing; 
    } 

我的接口文件看起來像這樣:

[OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "xml/{id}")] 
    string XMLData(string id); 

    [OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "json/{id}")] 
    string JSONData(string id); 

    [OperationContract] 
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, 
     UriTemplate = "testdt/")] 
    DataTable TestDT(); 

json方法還有xml方法工作完全正常。當我打電話給TestDT時,我得到一個標準的IE頁面,顯示沒有連接。我已經用數據或datable中的數據嘗試過,結果相同。

這裏的一個其他注意事項:當我在本地運行WCF服務(擊中PLAY)我的測試應用程序現在顯示的服務我看到這一點:

enter image description here

我如何能得到這個具有確定年代的工作?

編輯#1:我實際上解決了數據沒有返回的問題。我最終放棄了試圖返回一個數據集,而是我創建了一個鋸齒形數組,最終以JSON的形式返回。我稍後會將其作爲答案發布,以便人們知道我是如何做到的。什麼我更想知道現在是第2部分。爲什麼我的WCF測試客戶端沒有表現出任何的方法(見圖片附後)我的預感是其相關的web.config文件,所以我張貼的下面:

<?xml version="1.0"?> 
<configuration> 
    <connectionStrings> 
    <add name="DBDjuggleWS" connectionString="Data Source=localhost;Initial Catalog=PrayUpDev;Persist Security Info=True;User=DjuggleMaster;Password=DJPassword!" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.0" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name ="PrayUpService.PrayUp" behaviorConfiguration="ServiceBehaviour"> 
     <endpoint address="" binding="webHttpBinding" contract="PrayUpService.IPrayUp" behaviorConfiguration ="web"></endpoint> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior name="ServiceBehaviour"> 
      <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment --> 
      <serviceMetadata httpGetEnabled="true"/> 
      <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information --> 
      <serviceDebug includeExceptionDetailInFaults="true"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="web"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 
    </system.serviceModel> 
<system.webServer> 
    <modules runAllManagedModulesForAllRequests="true"/> 
    </system.webServer> 

</configuration> 
+0

考慮返回使用數據合同。如果只有一列有多行,則可以考慮返回List(字符串)。 – Baskar

+0

[在WCF/.NET中返回DataTables]的可能重複(http://stackoverflow.com/questions/12702/returning-datatables-in-wcf-net) – tomcater

回答