如何找到原因"在序列化时检测到循环引用....' System.Reflection.RuntimeModule'

时间:2016-08-08 05:10:25

标签: asp.net asp.net-mvc circular-reference

我已经使用两个提交操作设置了MyView.chstml视图。但是请给我"在序列化类型为' System.Reflection.RuntimeModule'的对象时检测到循环引用。如何轻松排除故障?

 @using (Html.BeginForm("MyView", "Worker", FormMethod.Post, new { enctype = "multipart/form-data", @name = "formWorker" }))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal" id="divWork"> ....
        <div class="form-group">
             <button id="btnSubmit" class="btn btn-success" type="submit" value="Work1">Work1</button>
        </div>   
        <div id="Dynamictable" class="table-responsive hidden" name="dtable">
                <table class="table table-bordered" id="dctable" name="dctable"></table>
        </div>

       <div id="dialogsubmit" title="second submit">
                <div id="dialog-content" name="dialog-content" class="form-control hidden">    &nbsp;</div>
            </div>  
   </div>

然后在脚本中        

    (function () {
    //strangely the below ajax is never called
     $('#formWorker').submit(function () {
        debugger;
        $.ajax({
                url: this.action,
                type: this.method,
                data: $(this).serialize(),
                success: function (result) {
                    debugger;
                    bootbox.alert(result);
                }
            });
            // it is important to return false in order to
            // cancel the default submission of the form
            // and perform the AJAX call
            return false;
     });
    }
      $("#btnSubmit").click(function () {

        if(document.getElementById('dctable').getElementsByTagName("tr").length < 1)
        {
            aRow = document.all("dctable").insertRow();
            var oCell = aRow.insertCell();
            oCell = newRow.insertCell(); 
            oCell.innerHTML = '<input type="submit" name="submit2" value="submit2" />';
            **//strangely if i replace the above RHS with below, it act as submit halft submit form (half becuase the FormCollection object in HttpPost method of controller lacks key submit2 and also HttpPostedFileBase object came as null in controller method ,**  
            *//'<button id="submit2" name="submit2" class="btn submit2" value="submit2" ><i class="fa fa-search"></i> submit2</button>'*
            return false;
        }

      });



   </script>
控制器中的

       [HttpGet]
        public ActionResult Worker()
        {
            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Worker(HttpPostedFileBase file, FormCollection form)
        {
          string value = form["submit2"];  //half submit form  as it is null in additon to file is null. if i use commented RHS , all good here, but no option in view to process the output in same view.
          IEnumerable<object> data = Enumerable.Empty<object>();
          if (value != null) // key doesn't exist
          {
            //process here and and return json to shown result on same page using popup/alert.
            return this.Json(
                new 
                {
                    Result = data.ToList()
                }, JsonRequestBehavior.AllowGet
            );
           }
           return PartialView("anoterhView", data.ToList());
        }

1 个答案:

答案 0 :(得分:1)

当JSON序列化程序启动一个有效导致无限循环序列化的循环时,通常会引发循环引用异常。当您查看可序列化属性的架构时会发生这种情况。例如:

参加以下两个课程:

public class Node
{
    public Node Parent { get; set; }
}
public class Parent : Node
{
    public List<Node> Children { get; set; } = new List<Node>();
}

正如您所看到的,我们有两个简单的类,一个Node类,其公共属性Parent类型为Node。现在,如果我们查看Parent类,它会从Node中获取,并且属性ChildrenList<Node>。这是我们开始出现循环依赖问题的地方。请考虑以下简单方法。

public string SerializeJsonObject()
{
    var parent = new Parent();
    var child = new Node();
    child.Parent = parent;
    parent.Children.Add(child);

    return Json(parent);
}

此方法构建Parent对象parent,然后构建Node对象child。接下来,我们将Parent的{​​{1}}属性设置为child实例。然后我们parentAdd添加到父级child列表中。

现在考虑父级的序列化。

Children

从这里我们可以看到我们的循环依赖是-- Parent Item -- Parent: null no procesing -- Children : Serialize each Node object -- Node 1 -- Parent -- Parent: null no processing -- Children: -- Node 1 -- Parent --Parent: null no processing --Children: -- Node 1 ..... continues forever never finishing serializing Node 1 属性,但它只是一个循环引用,因为Parent引用了Parent中的Node采集。因此,序列化程序永远无法完成对象的序列化。

现在这不限于列表,我们可以看一个类似的例子,其中两个类彼此有引用并且都是可序列化的。考虑以下课程。

Children

此类对public class Node { public string Name { get; set; } public Node Previous { get; set; } public Node Next { get; set; } } Node属性都Previous有依赖关系。因此给出了一种构建小数据集的方法。

Next

这非常简单,但最终会有10个对象,其中1-9依赖于public static object SerailizeANode() { Node first = null; Node previous = null; for(var i = 0; i < 10; i++) { var current = new Node(); current.Name = $"Node {i}"; if(previous != null) { previous.Next = current; current.Previous = previous; } previous = current; if (first == null) first = current; } return Json(first); } 节点,而对象Next依赖于2-10节点。因此,鉴于Previous

的序列化
first

再次,我们通过属性(-- first -- Name: Node 0 -- Previous: Null -- Next: -- Name: Node 1 -- Previous -- Name: Node 0 -- Previous: null -- Next: -- Name: Node 1 -- Previous: -- Name: Node 0 -- Previous: null -- Next: --Name: Node 1 continues on forever. &amp; Previous)的依赖序列化看到,导致序列化程序命中循环引用(无限循环)并抛出异常。

我期望在数据中出现类似的问题,通过该部分很好地注释回到浏览器。

Next

如果您需要更多信息,请在注释掉的部分中发布所返回类的架构。