我在Web应用程序中具有ASMX service
的以下代码。尝试获取XML格式的数据。为了清楚起见,我删除了一些数据
/// <summary>
/// Summary description for CompanyServices
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script,using ASP.NET AJAX,uncomment the following line.
// [system.web.Script.Services.scriptservice]
public class CompanyServices: system.web.Services.WebService
{
[WebMethod]
public List<Product> GetData(int companyId,int custId)
{
return ProductService.getcompanyData(companyId,custId).ToList();
}
在调试模式下所有运行,但我不断收到错误消息
System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: A circular reference was detected while serializing an object of type Company
所以我创建了相同的类型,并且围绕它的每个循环都使用了类似的
foreach (Product p in ProductService.getcompanyData(companyId,custId).ToList())
{
Product newProd = new Product
newProd.Name = p.Name;
newProd.Department = p.Department;
}
这一直有效,直到我添加了Department,该部门已链接到另一个表(部门)
在Google周围四处搜寻,但不知道是什么原因引起的或如何解决?