Is it possible to reset the ServicePointManager?(是否可以重置 ServicePointManager?)
问题描述
我正在尝试遵循与 System.Net.Mail.SMTPClient 如何进行本地 IP 绑定 我在具有多个 IP 地址的机器上使用 Windows 7 和 .Net 4.0.我定义了 BindIPEndPointDelegate
I am attempting to following the code similar to the one given at How does System.Net.Mail.SMTPClient do its local IP binding I am using Windows 7 and .Net 4.0 on a machine with multiple IP Addresses. I have the BindIPEndPointDelegate defined
private static IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
{
string IPAddr = //some logic to return a different IP each time
return new IPEndPoint(IPAddr, 0);
}
然后我使用
SmtpClient client = new SmtpClient();
client.Host = SMTP_SERVER; //IP Address as string
client.Port = 25;
client.EnableSsl = false;
client.ServicePoint.BindIPEndPointDelegate
= new System.Net.BindIPEndPoint(BindIPEndPointCallback);
client.ServicePoint.ConnectionLeaseTimeout = 0;
client.Send(msg); //msg is of type MailMessage properly initialized/set
client = null;
第一次调用此代码时,将调用委托,并且无论设置什么 IP 地址,都会使用它.随后调用此代码时,委托永远不会被调用,即随后使用第一个 IP 地址. 是否可以改变这种每次调用代码时调用委托回调的情况?
The first time this code gets called, the delegate gets called and whatever IP address gets set, it gets used. The subsequent times this code gets called, the delegate never gets called i.e. the first IP Address is used subsequently. Is it possible to change this situation where each time the code gets called, the delegate callback is called?
我在想 ServicePointManager(其中是一个静态类)缓存第一次调用委托的结果.可以重置这个类吗?我不在乎性能.
I am thinking the ServicePointManager (which is a static class) caches the result of the first call to the delegate. Is it possible to reset this class? I don’t care about performance.
谢谢你,O.O.
推荐答案
我遇到了类似的问题,想重置 ServicePointManager 并为不同的测试结果更改证书.对我有用的方法是将 MaxServicePointIdleTime 设置为一个较低的值,这将有效地重置它.
I ran into a similar problem and wanted to reset ServicePointManager and changing certs for different outcomes of tests. The way that worked for me was to set MaxServicePointIdleTime to a low value, which would effectively reset it.
ServicePointManager.MaxServicePointIdleTime = 1;
这篇关于是否可以重置 ServicePointManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:是否可以重置 ServicePointManager?
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 输入按键事件处理程序 2022-01-01
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01