是否可以重置 ServicePointManager? [英] Is it possible to reset the ServicePointManager?

查看:26
本文介绍了是否可以重置 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆