问题
[WebMethod]
public DataSet GetPopularFlavors()
{
return Helper.PopularItems();
}
localhost.Statistics s = new localhost.Statistics();
topItems.DataSource = s.GetPopularFlavors();
And just for the record, im kind of new at this so im really sorry if im doing something that is obviously wrong...
回答1:
Web services by default return json ("stringed" objects). They can return XML too, if specified. A DataSet is complex and should be broken down into tables perhaps, or something smaller that can be easily retrieved and worked with by any system.
Scott Hanselman's rant.
You might get away with adding the Serializable attribute to your class (? not sure), or serializing the DataSet inside the method before returning it.
[Serializable]
public class MyClass...
Use JSON.NET (a helper library) to do the work, then return the result.
JsonConvert.SerializeObject(tbl);
It might be best to create a simpler object first and return that.
So, I think you're getting an array because the web service has to convert the object into something it can return via http, and it can't return a native .NET object, like a DataSet or DataTable, without converting it first.
来源:https://stackoverflow.com/questions/49961146/webmethod-in-my-webservice-should-return-a-dataset-and-yet-returns-an-array