3

Ok, so I am confused as to why a string I am building is 'magically' having extra characters added into it.

First of all, I saw backslashes appearing in the immediate window:

"[{\"ID\":\"1\",\"F1\":\"lala\",\"F2\":\"hehe\"},{\"ID\":\"2\",\"F1\":\"abc\",\"F2\":\"def\"}]"

But after reading on Google that these are only 'visual', and do not actually exist in the variable value itself, I tried the text Visualizer:

[{"ID":"1","F1":"lala","F2":"hehe"},{"ID":"2","F1":"abc","F2":"def"}]

.. so ok, they are not there :)

Yet then I go to call the webservce from Chrome browser:

{"JSONsampleResult":"[{\"ID\":\"1\",\"F1\":\"lala\",\"F2\":\"hehe\"},{\"ID\":\"2\",\"F1\":\"abc\",\"F2\":\"def\"}]"}

So not only are the backslashes there, but it's also added quotes around my square brackets !?

Please please please can someone explain why I cannot parse a simple string ?? What is adding all these extras ?

ServiceRestTable.svc (extract of)

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace netshedWebService_2
{
     public class ServiceRestTable : IServiceRestTable
    {
        private static long EpochTicks = new DateTime(1970, 1, 1).Ticks;

        string ConString = ConfigurationManager.ConnectionStrings["netshedConnectionString"].ConnectionString;
        SqlConnection con;
        SqlCommand cmd;
        SqlDataAdapter sda;
        DataTable dt;
        Sample emp = new Sample();

        public String JSONsample(string p_str)
        {
            string myList;
            using (con = new SqlConnection(ConString))
            {
                cmd = new SqlCommand("SELECT ID, F1, F2 FROM ADT.Test", con);
                sda = new SqlDataAdapter(cmd);
                dt = new DataTable("Paging");
                sda.Fill(dt);
                emp.SampleTable = dt;
                myList = FromDataTable(emp.SampleTable);
                return myList;
            }
        }

IServiceRestTable.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace netshedWebService_2
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IRestServiceImpl" in both code and config file together.
    [ServiceContract]
    public interface IServiceRestTable
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "xml/{id}")]
        Sample XMLsample(string id);

        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
            BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "json/{id}")]
        String JSONsample(string id);
    }
    [DataContract]
    public class Sample
    {
        [DataMember]
        public DataTable SampleTable
        {
            get;
            set;
        }
    }
}

Web.config

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <add name="netshedConnectionString" connectionString="xxxxx"/>
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="netshedWebService_2.ServiceRestTable" behaviorConfiguration ="RestServiceBehaviour">
        <endpoint address="" binding="webHttpBinding" contract="netshedWebService_2.IServiceRestTable" behaviorConfiguration="web">
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="RestServiceBehaviour">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
            <webHttp/>
        </behavior>
      </endpointBehaviors>      
    </behaviors>    
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration>
Esby
  • 105
  • 1
  • 15
  • 4
    Most probably you are double serializing your object. My guess, you serialize your datatable in `FromDataTable` and return a string. WCF also serializes your string. If this is the case, return your object directly in your method, do not manually serialize it. – L.B Mar 10 '14 at 07:45
  • I am manually serializing, simply because I was following an example given to me. So, in essence, you are saying that instead of building a string, I should create my own object with pre-defined fields, populate that, and then return the full object ? (unfortunately I cannot post the rest of my code for many more hours as my rep is < 10 !?) – Esby Mar 10 '14 at 08:47
  • omg .. I have it working ! And yes, looking at the solution in hindsight it is so simple. I could sit and wonder why the tutorial I was looking at does not work for me, but did for the author .... but there again, maybe best just to be grateful for the here and now! Thank you so much for helping with what for many, might seem a basic operation... but for me as a noob, a nightmare !! – Esby Mar 10 '14 at 09:29

0 Answers0