简体   繁体   English

httpclient异步/等待与否

[英]httpclient async/await or not

I'm using one httpclient instance to send multiple requests to rest web api to get data. 我正在使用一个httpclient实例发送多个请求来休息web api以获取数据。 Here is what my code looks like: 这是我的代码的样子:

First I have a control layer that calls the data layer for data. 首先,我有一个控制层,用于调用数据的数据层。

public class ControlLayer
{
    protected DataLayer dal;

    //constructors here

    public int getInfo1(int param)
    {
      int ret = this.dal.getInfo1(param);
      return ret;
    }

    public int getInfo2(int param)
    {
      int ret = this.dal.getInfo2(param);
      return ret;
    }
}

then I have the dataLayer that calls webAPI, it looks like this. 然后我有调用webAPI的dataLayer,它看起来像这样。 here for simplicity, im using .result directly. 这里为了简单起见,我直接使用.result。

public class DataLayer
{
    HttpClient client = new HttpClient();
    string url = "";

    public int getInfo1(int param1)
    {
      int ret=0;
      HttpResponseMessage response = client.GetAsync(url).Result;
      //.... do some work, get the value for ret

      return ret;
    }

    public int getInfo2(int param1)
    {
      int ret = 0;
      HttpResponseMessage response = client.GetAsync(url).Result;
      //.... do some work, get the value for ret

      return ret;
    }
}

my questions is I've seen some tutorials saying that we should avoid using .result, because it might lead to deadlock. 我的问题是我看到一些教程说我们应该避免使用.result,因为它可能导致死锁。 I'm not sure in my case do I need to use async/await? 在我的情况下,我不确定是否需要使用async / await? if I do need, I know I should async all the way down, but I do want my controlLayer to be sync, because I have other layer that calls the controlLayer's function, I don't want all the layer's function to be async and the result be Task<>, is this a situation of sync over async? 如果我确实需要,我知道我应该一直异步,但我确实想让我的controlLayer同步,因为我有其他层调用controlLayer的函数,我不希望所有图层的函数都是异步的结果是Task <>,这是异步同步的情况吗? am I miss something? 我想念一下吗? any suggestions are appreciated. 任何建议表示赞赏。 thanks! 谢谢!

I do want my controlLayer to be sync, because I have other layer that calls the controlLayer's function, I don't want all the layer's function to be async and the result be Task<> 我确实希望我的controlLayer是同步的,因为我有其他层调用controlLayer的函数,我不希望所有图层的函数都是异步的,结果是Task <>

I recommend you rethink this. 我建议你重新考虑一下。 A web request is a fundamentally asynchronous operation, so I recommend that you expose your "control layer" as an asynchronous API and allow the async to "grow" through the layers in your code base. Web请求是一种基本的异步操作,因此我建议您将“控制层”公开为异步API,并允许async通过代码库中的层“增长”。

However, if you really want a synchronous API, then you should only call synchronous APIs. 但是,如果您确实需要同步API,那么您应该只调用同步API。 Eg, use WebClient instead of HttpClient . 例如,使用WebClient而不是HttpClient Do not call Result to wrap an asynchronous API with a synchronous API. 别叫 Result来包装异步API与同步API。

You only need to use async and await if your code is asynchronous - for example if it dispatches several requests simultaneously, doing work after a request is sent instead of just blocking until the response arrives. 如果您的代码是异步的,您只需要使用asyncawait - 例如,如果它同时调度多个请求,则在发送请求后执行工作,而不是仅阻塞直到响应到达。

Ignoring the deadlock issue for a moment - if your code is simply synchronous, that is: every time you send a request you just wait for the response before doing anything else, you don't need to use await and can use Result . 暂时忽略死锁问题 - 如果您的代码只是同步,那就是:每次发送请求时,您只需等待响应,然后再执行其他操作,您不需要使用await并可以使用Result See this question for a similar debate. 看到这个问题进行类似的辩论。 Alternatively, you can use a synchronous API (such as WebClient as suggested in the comments and other answer). 或者,您可以使用同步API(例如评论和其他答案中建议的WebClient )。

As for Result -related deadlocks, I recommend you read this article on MSDN for a better understanding of what happens and why. 至于与Result相关的死锁,我建议您阅读MSDN上的这篇文章,以便更好地了解发生的情况和原因。 If you're writing a simple console application, you don't really need to worry about it (the right way to deal with it is to have only your Main method non-async, and use Result or Wait there). 如果你正在编写一个简单的控制台应用程序,你真的不需要担心它(处理它的正确方法是只让你的Main方法非异步,并使用ResultWait那里)。

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

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