【发布时间】:2020-10-17 09:04:59
【问题描述】:
我在 EXCEL COM 插件中使用 HttpClient 类时遇到了奇怪的行为。
首次尝试调用 API 需要 20-30 秒。 之后,相同的请求或任何其他请求几乎立即返回。下面是我正在运行的代码:
加载项启动时(调用一次)
System.Net.ServicePointManager.SecurityProtocol |= System.Net.SecurityProtocolType.Tls12;
Client = new HttpClient() { BaseAddress = new Uri("https://powertoolstest.mvwc.com/") };
Client.DefaultRequestHeaders.Accept.Clear();
第一次使用HttpClient:
DateTime Start = DateTime.Now;
var result = await Client.GetAsync("api/Ping");
result.EnsureSuccessStatusCode();
DateTime End = DateTime.Now;
TimeSpan TotalTime = End - Start;
- 第一次执行此代码可能需要 20-30 秒。
- 所有后续尝试都在 1 秒内完成。通常为 300-500 毫秒。
- 直到尝试运行了几乎所有 30 秒,wireshark 跟踪才发现活动。一旦它确实收到了请求,请求本身就需要 300-500 毫秒。这消除了服务器配置作为可能的罪魁祸首。
延迟似乎在 System.Net.Sockets.Socket.DoBind 中。具体来说,我相信它是在它调用时:
// This may throw ObjectDisposedException.
SocketError errorCode = UnsafeNclNativeMethods.OSSOCK.bind(
m_Handle,
socketAddress.m_Buffer,
socketAddress.m_Size);
- 使用 .NET Framework 4.6.1
- 我们在独立桌面应用程序 (.NET Core) 中使用 HTTPClient,从未遇到过此问题。
- 使用 4.6.1 构建控制台应用程序并使用相同的代码,也无法重现。只发生在 EXCEL COM 插件中。
我尝试关闭代理搜索,但没有帮助。有关可能导致此延迟的安全设置或配置的任何建议?
初始请求的堆栈跟踪:
System.dll!System.Net.Sockets.Socket.DoBind(System.Net.EndPoint endPointSnapshot, System.Net.SocketAddress socketAddress) Line 940 C# Taking 20-30 seconds on HTTPClient
System.dll!System.Net.Sockets.Socket.InternalBind(System.Net.EndPoint localEP) Line 934 C#
System.dll!System.Net.Sockets.Socket.BeginConnectEx(System.Net.EndPoint remoteEP, bool flowContext, System.AsyncCallback callback, object state) Line 6899 C#
System.dll!System.Net.Sockets.Socket.UnsafeBeginConnect(System.Net.EndPoint remoteEP, System.AsyncCallback callback, object state) Line 2852 C#
System.dll!System.Net.ServicePoint.ConnectSocketInternal(bool connectFailure, System.Net.Sockets.Socket s4, System.Net.Sockets.Socket s6, ref System.Net.Sockets.Socket socket, ref System.Net.IPAddress address, System.Net.ServicePoint.ConnectSocketState state, System.IAsyncResult asyncResult, out System.Exception exception) Line 1449 C#
System.dll!System.Net.ServicePoint.GetConnection(System.Net.PooledStream PooledStream, object owner, bool async, out System.Net.IPAddress address, ref System.Net.Sockets.Socket abortSocket, ref System.Net.Sockets.Socket abortSocket6) Line 264 C#
System.dll!System.Net.PooledStream.Activate(object owningObject, bool async, System.Net.GeneralAsyncDelegate asyncCallback) Line 157 C#
System.dll!System.Net.Connection.CompleteStartConnection(bool async, System.Net.HttpWebRequest httpWebRequest) Line 1084 C#
System.dll!System.Net.Connection.CompleteStartRequest(bool onSubmitThread, System.Net.HttpWebRequest request, System.Net.TriState needReConnect) Line 953 C#
System.dll!System.Net.Connection.SubmitRequest(System.Net.HttpWebRequest request, bool forcedsubmit) Line 752 C#
System.dll!System.Net.ServicePoint.SubmitRequest(System.Net.HttpWebRequest request, string connName) Line 409 C#
System.dll!System.Net.HttpWebRequest.SubmitRequest(System.Net.ServicePoint servicePoint) Line 4188 C#
System.dll!System.Net.HttpWebRequest.BeginGetResponse(System.AsyncCallback callback, object state) Line 2021 C#
System.Net.Http.dll!System.Net.Http.HttpClientHandler.StartGettingResponse(System.Net.Http.HttpClientHandler.RequestState state) Line 1089 C#
System.Net.Http.dll!System.Net.Http.HttpClientHandler.StartRequest(object obj) Line 946 C#
System.Net.Http.dll!System.Net.Http.HttpClientHandler.SendAsync.AnonymousMethod__0() Line 921 C#
mscorlib.dll!System.Threading.Tasks.Task.InnerInvoke() Line 2884 C#
mscorlib.dll!System.Threading.Tasks.Task.Execute() Line 2498 C#
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj) Line 2861 C#
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 980 C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 928 C#
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Line 2827 C#
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution) Line 2767 C#
mscorlib.dll!System.Threading.Tasks.Task.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() Line 2704 C#
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Line 820 C#
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Line 1161 C#
【问题讨论】:
标签: .net sockets com httpclient