简体   繁体   中英

File not being released by IIS when processing

In an ASP.Net MVC4 application, I'm using the following code to process a Go To Webinar Attendees report (CSV format).

For some reason, the file that is being loaded is not being released by IIS and it is causing issues when attempting to process another file.

Do you see anything out of the ordinary here?

The CSVHelper (CsvReader) is from https://joshclose.github.io/CsvHelper/

public AttendeesData GetRecords(string filename, string webinarKey)
{
    StreamReader sr = new StreamReader(Server.MapPath(filename));
    CsvReader csvread = new CsvReader(sr);
    csvread.Configuration.HasHeaderRecord = false;
    List<AttendeeRecord> record = csvread.GetRecords<AttendeeRecord>().ToList();
    record.RemoveRange(0, 7);
    AttendeesData attdata = new AttendeesData();
    attdata.Attendees = new List<Attendee>();
    foreach (var rec in record) 
    {
        Attendee aa = new Attendee();
        aa.Webinarkey = webinarKey;
        aa.FullName = String.Concat(rec.First_Name, " ", rec.Last_Name);
        aa.AttendedWebinar = 0;
        aa.Email = rec.Email_Address;
        aa.JoinTime = rec.Join_Time.Replace(" CST", "");
        aa.LeaveTime = rec.Leave_Time.Replace(" CST", "");
        aa.TimeInSession = rec.Time_in_Session.Replace("hour", "hr").Replace("minute", "min");
        aa.Makeup = 0;
        aa.RegistrantKey = Registrants.Where(x => x.email == rec.Email_Address).FirstOrDefault().registrantKey;

        List<string> firstPolls = new List<string>()
        {
           rec.Poll_1.Trim(), rec.Poll_2.Trim(),rec.Poll_3.Trim(),rec.Poll_4.Trim()
        };
        int pass1 = firstPolls.Count(x => x != "");

        List<string> secondPolls = new List<string>()
        {
           rec.Poll_5.Trim(), rec.Poll_6.Trim(),rec.Poll_7.Trim(),rec.Poll_8.Trim()
        };
        int pass2 = secondPolls.Count(x => x != "");

        aa.FirstPollCount = pass1;
        aa.SecondPollCount = pass2;

        if (aa.TimeInSession != "")
        {
            aa.AttendedWebinar = 1;
        }
        if (aa.FirstPollCount == 0 || aa.SecondPollCount == 0)
        {
            aa.AttendedWebinar = 0;
        }

        attdata.Attendees.Add(aa);
        attendeeToDB(aa); // adds to Oracle DB using EF6.
    }
    // Should I call csvread.Dispose() here?
    sr.Close();
    return attdata;
}

Yes. You have to dispose objects too.

sr.Close();
csvread.Dispose();
sr.Dispose();

Better strategy to use using keyword.

You should use usings for your streamreaders and writers.

You should follow some naming conventions (Lists contains always multiple entries, rename record to records)

You should use clear names (not aa)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM