Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在长时间运行http请求的过程中启动新的快速http请求_C#_Httpclient - Fatal编程技术网

C# 在长时间运行http请求的过程中启动新的快速http请求

C# 在长时间运行http请求的过程中启动新的快速http请求,c#,httpclient,C#,Httpclient,根据标题,我有一个长时间运行的GET请求正在执行,需要在等待长请求的响应时执行小而快速的请求。不幸的是,快速请求似乎必须等待长时间运行的请求执行后才能被允许执行。如果有助于可视化的话,这是一个web服务,它将请求转发给另一个web服务,并将结果返回给它的客户机,这样无论客户机何时启动它们,它们都可以进入 我尝试过使用相同的httpclient,不同的httpclient,我尝试过更改HttpClientHandler.MaxConnectionsPerServer和ServicePointMan

根据标题,我有一个长时间运行的GET请求正在执行,需要在等待长请求的响应时执行小而快速的请求。不幸的是,快速请求似乎必须等待长时间运行的请求执行后才能被允许执行。如果有助于可视化的话,这是一个web服务,它将请求转发给另一个web服务,并将结果返回给它的客户机,这样无论客户机何时启动它们,它们都可以进入

我尝试过使用相同的httpclient,不同的httpclient,我尝试过更改HttpClientHandler.MaxConnectionsPerServer和ServicePointManager.DefaultConnectionLimit,但似乎没有任何效果。我见过使用WhenAll等的解决方案,但这在这里不起作用,因为小请求是在长时间运行的请求很久以前启动并“WhenAll”之后出现的

我创建了一个测试应用程序,它是一个简单的windows窗体来解决这个问题,它看起来非常像这样(通过单击按钮来模拟接收到的web请求):

CookieContainer CookieContainer=null;
我的客户我的客户;
公共表格1()
{
初始化组件();
ServicePointManager.DefaultConnectionLimit=10;
myClient=新的myClient(参考CookieContainer);
}
私有异步无效按钮1\u单击(对象发送方,事件参数e)
{            
bool success=wait myClient.GetStockItemTables();
this.label1.Text=success.ToString();
}
私有异步无效按钮2\u单击(对象发送方,事件参数e)
{            
bool success=等待myClient.GetGiftCardBalance();
this.label2.Text=success.ToString();
}
类MyClient
{
字符串EndpointURL=@“XXX”;
私有HttpClient;
私有HttpClientHandler-ClientHandler;
公共MyClient(参考CookieContainer CookieContainer)
{
尝试
{
bool loginRequired=false;
if(cookieContainer==null)
{
loginRequired=true;
cookieContainer=新cookieContainer();
//clientHandler=new-HttpClientHandler{UseCookies=true,CookieContainer=new-CookieContainer()};
}
ClientHandler=new HttpClientHandler{UseCookies=true,CookieContainer=CookieContainer,MaxConnectionsPerServer=10};
Client=新的HttpClient(ClientHandler)
{
BaseAddress=新Uri(端点URL),
DefaultRequestHeader=
{
Accept={MediaTypeWithQualityHeaderValue.Parse(“text/json”)}
},
超时=TimeSpan.FromHours(1)
};
如果(需要登录)
多洛金(假);
}
捕获(例外情况除外)
{
}
}
公共异步任务LongRunningRequest()
{
尝试
{                
var result=await Client.GetAsync(EndpointURL+“YYY”);
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
公共异步任务QuickRequest()
{
尝试
{                
var result=await Client.GetAsync(EndpointURL+“ZZZ”);
返回true;
}
捕获(例外情况除外)
{
返回false;
}
}
}

传入/传出CookieContainer是为了维护登录信息,并排除登录方法,因为它可以工作,并且与问题没有实际关系。单击按钮1然后单击按钮2的结果是代码在快速请求“PostAsync”处“卡住”,直到长请求“GetAsync”完成。如果每次调用都使用自己的MyClient而不是共享全局MyClient,则结果相同。帮助?

看来不是我或密码。我正在使用的web服务阻止了来自同一会话的并发请求。将快速请求更改为只执行google的“get”会立即进行评估,不会像预期的那样被阻止。看起来,解决方案是为这些快速呼叫创建第二个会话,如果我们在长时间的请求中,它们不会被阻塞。

等待的是什么?见答案。是web服务阻止了并发请求。
    CookieContainer CookieContainer = null;
    MyClient myClient;

    public Form1()
    {
        InitializeComponent();
        ServicePointManager.DefaultConnectionLimit = 10;

        myClient = new MyClient(ref CookieContainer);
    }

    private async void button1_Click(object sender, EventArgs e)
    {            
        bool success = await myClient.GetStockItemTables();
        this.label1.Text = success.ToString();
    }

    private async void button2_Click(object sender, EventArgs e)
    {            
        bool success = await myClient.GetGiftCardBalance();
        this.label2.Text = success.ToString();
    }


class MyClient
{
    string EndpointURL = @"XXX";
    private HttpClient Client;
    private HttpClientHandler ClientHandler;       

    public MyClient(ref CookieContainer cookieContainer)
    {
        try
        {
            bool loginRequired = false;                

            if (cookieContainer == null)
            {
                loginRequired = true;
                cookieContainer = new CookieContainer();
                //clientHandler = new HttpClientHandler { UseCookies = true, CookieContainer = new CookieContainer() };
            }

            ClientHandler = new HttpClientHandler { UseCookies = true, CookieContainer = cookieContainer, MaxConnectionsPerServer = 10 };

            Client = new HttpClient(ClientHandler)
            {
                BaseAddress = new Uri(EndpointURL),
                DefaultRequestHeaders =
                {
                    Accept = {MediaTypeWithQualityHeaderValue.Parse("text/json")}
                },
                Timeout = TimeSpan.FromHours(1)
            };

            if (loginRequired)               
                DoLogin(false);                
        }
        catch (Exception ex)
        {

        }
    }
    public async Task<bool> LongRunningRequest()
    {

        try
        {                
            var result = await Client.GetAsync(EndpointURL + "YYY");

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
    public async Task<bool> QuickRequest()
    {

        try
        {                
            var result = await Client.GetAsync(EndpointURL + "ZZZ");

            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
}