2

I consuming web-service from other project in my client project (ALTHOUGH both project are in same solution)

I am expecting JSON out put bu unable to get it,

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "FetchSitePerformanceDT/{fromDate}/{country}")]
    public DataTable FetchSitePerformanceDT(string fromDate, string country)
    {
        SitePerformance objSiteP = new SitePerformance();
        DataTable dt = new DataTable();
        dt = objSiteP.getPerformanceByDateAndCountryAsDataTable(fromDate, country);
        return dt;
    }

This function returns output like,

{"FetchSitePerformanceDTResult":"<DataTable xmlns=\"http:\/\/schemas.datacontract.org\/2004\/07\/System.Data\"><xs:schema id=\"NewDataSet\" xmlns:xs=\"http:\/\/www.w3.org\/2001\/XMLSchema\" xmlns=\"\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:MainDataTable=\"DailyBingRTT_Performance_Last7Days_Result\" msdata:UseCurrentLocale=\"true\"><xs:complexType><xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\"><xs:element name=\"DailyBingRTT_Performance_Last7Days_Result\"><xs:complexType><xs:sequence><xs:element name=\"DailyTimeStamp\" type=\"xs:string\" minOccurs=\"0\"\/><xs:element name=\"Performance\" type=\"xs:string\" minOccurs=\"0\"\/><\/xs:sequence><\/xs:complexType><\/xs:element><\/xs:choice><\/xs:complexType><\/xs:element><\/xs:schema><diffgr:diffgram xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\"><DocumentElement xmlns=\"\"><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result1\" msdata:rowOrder=\"0\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 03, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result2\" msdata:rowOrder=\"1\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 04, 2013<\/DailyTimeStamp><Performance>119542<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result3\" msdata:rowOrder=\"2\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 05, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result4\" msdata:rowOrder=\"3\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 06, 2013<\/DailyTimeStamp><Performance>119542<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result5\" msdata:rowOrder=\"4\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 07, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result6\" msdata:rowOrder=\"5\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 08, 2013<\/DailyTimeStamp><Performance>119542<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><DailyBingRTT_Performance_Last7Days_Result diffgr:id=\"DailyBingRTT_Performance_Last7Days_Result7\" msdata:rowOrder=\"6\" diffgr:hasChanges=\"inserted\"><DailyTimeStamp>Nov 09, 2013<\/DailyTimeStamp><Performance>106917<\/Performance><\/DailyBingRTT_Performance_Last7Days_Result><\/DocumentElement><\/diffgr:diffgram><\/DataTable>"}

What is that? xml or what? I don't want taht, so manualy converted it to JSON like,

[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
           BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "FetchSitePerformanceStream/{fromDate}/{country}")]
        public Stream FetchSitePerformanceStream(string fromDate, string country)
        {
            SitePerformance objSiteP = new SitePerformance();

            List<DailyBingRTT_Performance_Last7Days_Result> l = new List<DailyBingRTT_Performance_Last7Days_Result>();
            l = objSiteP.getPerformanceByDateAndCountry(fromDate, country);

            var javaScriptSerializer = new JavaScriptSerializer();
            var json = Encoding.UTF8.GetBytes(javaScriptSerializer.Serialize(l));
            var memoryStream = new MemoryStream(json);
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            return memoryStream;

        }

It gives correct output like,

[{"DailyTimeStamp":"Nov 03, 2013","Performance":106917},{"DailyTimeStamp":"Nov 04, 2013","Performance":119542},{"DailyTimeStamp":"Nov 05, 2013","Performance":106917},{"DailyTimeStamp":"Nov 06, 2013","Performance":119542},{"DailyTimeStamp":"Nov 07, 2013","Performance":106917},{"DailyTimeStamp":"Nov 08, 2013","Performance":119542},{"DailyTimeStamp":"Nov 09, 2013","Performance":106917}]

Can anyone please help me what is wrong in my first method of web-service. Thanks for reading so long question.

AK47
  • 3,707
  • 3
  • 17
  • 36

2 Answers2

1

The main thing to missing in your question is DailyBingRTT_Performance_Last7Days_Result

Is that custom build class or data table ? What is the structure?

The solution could be as follows:

Class Performance { 
  publc datetime DailyTimeStamp { get; set;}
  public int performance ... 
}

List<Performance> ... 

return serializer.Serialize(rows);
Brandon
  • 68,708
  • 30
  • 194
  • 223
codebased
  • 6,945
  • 9
  • 50
  • 84
  • are you saying that function should be of type list always? – AK47 May 22 '14 at 12:12
  • I am assuming in my answer that you are looking for the output as : [{"DailyTimeStamp":"Nov 03, 2013","Performance":106917} Thus you need to provide the custom class that you are looking for. Otherwise serializer will never know what is the serliaze format – codebased May 22 '14 at 12:13
  • I am trying to use code which is already build by .net, as we can see there is property in webInvoke as "responseformat=JSON". if property is correct then why output is not in JSON? – AK47 May 22 '14 at 12:14
  • refer: http://stackoverflow.com/questions/17928513/whats-the-best-way-to-json-serialize-a-net-datatable-in-wcf – codebased May 22 '14 at 12:16
  • if you want to get more idea on what I was saying then go here: :http://stackoverflow.com/questions/17398019/how-to-convert-datatable-to-json-in-c-sharp – codebased May 22 '14 at 12:17
  • I tried ur opinion friend but got output as {"FetchSitePerformanceAutoResult":[{"DailyTimeStamp":"Nov 03, 2013","Performance":106917},{"DailyTimeStamp":"Nov 04, 2013","Performance":119542},{"DailyTimeStamp":"Nov 05, 2013","Performance":106917},{"DailyTimeStamp":"Nov 06, 2013","Performance":119542},{"DailyTimeStamp":"Nov 07, 2013","Performance":106917},{"DailyTimeStamp":"Nov 08, 2013","Performance":119542},{"DailyTimeStamp":"Nov 09, 2013","Performance":106917}]}. But I dont know why in javascript it is not readable, difference is "FetchSitePerformanceAutoResult" at the satrt – AK47 May 22 '14 at 12:17
  • Very close - then at client side you willdo the following: var data = jQuery.parseJSON(webservicereturn) - data. FetchSitePerformanceAutoResult should be able to give you wht you are ooking for – codebased May 22 '14 at 12:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54200/discussion-between-ak47-and-codebased). – AK47 May 22 '14 at 12:22
1

Don't return a DataTable from a Web Service. You're making the load that needs to be transferred over the wire unnecessarily heavier.

Not only does a DataTable object contain data, but it also contains Schema information. Not only is this information useless to non-Microsoft clients, but this Schema information also bloats the size of the data to be transferred.

Instead create a DTO (Data Transfer Object) and instead send a collection of those to the client.

Derek W
  • 9,708
  • 5
  • 58
  • 67