"A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'"

32,362

Solution 1

Here we go for Solution

I modified my code with below set of code and it worked for me

public JsonResult populateData(string application, string columns, string machine, string pages, string startDate, string endDate)
    {
        ErrorPage _objError = new ErrorPage();
        var ErrorResult = _objError.GetErrorData(application, columns, machine, pages, startDate, endDate);


        var result = JsonConvert.SerializeObject(ErrorResult.ErrorData, Formatting.Indented,
                       new JsonSerializerSettings
                       {
                           ReferenceLoopHandling = ReferenceLoopHandling.Ignore
                       });

        return Json(result, JsonRequestBehavior.AllowGet);
    }

We need to serailize the object rather than sending a direct object of Model.

Thanks.

Solution 2

A circular reference happens when a property of an object has a property of its own that points back to the parent. It causes an infinite loop. Without seeing the details of what makes up the class ErrorPage, it'll be tough to tell you which property is responsible for this.

But typical solutions for this kind of thing are to either make a ViewModel which is identical to your class structure minus the circular references, or you could use https://json.codeplex.com/ which has some decoration attributes you can add to have a property ignored during serialization.

Solution 3

The error message is very explicit: The object you're tying to serialize (_objError) has a circular reference. This mean the one of it's properties is pointing to it's own instance directly or indirectly. Ex. instance A has a property pointing to instance B which points to instance A (like a parent property).

This is causing the serialization to fail because it would create an infinite loop (A.child = B / B.parent = A / A.child = B / ...). In order to fix this, you'll have to break the circular reference by ignoring the property causing the circular reference or by creating an other object that doesn't has such property.

Share:
32,362
Anand
Author by

Anand

Updated on July 09, 2022

Comments

  • Anand
    Anand almost 2 years

    I am trying to return the object of MVC Model in JSON result using jQuery. I am getting a failure message as:

    A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'

    This is my controller where i am returing the Json result

    public ActionResult populateData(string application, string columns, string machine, string pages, string startDate, string endDate)
        {
    
            ErrorPage _objError = new ErrorPage();
            _objError.ErrorData = dbl.GetDataTable(DbConnectionString, Table, whereCondition, columns);
    
    
            //Column description: Name and Type    
            var columnlist = new Dictionary<string, System.Type>();
            foreach (System.Data.DataColumn column in _objError.ErrorData.Columns)
            {       
                var t = System.Type.GetType( column.DataType.FullName );
                columnlist.Add(column.ColumnName, t);  
            }
    
            _objError.ErrorColumns = columnlist;
    
    
            //DataSourceRequest result = _objError.ToDataSourceResult(request);
    
            if (_objError.ErrorData.Rows.Count > 0)
                Message = "Showing Error log for " + AppName + " . To Change the application or filtering options please select the appropriate application from Application Dropdown";
            else
                Message = "No errors found for " + AppName + " in last 24 hours.";
    
    
            return Json(_objError);
        }
    

    Here i am giving a Ajax call to Controller method:

    $.ajax({
            type: "POST",
            url: '@Url.Content("~/Common/PopulateData")',
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            data: JSON.stringify({ application: app, columns: columns, machine: machine, pages: pages, startDate: startDate, endDate: endDate }),
            success: function (data) {
                alert("Success");
            },
            error: function (error) {
                alert('error; ' + eval(error));
                alert('error; ' + error.responseText);
            }
        });
    

    Kindly help how to return a Model class object to Ajax post call ?