Am I doing something wrong?
Well, you’re not really doing anything asynchronously. Your ReportExcelAsync
method is entirely synchronous, as it doesn’t have any await
expressions. So GenerateReportExcel
will call ReportExcelAsync
, which will run synchronously, and then return a completed task. All you’ve done at the moment is add a small amount of overhead for creating the state machine etc that is used to implement async
/await
.
It’s not clear why you expected it to actually happen asynchronously, but I suspect it’s a misunderstanding of what await/async actually does. It doesn’t start up new threads for you automatically – it just makes it much easier to create and consume asynchronous APIs.
Now one option is to just want to change ReportExcelAsync
into a synchronous method (ReportExcel
, returning a string
) and create a new task for that in the calling code:
var filePath = await Task.Run(excel.ReportExcel);
However, it’s not clear that this would actually give you much benefit. You’re writing a web app, so it’s not like the response is going to be delivered any faster this way – you’re effectively just shifting the thread the work is done on.
You say:
Since I need it to be done asynchronous
… but give no reasons why it needs to be done asynchronously. What’s wrong with the synchronous approach in this case? Asynchrony is great when it’s appropriate, but I don’t think it is in your case.