• 调用WebService时报A circular reference was detected while serializing an object 错误
  • 发布于 1周前
  • 36 热度
    1 评论
  • 赵金阳
  • 0 粉丝 13 篇博客
  •   

最近有个系统在用WebService方式调用另一个程序获取数据时,系统报如下错误:System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.InvalidOperationException: There was an error generating the XML document. ---> System.InvalidOperationException: A circular reference was detected while serializing an object of type Com.ABC.CN.PDP.Entity.PDPMain 。

我查看了下那个WebService的方法,就是通过Linq返回一个HowDevelopmentPlan List对象,有谁知道这是什么问题,该怎么解决?

 public static IEnumerable<HowDevelopmentPlan> GetHowDevelopmentPlan(string dynamicConditions)
        {
            PDPDataContext dc = new PDPDataContext(BaseBLL.ConnectionString());
            return (from gHowDevelopmentPlan in dc.HowDevelopmentPlans.Where(dynamicConditions) select gHowDevelopmentPlan);
        }


用户评论
  • 洪峰
  • 你查看一下你的HowDevelopmentPlan这张表是不是和其它表有建立主外键关系,所以系统在序列化HowDevelopmentPlan这个对象因为Linq的主外键关系,在序列化的这个过程中,由于这个对象有映射关系,那么它在序列化HowDevelopmentPlan对象的时候会序列化该对象的属性外键对象,而这个属性外键对象又有属性对象引用HowDevelopmentPlan,依次反复。就导致了这个问题的产生。

    解决方法:

    不要直接返回Linq查询的值,而是自己新建一个HowDevelopmentPlan List对象,把Linq返回的每一个值重新实例化一个对象加入List中,并最终返回这个List,类似这样:

     List<HowDevelopmentPlan> listInfo = new List<HowDevelopmentPlan>();
                  var result= HowDevelopmentPlanBLL.GetHowDevelopmentPlan("搜索条件").ToList();
                  foreach (var item in result)
                  {
                      HowDevelopmentPlan newItem = new HowDevelopmentPlan();
                      newItem.CompleteCondition = item.CompleteCondition;
                      newItem.CreateBy = item.CreateBy;
                      newItem.CreateTime = item.CreateTime;
                      newItem.DevelopmentArea = item.DevelopmentArea;
                      newItem.DevelopmentPlan = item.DevelopmentPlan;
                      newItem.DevelopmentPlanID = item.DevelopmentPlanID;
                      newItem.DevelopmentType = item.DevelopmentType;
                      newItem.PDPStatusCode = item.PDPStatusCode;
                      newItem.PlanCompleteTime = item.PlanCompleteTime;
                      newItem.RequestID = item.RequestID;
                      newItem.UpdateBy = item.UpdateBy;
                      newItem.UpdateTime = item.UpdateTime;
                      listInfo.Add(newItem);
                  }
                    return listInfo;

  • 2018/8/31 11:12:00 [ 0 ] [ 0 ] 回复