简体   繁体   English

你能同步调用异步 web api controller 方法吗?

[英]Can you call an async web api controller method synchronously?

Let's say I have a webservice with the following API controller method:假设我有一个具有以下 API controller 方法的网络服务:

public async Task<ActionResult<string>> Get()
{
   await Task.Delay(1000);
   return Ok("Success");
}

It seems like I can call this api controller method synchronously like such好像我可以像这样同步调用这个 api controller 方法

var url = "http://example.com/api/example";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.Method = "GET";

string webStringResult;
using (HttpWebResponse httpResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
      var result = streamReader.ReadToEnd();
      webStringResult= JsonConvert.DeserializeObject<string>(result);
    }
}

I was shocked that this works.我很震惊这有效。 I thought that I would have to be forced to use httpWebRequest.GetResponseAsync()我以为我必须被迫使用httpWebRequest.GetResponseAsync()

But it seems like httpWebRequest.GetResponse() also works.但似乎httpWebRequest.GetResponse()也有效。

My question is, if this is correct workflow and what the implications are of using GetResponse() with an async web method versus GetResponseAsync() .我的问题是,如果这是正确的工作流程,以及将GetResponse()与异步 web 方法与GetResponseAsync() ) 一起使用的含义是什么。 Normally if you're calling an async method directly from another library/class you must be calling it from an async method and you have to await it.通常,如果您直接从另一个库/类调用异步方法,则必须从异步方法调用它,并且必须等待它。 However, making a web request is a little different since you're not technically directly calling the method.但是,发出 web 请求有点不同,因为从技术上讲,您并不是直接调用该方法。 So I was surprised to see that this worked and it would help to know the implications/understanding of what is going on here.所以我很惊讶地看到这有效,这将有助于了解这里发生的事情的含义/理解。

Normally if you're calling an async method directly from another library/class you must be calling it from an async method and you have to await it.通常,如果您直接从另一个库/类调用异步方法,则必须从异步方法调用它,并且必须等待它。

Yes, within the same process .是的,在同一个过程中

However, making a web request is a little different since you're not technically directly calling the method.但是,发出 web 请求有点不同,因为从技术上讲,您并不是直接调用该方法。

Right.正确的。 The client isn't calling the server method at all.客户端根本不调用服务器方法。 It's making an HTTP request to a server.它正在向服务器发出 HTTP 请求。 The server application handles that HTTP request by invoking the server method.服务器应用程序通过调用服务器方法来处理 HTTP 请求。

Since these are different processes, the server doesn't care whether the client is synchronous or asynchronous;由于这些是不同的进程,服务器不关心客户端是同步的还是异步的; and the client doesn't care whether the server is synchronous or asynchronous.并且客户端不关心服务器是同步的还是异步的。 They're both just talking over the.network.他们都只是在通过网络交谈。

if this is correct workflow and what the implications are of using GetResponse() with an async web method versus GetResponseAsync().如果这是正确的工作流程,以及将 GetResponse() 与异步 web 方法一起使用与 GetResponseAsync() 相比有何影响。

GetResponse just means your client is synchronous. GetResponse仅表示您的客户端是同步的。 I'd say it would technically be more proper as an asynchronous method, since it is I/O-based, but if you're fine with a synchronous client, then that's totally fine.我会说它在技术上更适合作为一种异步方法,因为它是基于 I/O 的,但是如果您对同步客户端没问题,那就完全没问题了。

One way to think about it is to assume that the client and the server are written in different languages and the server code was written 20 years ago.一种思考方式是假设客户端和服务器是用不同的语言编写的,并且服务器代码是 20 年前编写的。 They can work together because they use the same protocol (http in this case) and not because they are written in the same version of a language.它们可以协同工作,因为它们使用相同的协议(在本例中为 http),而不是因为它们是用相同版本的语言编写的。

Having said that, please note that this separation - achieved by using the http protocol - is true even if you running both on the same machine, and even as part of the same process.话虽如此,请注意这种分离 - 通过使用 http 协议实现 - 即使您在同一台机器上运行两者,甚至作为同一进程的一部分,也是如此。 For example, in a test, you could start a web server and make http calls to it, async or not, from the same program.例如,在测试中,您可以启动一个 web 服务器并从同一个程序对其进行 http 次异步调用或非异步调用。

Another way to think about it is to imagine that the client and server use a 'file protocol': if the client wants work done it drops a file with json in a special folder;另一种思考方式是想象客户端和服务器使用“文件协议”:如果客户端想要完成工作,它会在一个特殊文件夹中放置一个带有 json 的文件; the sever monitors this folder reads the file and later saves the response as 'response.json'.服务器监视此文件夹读取文件,然后将响应保存为“response.json”。 The client could write the file with await File.WriteAllTextAsync and the server could read the file with File.ReadAllText (not async).客户端可以使用await File.WriteAllTextAsync写入文件,服务器可以使用File.ReadAllText (非异步)读取文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM