使用RESTSHAR进行并行呼叫,而无需对每个呼叫实例化restclient

发布于 2025-01-29 17:15:20 字数 1770 浏览 3 评论 0原文

我有一个.NET标准2.0库,该库已由各种项目(.NET Framework,.net Core,UWP,Xamarin等)使用。基本上,这是一个数据客户端,项目可以调用以从Web服务API方法获取数据。我试图弄清楚如何并行呼叫方法时如何提高性能。示例Web服务的帖子通话代码如下。 通常所有方法都将使用_Client,但这需要42秒才能完成50个并行呼叫。如果我对每个呼叫进行实例化restclient(代码中的评论行)并将其用于等待,则50个并行呼叫需要10秒钟才能完成。但是,RestSharp接下来的文档说不执行此操作,因为连接池可能会筋疲力尽。

性能命中可能与ServicePointManager.defaultConnectionLimit 2有关,因为如果使用静态_Client,则可以看到对Web服务的调用。在挖掘时,我看到需要在启动RESTCLIENT之前设置ServicePointManager.defaultConnectionLimit,但我不确定如何在.NET标准库中进行操作。基本问题:我可以在10秒内进行50次通话,而无需在每个电话中实例化客户端吗?如果是这样,怎么样?请注意,这是一个测试场景。在产品中,我希望有200-300个并行电话。

using System; 
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using RestSharp;

namespace DataClient.Helpers
{
    public static class ApiHelper
    {
        static string baseurl = "xxxxx";
        static RestClient _client = new RestClient(baseurl);

        public static async Task<RestResponse> PostResultAsync(string endpoint, string data, bool useApikey)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 50;
            //RestClient client = new RestClient(baseurl);
            RestRequest request = new RestRequest(endpoint, Method.Post);
            request.AddStringBody(data, DataFormat.Json);
            request.AddHeader("Authorization", string.Format("Bearer {0}", Settings.AccessToken));

            if (useApikey)
            {
                request.AddHeader("apikey", "xxxxx");
            }

            try
            {
                var response = await _client.PostAsync(request);
                return response;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
    }
}

I've got a .NET Standard 2.0 library that's used by a variety of projects (.NET Framework, .NET Core, UWP, Xamarin, etc.). It's basically a data client that projects can call to get data from web service API methods. I'm trying to figure out how to improve performance when calls to a method happen in parallel. Example web service calling code for a POST is below. Normally all methods would use _client but this takes 42 seconds to complete 50 parallel calls. If I instantiate RestClient for every call (commented line in code) and use that for the await, 50 parallel calls take 10 seconds to complete. However the RestSharp Next docs say not to do this as the connection pool could become exhausted.

The performance hit may have to do with the ServicePointManager.DefaultConnectionLimit of 2 as I can sort of see calls coming in twos to the web service if the static _client is used. Digging around, I see that ServicePointManager.DefaultConnectionLimit needs to be set before the RestClient is initiated but I'm not sure how to do that in a .NET Standard library. Basic question: Can I get the 50 calls in 10 seconds performance without instantiating a client for every call? If so, how? Note this is a test scenario. In prod, I expect to have 200-300 parallel calls.

using System; 
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using RestSharp;

namespace DataClient.Helpers
{
    public static class ApiHelper
    {
        static string baseurl = "xxxxx";
        static RestClient _client = new RestClient(baseurl);

        public static async Task<RestResponse> PostResultAsync(string endpoint, string data, bool useApikey)
        {
            System.Net.ServicePointManager.DefaultConnectionLimit = 50;
            //RestClient client = new RestClient(baseurl);
            RestRequest request = new RestRequest(endpoint, Method.Post);
            request.AddStringBody(data, DataFormat.Json);
            request.AddHeader("Authorization", string.Format("Bearer {0}", Settings.AccessToken));

            if (useApikey)
            {
                request.AddHeader("apikey", "xxxxx");
            }

            try
            {
                var response = await _client.PostAsync(request);
                return response;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文