简体   繁体   English

HttpClient异步-太快跳过Web服务请求

[英]HttpClient async - Too fast skipping webservice requests

I have an issue where i loop over about 31 webservice URLs. 我有一个问题,在其中我遍历了约31个Web服务URL。 If i put a Thread.Sleep(1000) in the top code, it will work perfectly, but if I remove this, I only get success on 10 (sometimes less and sometimes more) request out of 31. How do I make it wait? 如果我将Thread.Sleep(1000)放在顶部代码中,它将很好地工作,但是如果删除此代码,我只会在31个请求中有10个(有时更少,有时更多)请求获得成功。如何使其等待?

Code

foreach(var item in ss)
{ 
   //Call metaDataApi(url,conn,name,alias)
}

 public static void metadataApi(string _url, string _connstring, string _spname, string _alias)
        {
           // Thread.Sleep(1000);
            //Metadata creation - Table Creation
            using (var httpClient = new HttpClient())
            {
                string url = _url;

                using (HttpResponseMessage response = httpClient.GetAsync(url).GetAwaiter().GetResult())
                using (HttpContent content = response.Content)
                {
                    Console.WriteLine("CHECKING");
                    if (response.IsSuccessStatusCode)
                    {
                        Console.WriteLine("IS OK");
                        string json = content.ReadAsStringAsync().GetAwaiter().GetResult();

                       //Doing some stuff not relevant

                     }
                }
            }
         }

How it can look 看起来如何 在此处输入图片说明

You should look to use async/await where you can, but you could try something like this: 您应该尝试在可能的地方使用async/await ,但是您可以尝试执行以下操作:

// you should share this for connection pooling  
public static HttpClient = new HttpClient();

public static void Main(string[] args)
{
    // build a list of tasks to wait on, then wait
    var tasks = ss.Select(x => metadataApi(url, conn, name, alias)).ToArray();
    Task.WaitAll(tasks);
}

public static async Task metadataApi(string _url, string _connstring, string _spname, string _alias)
{
    string url = _url;
    var response = await httpClient.GetAsync(url);
    Console.WriteLine("CHECKING");
    if (response.IsSuccessStatusCode)
    {
        Console.WriteLine("IS OK");
        string json = await content.ReadAsStringAsync();

        //Doing some stuff not relevant

    }
}

One thing to note, this will try to run many in parallel. 需要注意的一件事是,这将尝试并行运行多个。 If you need to run them all one after the other, may want to make another async function that waits on each result individually and call that from the Main . 如果您需要一个接一个地运行它们,则可能需要创建另一个async函数,该函数分别等待每个结果并从Main调用该函数。 .Result is a bit of an antipattern (with modern c# syntax, you can use async on the main function) but for your script it should be "ok", but I'd minimize usage of it (hence why I wouldn't use .Result inside of a loop. .Result有点反模式(使用现代c#语法,您可以在主函数上使用async ),但是对于您的脚本,它应该是“ ok”,但我会尽量减少使用它(因此为什么我不使用.Result一个循环内。

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

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