我的Web服务中的WebMethod应该返回数据集却返回数组吗? [英] WebMethod in my webservice should return a dataset and yet returns an array?

查看:89
本文介绍了我的Web服务中的WebMethod应该返回数据集却返回数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    [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...

推荐答案

Web服务默认情况下返回json(字符串对象)。如果指定,它们也可以返回XML。 DataSet很复杂,应该分解为表格,或者可以分解为更小的表,以便于任何系统轻松检索和使用。

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.

斯科特·汉塞尔曼的咆哮。

您可能不愿意在类中添加Serializable属性(?不确定),或者在返回之前在方法内部序列化DataSet。

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...

使用JSON.NET (帮助程序库)进行工作,然后返回结果。

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.

所以,我认为您正在获取一个数组,因为Web服务必须将对象转换为可以通过http返回的对象,并且不能返回本地对象.NET对象,例如DataSet或DataTable,而无需先进行转换。

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.

这篇关于我的Web服务中的WebMethod应该返回数据集却返回数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆