简体   繁体   English

ASP.NET core & C#. 如何异步运行同步方法

[英]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.

相关问题 如何在 Asp.Net MVC Core 3.1 c# 中实现 HttpDelete 和 HttpPut。 解决 HTTP 错误 405? - How to Implement HttpDelete and HttpPut in Asp.Net MVC Core 3.1 c#. Resolve HTTP ERROR 405? 缓冲流 - ASP.NET Core 3.0 中不允许同步操作 - Buffered Stream - Synchronous operations are disallowed in ASP.NET Core 3.0 使用同步方法在ASP.NET Core中导入WCF服务 - Importing WCF services in ASP.NET core with synchronous methods ASP.NET Core MVC 异步加载会话 - ASP.NET Core MVC Loading Session Asynchronously 在ASP.NET Core中异步保存文件导致DbContext异常 - Saving a file asynchronously in ASP.NET Core causing exceptions with DbContext 在 ASP.NET 核心 6 中,是否由异步枚举的 controller 操作返回的 IQueryable? - In ASP.NET Core 6, is an IQueryable returned by a controller action enumerated asynchronously? 如何在ASP.NET CORE上将同步结构转换为异步结构 - How can I turn a synchronous structure to asynchronous on ASP.NET CORE 我可以在asp.net核心上运行C#游戏吗? - Can I run a C# game on asp.net core? c#文件更改后如何自动运行asp.net核心应用 - How to run asp.net core app automatically after changes in c# files 如何在Nginx中运行具有不同端口的C#ASP.NET Core 2.1和Node.js? - How to Run C# ASP.NET Core 2.1 and Node.js with Different Ports in Nginx?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM