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?


基础教程推荐
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01