3

I have a strange issue that I can't wrap my head around. I am trying to create an "export to csv" function for my MVC4 application where the relevant JSON is passed via an ajax call to my ActionResult. The ActionResult deserializes the stringify'd JSON (with JSON.Net), writes it to a file in csv format, then returns the server path to the new file. My success callback then receives the path and calls the url to download.

This works fine locally, but on my live test server I get the following exception:

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

The JSON (and subsequently the objects they are deserialized to) are slightly complex. They come from a grouped subset of a SlickGrid DataView. I was getting circular reference exceptions when I included the aggregate information for column totals (this is only relevant to those that are versed in SlickGrid, I do not believe the data being passed to the server is an issue), but I've removed them before passing the JSON to the server. Here is my JSON to C# class structure:

[Serializable]
public class Row
{
    public int id { get; set; }
    public DateTime DateCreated { get; set; }
    public int RefNo { get; set; }
    public string ClientName { get; set; }
    public string Plate { get; set; }
    public string Address1 { get; set; }
    public int? ProductID { get; set; }
    public string Product { get; set; }
    public string S1 { get; set; }
    public string S2 { get; set; }
}

[Serializable]
public class RootReportObject
{
    public bool __group { get; set; }
    public int level { get; set; }
    public int count { get; set; }
    public string value { get; set; }
    public string title { get; set; }
    public int collapsed { get; set; }
    public List<Row> rows { get; set; }
    public object groups { get; set; }
    public string groupingKey { get; set; }
}

The only thing that I'm thinking is that, because of the way the data is structured, the List<> of rows in the root object may be throwing the circular references during deserializtion because a group does not necessarily have unique row references.

My question is why does it work fine locally?? I have no idea what I'm missing.

Mike H.
  • 1,731
  • 9
  • 31
  • Make sure you're actually serializing the thing that you think you are. You should only serialize raw data made up of numbers and strings and things like that - it appears that you're attempting to serialize something too complex. – Joe Enos Nov 12 '13 at 22:32
  • @JoeEnos why does it work fine locally though? This only happens on my test "live" server...being that it is a serialization issue I have to assume the server is missing something but not quite sure where to look. – Mike H. Nov 13 '13 at 15:26
  • I don't have an answer there - but if you're trying to serialize a SlickGrid object or whatever instead of the item you truly want to serialize, then who knows what would blow up. Once you know exactly what object you're serializing, or what JSON text you're deserializing, finding the problem should be easier, since you would then be able to break out your problem into a more reproducible environment. – Joe Enos Nov 13 '13 at 15:54
  • can you please add a serialized object, one which local works fine. try to add some logs to see how the object looks after release to compare with the local one. – arnoldrob Jan 17 '14 at 19:37
  • It would be helpful to see the code that you are deserializing with or even source JSON that throws the circular reference. If you suspect a specific field to be the culprit, you can use the `[ScriptIgnore]` attribute on that field in the target class to prevent the circular reference. – drankin2112 Jan 20 '14 at 18:33
  • @drankin2112 `[ScriptIgnore]` works, thank you very much. Time to find out why. Feel free to post as an answer for the bounty....any additional info would be appreciated. – Mike H. Jan 20 '14 at 21:23
  • What happens when you use `` in your development environment? – drankin2112 Jan 20 '14 at 21:33

1 Answers1

2

That's great that the [ScriptIgnore] attribute is helping. Also, something to be completely sure of is that all of your URL paths, including in your AJAX code, resolve correctly to the application root. When some of these are wrong, this is a notorious source of problems during the move from development to production.

It doesn't sound like it is necessarily the primary issue but I don't have any understanding of your app's design. It's definitely worth looking over.

drankin2112
  • 4,715
  • 1
  • 15
  • 20