darren
darren

Reputation: 41

StackOverflow Exception CSVHelper Writer

I am getting a stackoverflow exception when writing a new csv file using csvhelper. Exception is raised on the line:

csv.WriteRecords(listToSave)

Originally thought it was down to using generic List but have also tried with a concrete list and can replicate it with any sized list and any type of list. All lists are created using Entity Framework .ToList() have also tried AsEnumerable() etc...

Not sure if I'm missing something blindingly obvious?

Any help to resolve, much appreciated.

public string SaveTemporaryCsvToFileSystem<T>(IEnumerable<T> listToSave, string fileName)
{
    //Store in folder
    string folder = "C:\\inetpub\\wwwroot\\Content\\EmailOutput\\Temporary Files\\";
    string filePath = fileName + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year + ".CSV";

    if(!Directory.Exists(folder))
    {
        Directory.CreateDirectory(folder);
    }

    using (TextWriter writer = new StreamWriter(folder + filePath))
    {
        var csv = new CsvWriter(writer);
        csv.WriteRecords(listToSave);
    }

    var zippedFile = ZipSecurely(folder + filePath);
    DeleteUnsecureFile(folder + filePath);

    return zippedFile;
}

Upvotes: 4

Views: 2216

Answers (3)

Nichsen
Nichsen

Reputation: 11

parameterless constructor is the keyword to solve this issue ;-)

Upvotes: 0

Daryl
Daryl

Reputation: 610

My problem was that the class I wanted to write didn't have a parameterless constructor.

Upvotes: 4

Hinek
Hinek

Reputation: 9739

I had this problem, when the type I was saving had a Uri property. It seems, that the structure of the System.Uri class causes some recursion.

Does your type have any properties, that are not of basic types?

Upvotes: 1

Related Questions