是否可以重置ServicePointManager?

我试图遵循类似于System.Net.Mail.SMTPClient如何进行本地IP绑定的代码我在具有多个IP地址的计算机上使用Windows 7和.Net 4.0。 我定义了BindIPEndPointDelegate

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地址。 是否有可能改变这种情况,每次调用代码时,调用委托回调?

我在想ServicePointManager (它是一个静态类 )缓存第一次调用委托的结果。 是否可以重置此课程? 我不关心表现。

谢谢,OO

我遇到了类似的问题,并希望重置ServicePointManager并更改不同测试结果的证书。 对我有用的方法是将MaxServicePointIdleTime设置为一个较低的值,这将有效地重置它。

 ServicePointManager.MaxServicePointIdleTime = 1; 

我在上面发布的问题中面临的问题是,所有电子邮件都将使用第一条消息的IP发送出去。 我认为某些东西(可能是ServicePointManager正在缓存连接。 虽然我没有找到重置ServicePointManager的解决方案,但我意识到我上面尝试设置client = null; 即使你调用GC.Collect();也不会真正关闭连接GC.Collect(); 不久之后。 我发现的唯一工作是:

 SmtpClient client = new SmtpClient(); //Remaining code here.... client.Send(msg); client.Dispose(); //Secret Sauce 

调用client.Dispose(); 发送每条消息后,总是重置连接,因此下一条消息可以选择需要输出的IP地址。

OO