[英]ASP.NET core & C#. How to run a synchronous method asynchronously
My ASP.NET core application uses Entity Framework Core.我的 ASP.NET 核心应用程序使用 Entity Framework Core。 As you would expect must controller methods are async and call async methods of EF Core.
正如您所期望的那样,controller 方法必须是异步的并调用 EF Core 的异步方法。
I also have controller methods thats need to read from and write to excel files.我还有 controller 个方法需要读取和写入 excel 个文件。 I'm using OpenXml.
我正在使用 OpenXml。 Since these are IO operation, ideally I they would be an async operation but OpenXml doesn't offer any async methods.
由于这些是 IO 操作,理想情况下它们将是异步操作,但 OpenXml 不提供任何异步方法。 Here is a simplified example
这是一个简化的例子
private async Task<model> ReadFromExcel()
{
using var document = SpreadsheetDocument.Open("filePathAndName", false);
// read data into model
document.Close();
context.Models.Add(newModel);
await Context.SaveAsync();
return newModel;
}
Also, I need to find the file in a folder first which I would also like to make async.另外,我需要先在一个文件夹中找到该文件,我也想将其设为异步。
Directory.EnumerateFiles("excelFolderName", ".xlsx");
According to this document ASP.NET Core Performance Best Practices I shouldn't use Task.Run to make an synchronous API asynchronous.根据此文档ASP.NET Core Performance Best Practices我不应该使用 Task.Run 来使同步 API 异步。 I understand why but does that rule apply to IO operations which will block the thread potential for a few seconds?
我明白为什么,但该规则是否适用于 IO 操作,这将阻塞线程潜力几秒钟? Should I make these IO operations async and if so what is the base way to make reading and writing excel file and getting file list asynchronous?
我应该使这些 IO 操作异步吗?如果是这样,使读写 excel 文件和获取文件列表异步的基本方法是什么?
Since these are IO operation, ideally I they would be an async operation but OpenXml doesn't offer any async methods.
由于这些是 IO 操作,理想情况下它们将是异步操作,但 OpenXml 不提供任何异步方法。
Also, I need to find the file in a folder first which I would also like to make async.
另外,我需要先在一个文件夹中找到该文件,我也想将其设为异步。
Ideally, those would be asynchronous APIs.理想情况下,这些将是异步 API。 But they're not.
但他们不是。 The way to make them asynchronous is to fix the API, not wrap it in
Task.Run
.使它们异步的方法是修复 API,而不是将其包装在
Task.Run
中。 You can open a request with the maintainers of OpenXml for asynchronous APIs.您可以向 OpenXml 的维护者提出请求以获取异步 API。 The file system operation is more awkward;
文件系统操作比较笨拙; it's a Win32 limitation, not a BCL limitation, and it's unlikely to be fixed, but you can ask.
这是 Win32 限制,而不是 BCL 限制,不太可能修复,但您可以询问。
does that rule apply to IO operations which will block the thread potential for a few seconds?
该规则是否适用于 IO 操作,这将阻塞线程潜力几秒钟?
Yes.是的。
The request is blocked for the same amount of time whether it's synchronous or asynchronous.无论请求是同步的还是异步的,请求都会被阻塞相同的时间。 So the thing to consider is how threads are blocked.
所以要考虑的是线程是如何被阻塞的。 In the ideal asynchronous case, no threads are blocked.
在理想的异步情况下,没有线程被阻塞。 Since you only have synchronous APIs, you do have to block a thread;
由于您只有同步 API,因此您必须阻塞一个线程; calling the API directly will block a thread pool thread, and shoving it off to
Task.Run
will block a different thread pool thread - which is pointless.直接调用 API 将阻塞一个线程池线程,并将其推到
Task.Run
将阻塞另一个线程池线程——这是没有意义的。
Should I make these IO operations async and if so what is the base way to make reading and writing excel file and getting file list asynchronous?
我应该使这些 IO 操作异步吗?如果是这样,使读写 excel 文件和获取文件列表异步的基本方法是什么?
You can't "make them async".你不能“让他们异步”。 You can request async APIs and then use the synchronous ones for now.
您可以请求异步 API,然后暂时使用同步 API。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.