【问题标题】:Return List<object> from webservice从 web 服务返回 List<object>
【发布时间】:2019-02-24 22:47:17
【问题描述】:

我正在使用 Chartjs 制作折线图,但我的网络服务出现错误。这是服务的代码:

[WebMethod]
    public List<object> getProgram12Months(string usersessionid)
    {
        List<object> iData = new List<object>();
        List<string> labels = new List<string>();

        //First get distinct Month Name for select Year.
        string query1 = "SELECT DISTINCT TOP (100) PERCENT TimeFrame FROM dbo.CSQ_ProgramCount12Months ORDER BY TimeFrame ";

        string conn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
        SqlDataAdapter dap = new SqlDataAdapter(query1, conn);
        DataSet ds = new DataSet();
        dap.Fill(ds);
        DataTable dtLabels = ds.Tables[0];


        foreach (DataRow drow in dtLabels.Rows)
        {
            labels.Add(drow["TimeFrame"].ToString());
        }
        iData.Add(labels);

        return iData;
    }

当我从浏览器调用该方法时,出现以下错误:

System.InvalidOperationException:生成错误 XML 文档。 ---> System.InvalidOperationException:类型 System.Collections.Generic.List`1[[System.String, mscorlib, 版本=2.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089]] 不能在这种情况下使用。

我以off this为基础。

【问题讨论】:

    标签: c# web-services chart.js


    【解决方案1】:

    List 不能序列化为 Web 方法,您可以返回 object[]。

    [WebMethod]
        public object[] getProgram12Months(string usersessionid)
        {
            List<object> iData = new List<object>();
            List<string> labels = new List<string>();
    
            //First get distinct Month Name for select Year.
            string query1 = "SELECT DISTINCT TOP (100) PERCENT TimeFrame FROM dbo.CSQ_ProgramCount12Months ORDER BY TimeFrame ";
    
            string conn = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;
            SqlDataAdapter dap = new SqlDataAdapter(query1, conn);
            DataSet ds = new DataSet();
            dap.Fill(ds);
            DataTable dtLabels = ds.Tables[0];
    
    
            foreach (DataRow drow in dtLabels.Rows)
            {
                labels.Add(drow["TimeFrame"].ToString());
            }
            iData.Add(labels.ToArray());
    
            return iData.ToArray();
        }
    

    【讨论】:

    • 问题是我还需要为图表添加数据。这只是标签,我需要再添加九个 List 来构建图表。
    • 你可以像 Dictionary> 这样使用字典,这样你就可以在上面的字典中添加许多列表并通过其键名访问每个列表
    • 我尝试这种方式时得到以下信息: System.NotSupportedException: System.Collections.Generic.Dictionary2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Collections.Generic.List1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] 不受支持,因为它实现了 IDictionary。
    • List 或 Dictionary 不能序列化为 Web 方法,因此我将其转换为返回对象数组。我更新了我的答案。请仔细查看并告诉我
    • 返回:System.InvalidOperationException:生成 XML 文档时出错。 ---> System.InvalidOperationException: System.String[] 类型不能在此上下文中使用。
    【解决方案2】:

    我通过创建两个类并填充它们来解决这个问题。谢谢你,你的帖子帮助我朝着正确的方向前进。

    public class ChartData2
        {
            public List<string> Legends;
            public List<int> AD;
    
    
        }
    
        public class Legend
        {
            public List<string> Months;
        }
    

    【讨论】:

    • 哇,太好了,你的问题终于解决了。您请在答案中添加相关详细信息,以便对未来的读者有所帮助,或者您可以编辑我的答案以添加此类详细信息。选择是你的:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-05
    • 1970-01-01
    相关资源
    最近更新 更多