0

I am beginning to use WCF to write a web service instead of the old asp.net asmx way. I want to be able to return JSON so I can't do it the old way. If I am returning a string or int the Web Service works fine but if I try to return a DataTable it becomes unresponsive and I get nothing. I have tried debugging to see where it blows up and it won't even stop on a breakpoint.

My Class file looks like this:

 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;
    }

My interface file looks like this:

    [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();

The json method as well as the xml method works perfectly fine. When I call TestDT I get a standard IE Page showing no connectivity. I have tried it with data or no data in the datable with the same results.

One other note here: when I run the WCF service locally (Hitting PLAY) my test application shows now services I see this:

enter image description here

How can I get this to work with a datable?

Edit#1: I actually resolved the issue with the data not returning. I finally gave up on trying to return a dataset and instead I created a jagged array and ended up return it as JSON. I'll post it later as an answer so people know how I did it. What I am more interested in knowing now is part 2. Why is my WCF Test Client not showing any methods (see image attached) My hunch is its related to the web.config so I am posting that below:

<?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>
logixologist
  • 3,694
  • 4
  • 28
  • 46
  • Consider to return using Data Contract. If there is only one column with multiple rows you can consider to return as List(string). – Baskar Rao Apr 07 '17 at 05:48
  • Possible duplicate of [Returning DataTables in WCF/.NET](http://stackoverflow.com/questions/12702/returning-datatables-in-wcf-net) – hvojdani Apr 08 '17 at 05:43

1 Answers1

0

you shoud return DataSet instead of DataTable but Returning data sets from web services is not typically considered a “good practice”. here you can read about it: Why returning dataset or data table from WCF service is not a good practice? What are the Alternatives?

i highly recommend use the DataSet’s methods to get the data in XML format and pass the XML string instead of the DataSet itself to the service.

PassDataSet(dsDataSet.GetXmlSchema(), dsDataSet.GetXml())
Community
  • 1
  • 1
Mohammad
  • 2,724
  • 6
  • 29
  • 55