简体   繁体   中英

Asp.net Webservice not working while returning List<object>

I am trying to return List type from a web service. I have used a similar version from here .

I get the following error :

System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Collections.Generic.List`1[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] may not be used in this context.

Code:

 [WebMethod]
    public List<object> getnpsTrend(string region, string client, string product)
    {
        List<object> iData = new List<object>();
        List<string> labels = new List<string>();

        labels.Add ("test1");
        iData.Add(labels);
        return iData; 
    }

Thanks for your help in advance!

Update: After hours of struggling, i have found out that it does not work only when my list object contains another complex object (like array / another list). It works works otherwise. The following code yields perfect result.

[WebMethod]
    public List<Object> getnpsTrend()
    {
        List<Object> li = new List<object>();
        string obj = "Test";
        li.Add(obj);
        return li ;
    }

But I need a list containing list to be the return type. Is there any way I can create a user defined type with the required structure, that will be serializable by xml ?

I was able to find the solution after more searching. I guess, when we use more complex return types, we will have to define the structure using classes. Only then asp is able to map it else it just displays irrelevant error messages :) . Thankfully another person had a similar question .

Please find the code below in case you are searching for the answer:

[WebMethod]        
    public List<ChartDatasets> getnpsTrend(string region, string client, string product)
    {
        List<ChartDatasets> chart1 = new List<ChartDatasets>();
        List<string> lblnames = new List<string>();

        DataTable dt = biz.FetchData_RCP("", region, client, product);
        dt.TableName = "data";
        foreach (DataRow drow in dt.Rows)
        {
            lblnames.Add(drow["Timeline"].ToString());                
        }


        Labels lbl1 = new Labels();
        lbl1.LabelNames = lblnames;


        List<Decimal> lst_dataItem_1 = new List<Decimal>();
        foreach (DataRow dr in dt.Rows)
        {
            lst_dataItem_1.Add(Convert.ToDecimal(dr["NPSScore"].ToString()));
        }
        dataset_deci ds1_class = new dataset_deci();
        ds1_class.Value = lst_dataItem_1;

        List<Decimal> lst_dataItem_2 = new List<Decimal>();
        foreach (DataRow dr in dt.Rows)
        {
            lst_dataItem_2.Add(Convert.ToDecimal(dr["Promoter_Count"].ToString()));
        }
        dataset_deci ds2_class = new dataset_deci();
        ds2_class.Value = lst_dataItem_2;


        chart1.Add(new ChartDatasets { Lbls = new List<Labels> { lbl1 }, ds1 = new List<dataset_deci> { ds1_class }, ds2 = new List<dataset_deci> { ds2_class } });
        return chart1;
    }

    public class ChartDatasets
    {
        public List<Labels> Lbls;
        public List<dataset_deci> ds1;
        public List<dataset_deci> ds2;
    }

    public class Labels
    {
        public List<string> LabelNames;
    }

    public class dataset_deci
    {
       public List<Decimal> Value;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM