I am using the library CsvHelper to read some data in a text file on the same location as the solution. The text file has about 12 000 lines, which isn't that many. But it takes like over 10 minutes or so and makes the page/browser say "This page isn't responding".
When reading the same file without using Tasks, but directly in the Main method, it takes less than a second to get the same amount (12 000) of records. Am I using the Task in a wrong way?
//method within the caller class
private static async Task<IResult> GetEmployees(IEmployeeData data)
{
try
{
return Results.Ok(await data.GetEmployees());
}
catch (Exception ex)
{
return Results.Problem(ex.Message);
}
}
//method within class that implements ICsvDataAccess
public Task<IEnumerable<T>> LoadData<T>(string path, bool hasHeaderRecord = false, string delimiter = ";")
{
return Task.Run(() =>
{
var config = new CsvConfiguration(CultureInfo.InvariantCulture)
{
HasHeaderRecord = hasHeaderRecord, Delimiter = delimiter
};
using (var reader = new StreamReader(path))
using (var csv = new CsvReader(reader, config))
{
return csv.GetRecords<T>().ToList().AsEnumerable();
}
});
}
//within EmployeeData class which implements IEmployeeData interface
private readonly ICsvDataAccess _file;
public Task<IEnumerable<EmployeeModel>> GetEmployees() =>
_file.LoadData<EmployeeModel>(path: "data.txt");
public interface IEmployeeData
{
Task<IEnumerable<EmployeeModel>> GetEmployees();
}
public interface ICsvDataAccess
{
Task<IEnumerable<T>> LoadData<T>(string path, bool hasHeaderRecord = false, string delimiter = ";");
}
Thanks!