我正在使用 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: There was an error generating the XML document. ---> System.InvalidOperationException: The type System.Collections.Generic.List`1[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] may not be used in this context.
我的基础是off this .
最佳答案
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();
}
关于c# - 从 web 服务返回列表<object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54842784/