WebMethod in my webservice should return a dataset and yet returns an array?

限于喜欢 提交于 2020-01-05 08:59:43

问题


    [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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!