Is there an example of Enqueue() to read/load a file using CSVHelper?

I wondered if anyone has tried to Enqueue a job that uses CSVHelper to load a file into a SQL Server database. I am just looking for example code to use as a template for my app. I have read through all of the Hangfire documentation on docs.hangfire.io and am still learning to use this great tool.

My code that will be Enqueued looks something like this:

       using (var reader = new StreamReader(model.ImportFile.OpenReadStream()))
       using (var csv = new CsvReader(reader))
        {
            csv.Configuration.HeaderValidated = null;
            csv.Configuration.MissingFieldFound = null;
            var record = new MyModel();
            var records = csv.EnumerateRecords(record);
            int recordCount = 0;
            foreach (var csvRecord in records)
            { ...
               await _dbContext.TableName.AddAsync(MyModel);
               await _dbContext.SaveChangesAsync();
            }

Thanks!

By the way, this is a ASP.NET Core 3.0 MVC web app.

The main question I have is do I need to define and instantiate my interfaces and services again in the enqueued method, or does the background job inherit those definitions from the Controller class it’s being enqueued from?

First attempt at just Enqueuing it and I got this error:

Newtonsoft.Json.JsonSerializationException: Error setting value to ‘ContentDisposition’ on ‘Microsoft.AspNetCore.Http.FormFile’.
—> System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.AspNetCore.Http.FormFile.set_ContentDisposition(String value)
at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value)
— End of inner exception stack trace —

I think I have to pass something into the method or define something in the method, but not sure what.

It seems like you can’t pass in an IFormFile object to an Enqueued task because it cannot be serialized? Is that true, or do I have to do something in my method to allow the background task to work with IFormFile?

Here’s my method:

   public async Task ImportAsync(IFormFile importFile, int userId)

This same question has been asked before but not answered:
http://hangfire.discourse.group/t/interface-or-abstract-class-and-cannot-be-instantiated/5442