本文介绍了 HttpClient 的一个众所周知的问题,该问题可能导致套接字耗尽。
我有一个 ASP.NET Core 3.1 Web 应用程序。在 .NET Standard 2.0 类库中,我按照此说明在 Visual Studio 2019 中添加了 WCF Web 服务引用。
在服务中,我按照文档中描述的方式使用 WCF 客户端。创建 WCF 客户端的实例,然后针对每个请求关闭客户端。
public class TestService
{
public async Task<int> Add(int a, int b)
{
CalculatorSoapClient client = new CalculatorSoapClient();
var resultat = await client.AddAsync(a, b);
//this is a bad way to close the client I should also check
//if I need to call Abort()
await client.CloseAsync();
return resultat;
}
}
Run Code Online (Sandbox Code Playgroud)
我知道在没有任何检查的情况下关闭客户端是不好的做法,但就本示例而言,这并不重要。
当我启动应用程序并向使用 …