Reputation: 3797
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.
Upvotes: 2
Views: 885
Reputation: 7073
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);
Upvotes: 1
Reputation: 10026
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.
Upvotes: 1