gpt4 book ai didi

c# - 为什么 HttpWebRequest.BeginGetResponse() 同步完成?

转载 作者:太空狗 更新时间:2023-10-29 23:09:21 25 4
gpt4 key购买 nike

我正在高负载下测试 ASP.NET (.NET 4) 网络应用程序,发现在某些情况下 HttpWebRequest.BeginGetResponse() 会同步完成,不会抛出任何异常。

在高负载下在多个 ASP.NET 线程中运行以下代码后,我发现“WEBR​​EQUEST COMPLETED SYNC!”日志中的消息。

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
var result = webRequest.BeginGetResponse(internalCallback, userState);
if (result.CompletedSynchronously)
{
Trace.Error("WEBREQUEST COMPLETED SYNC!");
}

注意:

  1. 如果达到线程池容量,将抛出 InvalidOperationException
  2. 如果在连接过程中发生错误,则抛出相应的异常

就我而言,没有异常(exception)!

我反编译了 System.Net 程序集,发现在某些情况下确实可以。但是我不明白这些条件是什么意思(System.Net.Connection.SubmitRequest(HttpWebRequest request, bool forcedsubmit)):

if (this.m_Free && this.m_WriteDone && !forcedsubmit && (this.m_WriteList.Count == 0 || request.Pipelined && !request.HasEntityBody && (this.m_CanPipeline && this.m_Pipelining) && !this.m_IsPipelinePaused))
{
this.m_Free = false;
needReConnect = this.StartRequest(request, true);
if (needReConnect == TriState.Unspecified)
{
flag = true;
this.PrepareCloseConnectionSocket(ref returnResult);
this.Close(0);
}
}

什么时候以及为什么这可能?

最佳答案

CompletedSynchronously 属性找到了这个
使用此属性确定异步操作是否同步完成。例如,如果 I/O 请求很小,此属性可以为异步 I/O 操作返回 true。
HERE

编辑:- 我怀疑响应可能被缓存了。因此尝试使用 request.CachePolicy = new HttpRequestCachePolicy(/*caching type*/);

停止响应缓存

关于c# - 为什么 HttpWebRequest.BeginGetResponse() 同步完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12283779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com