Tiara Dwi Dikiyanti
Tiara Dwi Dikiyanti

Reputation: 85

How to call async Task<List<>> class in another class?

I have this method

public async Task<List<InvoiceVAT>> Import(IFormFile file)
{
    var list = new List<InvoiceVAT>();
    using (var stream = new MemoryStream())
    {
        await file.CopyToAsync(stream);
        using (var package = new ExcelPackage(stream))
        {
        }
    }
    return list;
}

But am receiving the following error when attempting to call it:

There is no argument given that corresponds to the required formal parameter

There's also a warning:

Because this call is not awaited, execution of the current method continuous before the call is completed

Where am I going wrong?

Upvotes: 0

Views: 909

Answers (1)

Ali Nazem
Ali Nazem

Reputation: 116

The first error, "There is no argument given that corresponds to the required formal parameter", means you have not provided the IFormFile argument required by Import() method.

The second one,"Because this call is not awaited, execution of the current method continuous before the call is completed.", is raised because the Import() is defined as an asynchronous method and the call to it should be awaited. This implies that either the caller method is also async, where Import() should be called like this:

IFormFile formFile; //Assuming formFile is initiated or provided.
var invoiceVATs = await Import(formFile); //Assuming this is an instance method within the same class as Import()

Or you need to synchronously obtain the Result of the asynchronous method (block the caller method and wait on the asynchronous method to complete), which is not the recommended approach:

IFormFile formFile; //Assuming formFile is initiated or provided.
var invoiceVATs = Import(formFile).Result; //Assuming this is an instance method within the same class as Import()

If you want to know more about asynchropnous programming here is a great material: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/async/

Upvotes: 4

Related Questions